android仿Adapter自定义PagerAdapter

移动开发 简书

为增加PagerAdapter的可用性与可读性,自定义了一下PagerAdapter

public abstract class EjBasePagerAdapter extends PagerAdapter {

    protected List list;
    protected SparseArray<ejbasepagerholder> sparseArray;

    public EjBasePagerAdapter(List list){
        this.list = list;
        sparseArray = new SparseArray();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    public String getIcon(int position){
        return null;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        EjBasePagerHolder holder = sparseArray.get(getSparsePosition(position));
        if(null == holder){
            holder = onBundHolder(container);
            holder.setPosition(position);
            holder.bindDada(list.get(position),position);
            sparseArray.put(getSparsePosition(position),holder);
        }
        View view = holder.itemView;
        container.addView(view);
        return view;
    }

    @Override
    public void notifyDataSetChanged() {
        sparseArray.clear();
        super.notifyDataSetChanged();
    }

    protected int getSparsePosition(int position){
        return (position+10)*2;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    protected View inflate(int resorce, ViewGroup viewGroup) {
        return LayoutInflater.from(viewGroup.getContext()).inflate(resorce, viewGroup, false);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    protected abstract EjBasePagerHolder onBundHolder(ViewGroup container);

    public void onDestroy(){

    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {

    }

    protected abstract class EjBasePagerHolder{
        /**上下文*/
        protected Context mContext;
        private int position;

        SparseArray sparseArray;
        View itemView;

        protected EjBasePagerHolder(View itemView){
            this.itemView = itemView;
            this.mContext = itemView.getContext();
            sparseArray = new SparseArray();
            initView();
        }

        public abstract void initView();

        public View findViewById(int id){
            View view = sparseArray.get(id);
            if(null == view){
                view = itemView.findViewById(id);
                sparseArray.append(id,view);
            }
            return view;
        }

        public abstract void bindDada(D t,int position);

        public void onActivityResult(int requestCode, int resultCode, Intent data) {

        }

        public void setPosition(int position) {
            this.position = position;
        }

        public int getPosition() {
            return position;
        }

        public void onDestroy(){
            sparseArray.clear();
        }
    }
}</ejbasepagerholder

简单用法

public class EjMainFmtBannerPagerAdapter extends EjBasePagerAdapter{

    public EjMainFmtBannerPagerAdapter(List list) {
        super(list);
    }

    @Override
    protected EjBasePagerHolder onBundHolder(ViewGroup container) {
        return new EjBasePagerHolder(LayoutInflater.from(container.getContext()).inflate(R.layout.ejmain_item_fmt_banner,null)) {

            private ImageView ejmain_iv_banner;

            @Override
            public void initView() {
                ejmain_iv_banner = (ImageView)findViewById(R.id.ejmain_iv_banner);
            }

            @Override
            public void bindDada(EjMainFragmentDataBean t, int position) {
                GlidUtil.loadUrl(t.img1,ejmain_iv_banner,mContext);
            }
        };
    }
}
简书稿源:简书 (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » android仿Adapter自定义PagerAdapter

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录