一、定義
提供一種方法順序訪問一個容器對象中的各個元素,而又不需要暴露該對象的內部表示。
二、UML圖
三、簡單實現
①迭代器接口
public interface Iterator<T> {
/**
* 是否還有下一個元素
* @return true表示有 ,false表示沒有
*/
boolean hasNext();
/**
* 返回當前位置的元素并將位置移至下一位
* @return
*/
T next();
}
②具體迭代器類
public class ConcreteIterator<T> implements Iterator<T> {
private List<T> list=new ArrayList<>();
private int cursor=0;
public ConcreteIterator(List<T> list){
this.list=list;
}
@Override
public boolean hasNext() {
return cursor!=list.size();
}
@Override
public T next() {
T obj=null;
if (this.hasNext()){
obj=this.list.get(cursor++);
}
return obj;
}
}
③容器接口
public interface Aggregate<T> {
void add(T obj);
void remove(T obj);
Iterator<T> iterator();
}
④具體容器類
public class ConcreteAggregate<T> implements Aggregate<T> {
private List<T> list= new ArrayList<T>();
@Override
public void add(T obj) {
list.add(obj);
}
@Override
public void remove(T obj) {
list.remove(obj);
}
@Override
public Iterator<T> iterator() {
return new ConcreteIterator<T>(list);
}
}
⑤使用
Aggregate<String> objectConcreteAggregate = new ConcreteAggregate<>();
objectConcreteAggregate.add("1111");
objectConcreteAggregate.add("222");
objectConcreteAggregate.add("333");
Iterator<String> iterator = objectConcreteAggregate.iterator();
while (iterator.hasNext()){
Log.e("tag",iterator.next());
}
四、自定義View,使用迭代器模式
①創建適配器
public interface TabIterator {
BottomTabItem next();
boolean hashNext();
}
實現類
public class TabListIterator<T extends BottomTabItem> implements TabIterator {
private final ArrayList<T> items;
private int index;
public TabListIterator(){
items = new ArrayList<>();
}
public void addItem(T item){
items.add(item);
}
@Override
public BottomTabItem next() {
return items.get(index++);
}
@Override
public boolean hashNext() {
return index<items.size();
}
}
②創建容器類
public abstract class BottomTabItem {
private View mTabItemView;
private int mLayoutId;
private Context mContext;
public BottomTabItem(int layoutId,Context context){
this.mLayoutId=layoutId;
this.mContext=context;
}
public View getTabView(){
if(mTabItemView == null){
mTabItemView = LayoutInflater.from(mContext).inflate(mLayoutId,null);
initLayout();
}
return mTabItemView;
}
/**
* 初始化顯示
*/
protected abstract void initLayout();
protected <T> T findViewById(int id){
return (T)mTabItemView.findViewById(id);
}
/**
* 是否選擇當前條目
* @param selected
*/
protected abstract void setSelected(boolean selected);
}
實現類
public class MainBottomTabItem extends BottomTabItem {
private Builder mBuilder;
private MainBottomTabItem(Context context) {
super(R.layout.tab_main_bottom_item, context);
}
public MainBottomTabItem(Builder builder){
this(builder.context);
this.mBuilder=builder;
}
@Override
protected void initLayout() {
TextView tabText = findViewById(R.id.tab_text);
ImageView tabIcon = findViewById(R.id.tab_icon);
if(!TextUtils.isEmpty(mBuilder.text)){
tabText.setText(mBuilder.text);
}
if(mBuilder.resIconId != 0){
tabIcon.setImageResource(mBuilder.resIconId);
}
}
@Override
protected void setSelected(boolean selected) {
TextView tabText = findViewById(R.id.tab_text);
ImageView tabIcon = findViewById(R.id.tab_icon);
tabText.setSelected(selected);
tabIcon.setSelected(selected);
}
public static class Builder{
Context context;
String text;
int resIconId;
public Builder(Context context){
this.context = context;
}
public Builder text(String text){
this.text = text;
return this;
}
public Builder resIcon(int resIconId){
this.resIconId = resIconId;
return this;
}
public MainBottomTabItem create(){
return new MainBottomTabItem(this);
}
}
}
③自定義view
public class TabBottomNavigation extends LinearLayout {
private List<BottomTabItem> mTabItems;
private int mCurrentIndex = -1;
public TabBottomNavigation(Context context) {
this(context,null);
}
public TabBottomNavigation(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TabBottomNavigation(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//設置水平排列
setOrientation(HORIZONTAL);
mTabItems = new ArrayList<>();
}
public void addTabItem(TabIterator tabIterator){
mTabItems.clear();
// 當前的位置
int index = 0;
while (tabIterator.hashNext()){
BottomTabItem tabItem = tabIterator.next();
View tabView = tabItem.getTabView();
addView(tabView);
LinearLayout.LayoutParams params = (LayoutParams) tabView.getLayoutParams();
params.weight = 1;
params.gravity = Gravity.CENTER;
tabView.setLayoutParams(params);
// 給條目設置點擊事件,等等
setItemClickListener(tabView,index++);
mTabItems.add(tabItem);
}
// 第一個位置要設置為選中
mTabItems.get(0).setSelected(true);
mCurrentIndex = 0;
}
private void setItemClickListener(View tabView, final int position) {
tabView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(mCurrentIndex != position){
// 原來的標為非選中
mTabItems.get(mCurrentIndex).setSelected(false);
// 把當前設置為選中
mCurrentIndex = position;
mTabItems.get(mCurrentIndex).setSelected(true);
// 把點擊的為用接口回調出去供外部使用,調整顯示
}
}
});
}
}
④使用
TabListIterator<MainBottomTabItem> listIterator = new TabListIterator<>();
listIterator.addItem(new MainBottomTabItem.Builder(this)
.resIcon(R.drawable.main_tab_item).text("text1").create());
listIterator.addItem(new MainBottomTabItem.Builder(this)
.resIcon(R.drawable.main_tab_item).text("text2").create());
listIterator.addItem(new MainBottomTabItem.Builder(this)
.resIcon(R.drawable.main_tab_item).text("text3").create());
mTabBottomNavigation.addTabItem(listIterator);