BaseQuickAdapter 函数用法笔记 recyclerView 封装adapter

包引入: 

implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.47'

adapter:

public class TeamPoastAdapter extends  BaseQuickAdapter {
    private int position = -1;

    private Context mContext;

    public TeamPoastAdapter(Context context, List data) {
        super(R.layout.itme_equip, data);
        mContext = context;
    }

    public void setSelection(int pos) {
        this.position = pos;
        notifyDataSetChanged();
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, TeamPostEntity item) {
        //set TextView
        helper.setTextColor(R.id.tv_txt, helper.getLayoutPosition() == position ? Color.parseColor("#108EE9") : Color.parseColor("#363636"));
        helper.setText(R.id.tv_txt, item.getDicPorperty())
                .setText(R.id.score, + item.getAverage());
        
        helper.addOnClickListener(R.id.tv_txt);
    }
}

 

activity中 视图填充

LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rvTeampost.setLayoutManager(layoutManager);

adapter=new TeamPoastAdapter(context,list);
recycle.setAdapter(adapter);

itme点击事件封装

adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
    @Override
    public void onItemClick(BaseQuickAdapter adapters, View view, int position) {
        adapter.setSelection(position);//更新adapter 显示点击效果
        teamPostEntity=list.get(position);
    }
});

itme 子view的点击事件封装

adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
    @Override
    public void onItemChildClick(BaseQuickAdapter adapters, View view, int position) {
        adapter.setSelection(position);
        teamPostEntity=list.get(position);
    }
});

这一步需要到 adapter convert 中注册要点击的view

helper.addOnClickListener(R.id.tv_txt);

长安事件同上。

https://www.jianshu.com/p/40457c16e44a

你可能感兴趣的:(android)