当前位置:网站首页>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);
}
}
}
边栏推荐
- 35.搜索插入位置
- Getbackgroundaudiomanager controls music playback (dynamic binding of class name)
- Opencv learning path (2-5) -- Deep parsing imwrite function
- NVIDIA SMI has failed because it could't communicate with the NVIDIA driver
- About custom comparison methods of classes and custom methods of sort functions
- getBackgroundAudioManager控制音乐播放(类名的动态绑定)
- code
- Multi thread tutorial (XXIX) immutable design
- 高斯白噪声(white Gaussian noise,WGN)
- [go deep into kotlin] - get to know flow for the first time
猜你喜欢

WinForm (II) advanced WinForm and use of complex controls

6 questions to ask when selecting a digital asset custodian

Analysis while experiment - a little optimization of memory leakage in kotlin

PCB走線到底能承載多大電流

袋鼠雲數棧基於CBO在Spark SQL優化上的探索

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

How much current can PCB wiring carry

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

Analyzing while experimenting - memory leakage caused by non static inner classes

Restoration of binary tree -- number restoration
随机推荐
工具类ObjectUtil常用的方法
Use acme SH automatically apply for a free SSL certificate
截取文件扩展名
Analyzing while experimenting - memory leakage caused by non static inner classes
Multi threading tutorial (XXIV) cas+volatile
NDK learning notes (V)
[entry level basics] node basic knowledge summary
Analysis while experiment - a little optimization of memory leakage in kotlin
NDK learning notes (VI) Basics: memory management, standard file i/o
Carrier coordinate system inertial coordinate system world coordinate system
【入门级基础】Node基础知识总结
Section IV: composition and materials of asphalt mixture (1) -- structure composition and classification
Maximum number of points on the line ----- hash table solution
Traversal of binary tree -- restoring binary tree by two different Traversals
Introduction to coordinate system in navigation system
SwiftUI: Navigation all know
Handle double quotation mark escape in JSON string
Analyze while doing experiments -ndk article -jni uses registernatives for explicit method registration
Opencv learning path (2-2) -- Deep parsing namedwindow function
2021-04-19