需要一个关于RecyclerView.Adapter.notifyItemChanged(int position,Object payload)的例子

首先,我的英语很抱歉。 根据RecyclerView有关方法notifyItemChanged(int position,Object payload)的文档,

通知任何注册的观察者,位置的项目已经改变了可选的有效载荷对象。

我不明白如何在这个方法中使用第二个参数payload 。 我搜查了许多有关“有效载荷”的文件,但一切都是模棱两可的。

所以,如果你知道这个方法,请给我一个清晰的例子。 非常感谢你。

Related of "需要一个关于RecyclerView.Adapter.notifyItemChanged(int position,Object payload)的例子"

查看演示该function的示例代码。 当点击位置上的项目时notifyItemChanged(position, payload)它是一个RecyclerView ,它调用notifyItemChanged(position, payload) 。 你可以通过查找logcat语句来validationonBindViewHolder(holder, position, payload)被调用。

确保至less使用版本23.1.1的支持库,如下所示:

 dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:recyclerview-v7:23.1.1' compile 'com.android.support:cardview-v7:23.1.1' } 

HelloActivity.java

 package com.formagrid.hellotest; import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.CardView; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.List; public class HelloActivity extends Activity { private RecyclerView mRecyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)); mRecyclerView.setAdapter(new HelloAdapter()); DefaultItemAnimator animator = new DefaultItemAnimator() { @Override public boolean canReuseUpdatedViewHolder(RecyclerView.ViewHolder viewHolder) { return true; } }; mRecyclerView.setItemAnimator(animator); } private static class HelloAdapter extends RecyclerView.Adapter<HelloAdapter.HelloViewHolder> { public class HelloViewHolder extends RecyclerView.ViewHolder { public TextView textView; public HelloViewHolder(CardView cardView) { super(cardView); textView = (TextView) cardView.findViewById(R.id.text_view); } } @Override public HelloViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { CardView cardView = (CardView) LayoutInflater.from(parent.getContext()).inflate( R.layout.card_item, parent, false); return new HelloViewHolder(cardView); } @Override public void onBindViewHolder(HelloViewHolder holder, int position) { bind(holder); } @Override public void onBindViewHolder(HelloViewHolder holder, int position, List<Object> payload) { Log.d("butt", "payload " + payload.toString()); bind(holder); } @Override public int getItemCount() { return 20; } private void bind(final HelloViewHolder holder) { holder.textView.setText("item " + holder.getAdapterPosition()); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final int position = holder.getAdapterPosition(); Log.d("butt", "click " + position); HelloAdapter.this.notifyItemChanged(position, "payload " + position); } }); } } } 

activity_main.xml中

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".HelloActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout> 

card_item.xml

 <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="100dip" android:layout_margin="5dip" card_view:cardElevation="5dip"> <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="match_parent"/> </android.support.v7.widget.CardView> 

如果你想更新不是所有的持有者视图,但只是其中的一部分 ,这种方法是你所需要的。

您有以下ViewHolder的图像

 public class ViewHolder extends RecyclerView.ViewHolder { public final TextView tvName; public final TextView tvScore; public ViewHolder(View view) { super(view); tvName = (TextView) view.findViewById(R.id.tv_name); tvScore = (TextView) view.findViewById(R.id.tv_score); } } 

在代码中的某个地方,您可以调用适配器来更新单个 TextView – tvScore

 mRecyclerViewAdapter.notifyItemChanged(position, new Integer(4533)); 

[…]

onBindViewHolder(ViewHolder持有者,int位置,List有效载荷)首先捕获callback。 如果payloads不符合您的要求,您必须强制调用超类super.onBindViewHolder(holder,position, payloads); 在其他情况下触发onBindViewHolder(ViewHolder holder, int position)

  // Update only part of ViewHolder that you are interested in // Invoked before onBindViewHolder(ViewHolder holder, int position) @Override public void onBindViewHolder(ViewHolder holder, int position, List<Object> payloads) { if(!payloads.isEmpty()) { if (payloads.get(0) instanceof Integer) { holder.tvScore.setText(String.valueOf((Integer)payloads.get(0))) } }else { super.onBindViewHolder(holder,position, payloads); } } // Update ALL VIEW holder @Override public void onBindViewHolder(ViewHolder holder, int position) { MItem item = mList.get(position) // some update } 

当您只想在调用onBindViewHolder方法时对onBindViewHolder的数据执行部分更新时,您可以传入“可选的有效内容对象”。 但是,不能确定有效负载将始终被传递,例如,如果视图未附加,则应执行一些更多的检查。

无论如何,如果你通过null ,它将被执行一个完整的更新项目,你将不必担心它。