StickyListHeadersAdapter

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xueshao110/article/details/88980647

StickyListHeaders这个控件目前被很多app广泛应用,翻译过来的意思是“ 粘列表标题”。

使用步骤

1  implementation 'se.emilsjolander:stickylistheaders:2.7.0'

2  适配器implements StickyListHeadersAdapter其他用法和ListView一样

public class GoodsAdapter extends BaseAdapter implements StickyListHeadersAdapter {
    private Context con;
    private List<DataBean> data;

    public GoodsAdapter(Context con) {
        this.con = con;
    }

   //分类顶部布局
    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        TextView inflate = (TextView) View.inflate(con, R.layout.item_type_header, null);
        inflate.setText(data.get(position).getGoodsClass());
        return inflate;
    }
    //返回的类型总数
    @Override
    public long getHeaderId(int position) {
        return data.get(position).getID();
    }

    @Override
    public int getCount() {
        if (data != null && data.size() > 0) {

            return data.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {

        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(con, R.layout.item_goods, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        DataBean dataBean = data.get(position) ;
        Glide.with(con)
                .load(data.get(position).getImgUrl())
                .into(holder.ivIcon);
        holder.tvName.setText(dataBean.getShopName());

        return convertView;


    }

    public void setData(List<DataBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public List<DataBean> getdata() {
        return this.data ;
    }

    static class ViewHolder {
       @BindView(R.id.iv_icon)
        ImageView ivIcon;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_zucheng)
        TextView tvZucheng;
       @BindView(R.id.tv_newprice)
        TextView tvNewprice;
        @BindView(R.id.tv_oldprice)
        TextView tvOldprice;
       @BindView(R.id.ib_minus)
        ImageButton ibMinus;
        @BindView(R.id.tv_count)
        TextView tvCount;
        @BindView(R.id.ib_add)
        ImageButton ibAdd;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
            // tvName =(TextView) view.findViewById(R.id.tv_name);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/xueshao110/article/details/88980647