根据有关medthod notifyItemChanged(int position, Object payload)
的RecyclerView
文档
通知任何已注册的观察员,该位置的项目已更改为可选的有效负载对象。
我不明白如何在此方法中使用第二个参数payload
。我搜索过很多关于"有效载荷"的文件。但是一切都很暧昧。
所以,如果你知道这个方法,请给我一个明确的例子。非常感谢你。
答案 0 :(得分:52)
如果您想更新并非所有持有人视图,只是部分,则可以使用此方法。
您在 ViewHolder
之后的图片public class ViewHolder extends RecyclerView.ViewHolder {
public final TextView tvPlayer;
public final TextView tvScore;
public ViewHolder(View view) {
super(view);
tvPlayer = (TextView) view.findViewById(R.id.tv_player);
tvScore = (TextView) view.findViewById(R.id.tv_score);
}
}
在代码中的某处,您调用适配器来更新单 TextView - tvScore
mRecyclerViewAdapter.notifyItemChanged(position, new Integer(4533));
[...]
onBindViewHolder(ViewHolder holder,int position,List payloads) 首先捕获回调。
如果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
}
答案 1 :(得分:28)
查看演示该功能的示例代码。当点击位置RecyclerView
的项目时,notifyItemChanged(position, payload)
会调用position
。您可以通过查找logcat语句来验证是否已调用onBindViewHolder(holder, position, payload)
。
确保至少使用支持库的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>
答案 2 :(得分:2)
基本上,将有效载荷视为常数会更方便 如果数据集中的数据已经更改,则不必传递任何数据 因此,创建几个常量:
public static final String PAYLOAD_NAME = "PAYLOAD_NAME";
public static final String PAYLOAD_AGE = "PAYLOAD_AGE";
然后在适配器中,除了您更新整个项目的常规onBindViewHolder(YourViweHolder holder, int position)
之外,例如:
@Override
public void onBindViewHolder(final YourViewHolder holder, final int position) {
final YourItem item = getItem(position);
holder.tvName.setText(item.getName());
holder.tvAge.setText(String.valueOf(item.getAge()));
}
您还实施:
@Override
public void onBindViewHolder(final YourViewHolder holder, final int position, final List<Object> payloads) {
if (!payloads.isEmpty()) {
final YourItem item = getItem(position);
for (final Object payload : payloads) {
if (payload.equals(PAYLOAD_NAME)) {
// in this case only name will be updated
holder.tvName.setText(item.getName());
} else if (payload.equals(PAYLOAD_AGE)) {
// only age will be updated
holder.tvAge.setText(String.valueOf(item.getAge()));
}
}
} else {
// in this case regular onBindViewHolder will be called
super.onBindViewHolder(holder, position, payloads);
}
}
您可以根据需要处理尽可能多的案例,并仅更新实际更改的视图 最后一步 - 您只需:
adapter.notifyItemChanged(somePosition, YourAdapter.PAYLOAD_NAME);
答案 3 :(得分:1)
您可以传入&#34;可选的有效负载对象&#34;当您只想在调用ViewHolder
方法时对onBindViewHolder
中的数据进行部分更新时。但是,它不确定是否总是传递有效负载,例如,如果未附加视图,那么您应该执行更多检查。
无论如何,如果你通过null
,它将对该项目进行全面更新,你不必担心它。