美文网首页
迭代器模式

迭代器模式

作者: Ayres | 来源:发表于2017-11-06 18:01 被阅读0次

    一、定义

    提供一种方法顺序访问一个容器对象中的各个元素,而又不需要暴露该对象的内部表示。

    二、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);
    

    相关文章

      网友评论

          本文标题:迭代器模式

          本文链接:https://www.haomeiwen.com/subject/gznlmxtx.html