当前位置:网站首页>After adding the header layout to the recyclerview, use the adapter Notifyitemchanged (POS,'test') invalid local refresh
After adding the header layout to the recyclerview, use the adapter Notifyitemchanged (POS,'test') invalid local refresh
2022-06-11 05:36:00 【Cockroach bully BBQ】
to RecyclerView Adding a header layout uses HeaderAndFooterWrapper, Then what I do is slide the list to play short videos , But in every video Item There are grass pulling buttons and numbers under the , Click to plant grass or pull grass to refresh the number . Use RecyclerView Local refresh of
headerAndFooterWrapper.notifyItemChanged(zhong_ba_cao_pos+1,"test");
then build I found that the number did not change after clicking , Think there's something wrong with the code and go all the way debug It is found that the method has indeed been executed , It's just that I haven't called HeaderAndFooterWrapper The actual list adapter included is
mInnerAdapter Medium onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull List<Object> payloads)
Then I started to spend my time ing( Why didn't you call onBindViewHolder Three parameter function of ?)
The same thing is said in the adapter onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull List<Object> payloads) Medium judgement payloads If it is empty, how about , But I've been mInnerAdapter Of onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull List<Object> payloads) Yes ;
os:what ghost ?
Then I wrote a simple RecyclerView Partially refreshed demo Run discovery to refresh local item Of , There is no problem with the code , And then put RecyclerView Of adapter from headerAndFooterWrapper Become a headless layout mInnerAdapter, Step by step, narrow the scope of the problem , After the change, it is found that the part can also be refreshed , It seems that the problem lies in the header layout adapter HeaderAndFooterWrapper On .
stay HeaderAndFooterWrapper The same construct was found in the file
onBindViewHolder(RecyclerView.ViewHolder holder, int position) But there are no three parameters , brainwave , I added one, namely
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull List<Object> payloads) {
if(payloads.isEmpty()){
onBindViewHolder(holder,position);
}else{
mInnerAdapter.onBindViewHolder(holder,position - getHeadersCount(),payloads);
}
}
Post run discovery ok 了 , Problem solving . For a long time in the afternoon , Make a note of . Enclosed HeaderAndFooterWrapper The code of mInnerAdapter The code for is as follows , Just look at the two adapters onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull List<Object> payloads) Functions :
public class HeaderAndFooterWrapper extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
public static final int BASE_ITEM_TYPE_HEADER = 100000;
public static final int BASE_ITEM_TYPE_FOOTER = 200000;
private SparseArrayCompat<View> mHeaderViews = new SparseArrayCompat<>();
private SparseArrayCompat<View> mFooterViews = new SparseArrayCompat<>();
private RecyclerView.Adapter mInnerAdapter;
public HeaderAndFooterWrapper(RecyclerView.Adapter innerAdapter) {
mInnerAdapter = innerAdapter;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (mHeaderViews.get(viewType) != null){
return new MyViewHolder(mHeaderViews.get(viewType));
}else if (mFooterViews.get(viewType) != null){
return new MyViewHolder(mFooterViews.get(viewType));
}
return mInnerAdapter.onCreateViewHolder(parent,viewType);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull List<Object> payloads) {
if(payloads.isEmpty()){
onBindViewHolder(holder,position);
}else{
mInnerAdapter.onBindViewHolder(holder,position - getHeadersCount(),payloads);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (isHeaderViewPos(position))
return;
if (isFooterViewPos(position))
return;
mInnerAdapter.onBindViewHolder(holder,position - getHeadersCount());
}
@Override
public int getItemCount() {
return getHeadersCount() + getFootersCount() + getRealItemCount();
}
@Override
public int getItemViewType(int position) {
if (isHeaderViewPos(position)){
return mHeaderViews.keyAt(position);
}else if (isFooterViewPos(position)){
return mFooterViews.keyAt(position - getHeadersCount() - getRealItemCount());
}
return mInnerAdapter.getItemViewType(position - getHeadersCount());
}
/**
* To obtain normal data size
* @return
*/
private int getRealItemCount(){
return mInnerAdapter.getItemCount();
}
/**
* Judge whether it is Header
* @param position
* @return
*/
private boolean isHeaderViewPos(int position){
return position < getHeadersCount();
}
/**
* Judge whether it is Footer
* @param position
* @return
*/
private boolean isFooterViewPos(int position){
return position >= getHeadersCount() + getRealItemCount();
}
public void addHeaderView(View view){
mHeaderViews.put(mHeaderViews.size() + BASE_ITEM_TYPE_HEADER,view);
}
public void addFooterView(View view){
mFooterViews.put(mFooterViews.size() + BASE_ITEM_TYPE_FOOTER,view);
}
private int getHeadersCount(){
return mHeaderViews.size();
}
public int getFootersCount(){
return mFooterViews.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
public MyViewHolder(View itemView) {
super(itemView);
}
}
/**
* Fit grid layout
* @param recyclerView
*/
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
mInnerAdapter.onAttachedToRecyclerView(recyclerView);
final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if (layoutManager instanceof GridLayoutManager){
final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return isHeaderViewPos(position) || isFooterViewPos(position) ? ((GridLayoutManager) layoutManager).getSpanCount() : 1;
}
});
}
}
@Override
public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
mInnerAdapter.onViewAttachedToWindow(holder);
int position = holder.getLayoutPosition();
if (isHeaderViewPos(position)||isFooterViewPos(position)){
ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
if (lp != null && lp instanceof StaggeredGridLayoutManager.LayoutParams){
StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;
p.setFullSpan(true);
}
}
}
}
public class AdapterRecyclerView extends RecyclerView.Adapter<AdapterRecyclerView.MyViewHolder> {
public static final String TAG = "AdapterRecyclerView";
private Context context;
private MyData videoData;
public AdapterRecyclerView(Context context, MyData videoData) {
this.context = context;
this.videoData = videoData;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
context).inflate(R.layout.item_videoview, parent,
false));
return holder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position, @NonNull List<Object> payloads) {
if(payloads.isEmpty()){
onBindViewHolder(holder,position);
}else{
MyRow rowItem = videoData.get(position);
if (MathUtils.getInt(rowItem.get("user_like_flag") + "") == 1) { //like_main_bottom
holder.tv_font_zhong_cao.setTextColor(context.getResources().getColor(R.color.my_red));
} else {
holder.tv_font_zhong_cao.setTextColor(context.getResources().getColor(R.color.gray_color_d9d9d9));
}
if (MathUtils.getInt(rowItem.get("user_dislike_flag") + "") == 1) { //like_main_bottom
holder.tv_font_ba_cao.setTextColor(context.getResources().getColor(R.color.my_red));
} else {
holder.tv_font_ba_cao.setTextColor(context.getResources().getColor(R.color.gray_color_d9d9d9));
}
holder.zhong_cao_num.setText(MathUtils.getInt(rowItem.get("like_count") + "") + "");
holder.ba_cao_num.setText(MathUtils.getInt(rowItem.get("dislike_count") + "") + "");
}
}
@SuppressLint("LongLogTag")
@Override
public void onBindViewHolder(MyViewHolder holder, int position) { // Inside is my business logic Never mind
MyRow rowItem = videoData.get(position);
holder.userAvatar.setTag(null);
Glide.with(context.getApplicationContext()).asBitmap().load((rowItem.getString("avatar_url"))).into(holder.userAvatar);
holder.userAvatar.setTag(rowItem.getInt("user_id"));
Log.i(TAG, "onBindViewHolder [" + holder.jzvdStd.hashCode() + "] position=" + position);
holder.jzvdStd.setUp(
videoData.get(position).getString("play_addr"),
"", Jzvd.SCREEN_FULLSCREEN);
Glide.with(holder.jzvdStd.getContext()).load( videoData.get(position).getString("cover_url")).into(holder.jzvdStd.posterImageView);
LinearLayout.LayoutParams lps = (LinearLayout.LayoutParams) holder.layout_card.getLayoutParams();
lps.width = ScreenUtils.getScreenWidth(context)-ScreenUtils.dp2px(context,10);
lps.height = (int) (lps.width/0.727);
holder.layout_card.setLayoutParams(lps);
holder.layout_card.setRadius(5f);
holder.video_des.setText(rowItem.getString("video_des"));
if(rowItem.getBoolean("isShowLink")){
holder.ll_shop_tab.setVisibility(View.VISIBLE);
}else{
holder.ll_shop_tab.setVisibility(View.GONE);
}
MyRow r = new MyRow();
r.put("goods_from",rowItem.getString("goods_from"));
r.put("goods_url",rowItem.getString("goods_url"));
holder.fest_buy.setTag(r);
MyRow rCao = new MyRow();
rCao.put("id",rowItem.getInt("id"));
rCao.put("pos",position);
rCao.put("like_count",MathUtils.getInt(rowItem.get("like_count")+""));
rCao.put("dislike_count",MathUtils.getInt(rowItem.get("dislike_count")+""));
rCao.put("user_like_flag",MathUtils.getInt(rowItem.get("user_like_flag")+""));
rCao.put("user_dislike_flag",MathUtils.getInt(rowItem.get("user_dislike_flag")+""));
holder.ll_comment.setTag(rCao);
holder.zhong_cao.setTag(rCao);
holder.ba_cao.setTag(rCao);
holder.img_follow.setTag(rowItem.getInt("user_id"));
holder.user_name.setText(rowItem.getString("user_nickname"));
holder.layout_card.setRadius(5);
holder.ll_jubao.setTag(rowItem.getInt("id"));
MyRow r1 = new MyRow();
r1.put("pos", position);
r1.put("goods_from", rowItem.getString("goods_from"));
r1.put("goods_url", rowItem.getString("goods_url"));
holder.ll_get_link.setTag(r1);
rowItem.put("pos", position);
Glide.with(context.getApplicationContext()).asBitmap().load((rowItem.getString("goods_pic"))).into(holder.shop_img);
holder.shop_name.setText(rowItem.getString("goods_name"));
holder.shop_price.setText("¥"+rowItem.getString("goods_price"));
}
@Override
public int getItemCount() {
return null == videoData ? 0 : videoData.size();
}
static class MyViewHolder extends RecyclerView.ViewHolder {
JzvdStd jzvdStd;
CardView layout_card;
public CircleImageView userAvatar;
public ImageView img_follow;
public XCRoundRectImageView shop_img;
public TextView name,tv_font_zhong_cao,user_name, tv_font_ba_cao, tv_font_comment, video_des, zhong_cao_num, ba_cao_num, tv_font_sandian, comment_num, shop_name, shop_price, fest_buy;
public LinearLayout ll_get_link, ll_jubao, ll_shop_tab,zhong_cao,ba_cao,ll_comment;
public MyViewHolder(View itemView) {
super(itemView);
jzvdStd = itemView.findViewById(R.id.videoplayer);
layout_card= itemView.findViewById(R.id.layout_card);
// name = (TextView) itemView.findViewById(R.id.tv_video_name);
ll_get_link = itemView.findViewById(R.id.ll_get_link);
userAvatar = (CircleImageView) itemView.findViewById(R.id.head_icon);
img_follow = (ImageView) itemView.findViewById(R.id.img_follow);
tv_font_zhong_cao = itemView.findViewById(R.id.tv_font_zhong_cao);
tv_font_ba_cao = itemView.findViewById(R.id.tv_font_ba_cao);
// tv_font_get_link = itemView.findViewById(R.id.tv_font_get_link);
tv_font_comment = itemView.findViewById(R.id.tv_font_comment);
tv_font_sandian = itemView.findViewById(R.id.tv_font_sandian);
video_des = itemView.findViewById(R.id.video_des);
zhong_cao_num = itemView.findViewById(R.id.zhong_cao_num);
ba_cao_num = itemView.findViewById(R.id.ba_cao_num);
comment_num = itemView.findViewById(R.id.comment_num);
ll_jubao = itemView.findViewById(R.id.ll_jubao);
ll_shop_tab = itemView.findViewById(R.id.ll_shop_tab);
shop_name = (TextView) itemView.findViewById(R.id.shop_name);
shop_price = (TextView) itemView.findViewById(R.id.shop_price);
shop_img = itemView.findViewById(R.id.shop_img);
fest_buy = itemView.findViewById(R.id.fest_buy);
zhong_cao = itemView.findViewById(R.id.zhong_cao);
ba_cao= itemView.findViewById(R.id.ba_cao);
user_name = itemView.findViewById(R.id.user_name);
ll_comment= itemView.findViewById(R.id.ll_comment);
}
}
}
边栏推荐
- es-ik 安装报错
- How to make lamps intelligent? How to choose single fire and zero fire intelligent switches!
- Multithreading tutorial (XXV) atomic array
- Section II: structural composition characteristics of asphalt pavement (2) structural layer and performance requirements
- Section I: classification and classification of urban roads
- 推荐一款免费的内网穿透开源软件,可以在测试本地开发微信公众号使用
- 微信小程序text内置组件换行符不换行的原因-wxs处理换行符,正则加段首空格
- Flask develops and implements the like comment module of the online question and answer system
- [go deep into kotlin] - get to know flow for the first time
- Preliminary understanding of DFS and BFS
猜你喜欢

MySQL string to array, merge result set, and convert to array

If the MAC fails to connect with MySQL, it will start and report an error

Zed2 camera calibration -- binocular, IMU, joint calibration

在未来,机器人或 AI 还有多久才能具备人类的创造力?

微信自定义组件---样式--插槽

Multithreading tutorial (XXVII) CPU cache and pseudo sharing

Deep search + backtracking

自定义View之基础篇

如何让灯具智能化,单火、零火智能开关怎么选!

Customize the layout of view Foundation
随机推荐
C (I) C basic grammar all in one
Customize the layout of view Foundation
(15) Infrared communication
Recherche sur l'optimisation de Spark SQL basée sur CBO pour kangourou Cloud Stack
Zed2 running vins-mono preliminary test
Multi threading tutorial (XXIV) cas+volatile
27. Remove elements
About custom comparison methods of classes and custom methods of sort functions
MySQL string to array, merge result set, and convert to array
截取文件扩展名
22、生成括号
【入门级基础】Node基础知识总结
Recursively process data accumulation
Handle double quotation mark escape in JSON string
如何让灯具智能化,单火、零火智能开关怎么选!
[opencv learning problems] 1 Namedwindow() and imshow() show two windows in the picture
NDK learning notes (V)
If the MAC fails to connect with MySQL, it will start and report an error
Project - Smart City
[go deep into kotlin] - flow advanced