有关列表的视图优化

public class InfoAdapter extends BaseAdapter {
    private Context context;
    private List<NewsResponse.ResultBean> mNewResponse;
    private List<BannerResponse.DataBean> mBannerResponse;

    public InfoAdapter(Context context) {
        this.context = context;
        mNewResponse = new ArrayList<>();
        mBannerResponse = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return hasBanner() ? mNewResponse.size() + 1 : mNewResponse.size();
    }

    //设置banner数据
    public void setBannerDatas(List<BannerResponse.DataBean> datas) {
        if (datas != null) {
            mBannerResponse = datas;
        }
        notifyDataSetChanged();
    }

    //更新数据
    public void setNewDatas(List<NewsResponse.ResultBean> datas) {
        if (datas != null) {
            mNewResponse = datas;
        }
        notifyDataSetChanged();
    }

    //加载数据
    public void addNewDatas(List<NewsResponse.ResultBean> datas) {
        if (datas != null) {
            mNewResponse.addAll(datas);
        }
        notifyDataSetChanged();
    }

    private BannerViewHolder viewHolder;

    public void stop() {
        if (viewHolder != null) {
            viewHolder.stop();
        }
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (hasBanner() && position == 0) {
            return 0;
        }
        return 1;
    }

    //是否有banner
    private boolean hasBanner() {
        return mBannerResponse.size() > 0;
    }

    @Override
    public NewsResponse.ResultBean getItem(int position) {
        if (hasBanner()){
            if (position == 0){
                return null;
            }
            return mNewResponse.get(position - 1);
        }
        return mNewResponse.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (getItemViewType(position) == 0) {

            //Banner
            BannerViewHolder bannerViewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.item_banner, parent, false);
                bannerViewHolder = new BannerViewHolder(convertView);
            } else {
                bannerViewHolder = (BannerViewHolder) convertView.getTag();

            }
            this.viewHolder = bannerViewHolder;
            bannerViewHolder.bindData(mBannerResponse);
        } else {


            NewsViewHolder viewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.item_news, parent, false);
                viewHolder = new NewsViewHolder(convertView);
            } else {
                viewHolder = (NewsViewHolder) convertView.getTag();
            }
            viewHolder.bindData(getItem(position));
        }
        return convertView;
    }

    class BannerViewHolder {
        private final BannerAdapter adapter;
        ViewPager viewPager;
        private Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
                startSwitchPage();
            }
        };

        public BannerViewHolder(View itemView) {
            this.viewPager = itemView.findViewById(R.id.banner);
            viewPager.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        stop();
                    }
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        startSwitchPage();
                    }
                    return false;
                }
            });
            adapter = new BannerAdapter();
            this.viewPager.setAdapter(adapter);
            itemView.setTag(this);

        }

        public void bindData(List<BannerResponse.DataBean> bean) {

            adapter.setBannerDatas(bean);
            stop();
            if (bean.size() > 1) {
                viewPager.setCurrentItem(bean.size() * 5000);
                startSwitchPage();
            }

        }

        public void stop() {
            handler.removeMessages(0);
        }

        public void startSwitchPage() {
            if (viewPager.getAdapter().getCount() > 1) {
                handler.sendMessageDelayed(handler.obtainMessage(0), 2000);

            }

        }
    }

    class NewsViewHolder {
        TextView name;
        TextView summary;
        ImageView icon;

        public NewsViewHolder(View itemView) {
            this.name = itemView.findViewById(R.id.name);
            this.summary = itemView.findViewById(R.id.summary);
            icon = itemView.findViewById(R.id.icon);
            itemView.setTag(this);

        }

        public void bindData(NewsResponse.ResultBean bean) {
            name.setText(bean.getName());
            summary.setText(bean.getSummary());
            ImageLoader.getInstance().displayImage(bean.getImageUrl(), icon);

        }
    }

}

在这里插入代码片
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值