1、简介
该篇文章记录的是关于我们日常开发中常用的RecyclerView的一些小的细节。开发中用的比较多的有BRVAH框架,用于快速构建Adapter,介绍了添加头布局索引值的修改,还有设置空布局。以及滑动监听事件。
不知道大家有没有关注到列表的索引,像Java中的数组和List就是从索引0开始的,像我们时常使用的BRVAH(一款Adapter帮助工具)有必要知道返回的索引值是多少。因为这关乎我们更新的那个条目,或者说修正Data的值。
这里还记录的recyclerView的局部刷新的方式,能够有效的解决图片闪烁的效果,这里还是好好的记录一下吧,防止后面会用到。
2、细节探讨
2.1索引值
探究在内部类和外部类中 help.getAdapterPosition( )中的值
2.1.1 首先看一下没有头布局
mAdapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_scroll,mList) {
@Override
protected void convert(BaseViewHolder helper, String item) {
helper.setText(R.id.tv,item);
Log.e("RecyclerView ID",
"convert helper.getAdapterPosition() ="+helper.getAdapterPosition());
Log.e("RecyclerView ID","convert helper.getLayoutPosition() = "+helper.getLayoutPosition());
}
};
mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
Log.e("RecyclerView ID","tOnItemClick position ="+position);
}
});
E/RecyclerView ID: convert helper.getAdapterPosition() =0
E/RecyclerView ID: convert helper.getLayoutPosition() = 0
E/RecyclerView ID: convert helper.getAdapterPosition() =1
E/RecyclerView ID: convert helper.getLayoutPosition() = 1
E/RecyclerView ID: convert helper.getAdapterPosition() =2
E/RecyclerView ID: convert helper.getLayoutPosition() = 2
E/RecyclerView ID: convert helper.getAdapterPosition() =3
E/RecyclerView ID: convert helper.getLayoutPosition() = 3
E/RecyclerView ID: convert helper.getAdapterPosition() =4
E/RecyclerView ID: convert helper.getLayoutPosition() = 4
E/RecyclerView ID: convert helper.getAdapterPosition() =5
E/RecyclerView ID: convert helper.getLayoutPosition() = 5
E/RecyclerView ID: convert helper.getAdapterPosition() =6
E/RecyclerView ID: convert helper.getLayoutPosition() = 6
点击 i = 10的条目 OK的
E/RecyclerView ID: tOnItemClick position =10
小结论: 该例说明索引与我们预期一致。经测试在外部类也是一致的
2.1.2 为其添加一个头部View
View view = new View(this);
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,50));
view.setBackgroundColor(getResources().getColor(R.color.colorAccent));
mAdapter = new MyTestAdapter(R.layout.item_scroll,mList);
mAdapter.addHeaderView(view);
接着看打印的信息
E/RecyclerView ID: convert helper.getAdapterPosition() =1
E/RecyclerView ID: convert helper.getLayoutPosition() = 1
E/RecyclerView ID: convert helper.getAdapterPosition() =2
E/RecyclerView ID: convert helper.getLayoutPosition() = 2
E/RecyclerView ID: convert helper.getAdapterPosition() =3
E/RecyclerView ID: convert helper.getLayoutPosition() = 3
E/RecyclerView ID: convert helper.getAdapterPosition() =4
E/RecyclerView ID: convert helper.getLayoutPosition() = 4
E/RecyclerView ID: convert helper.getAdapterPosition() =5
E/RecyclerView ID: convert helper.getLayoutPosition() = 5
E/RecyclerView ID: convert helper.getAdapterPosition() =6
E/RecyclerView ID: convert helper.getLayoutPosition() = 6
E/RecyclerView ID: convert helper.getAdapterPosition() =7
E/RecyclerView ID: convert helper.getLayoutPosition() = 7
同样我们再点击list ID为10的条目
E/RecyclerView ID: tOnItemClick position =10
那么对于 mAdapter.notifyItemRemove