Android RecyclerView更新子项目notifyItemChanged
在过去Android的ListView中,如果ListView中某一个或者一批项目发生变化,需要通过adapter的notifyDataSetChanged更新数据到最新,但是从ListView演进到现在的RecyclerView,数据的更新不需要像ListView那样notifyDataSetChanged,而是要采用RecyclerView的notifyItemChanged,传入一个pos位置,通知RecyclerView该pos位置已经失效,需要重新绘制和UI更新。现在给出一个例子,上面一个Button按钮,下面一个RecyclerView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="随机更新" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
每次点击Button按钮,将随机更新某一个位置的时间到最新:
package zhangphil.demo;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Random;
public class MainActivity extends Activity {
private ArrayList data = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < 5; i++) {
data.add(System.currentTimeMillis());
}
RecyclerView mRecyclerView = findViewById(R.id.recycler_view);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setOrientation(LinearLayout.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);
final RecyclerView.Adapter mAdapter = new MyAdapter();
mRecyclerView.setAdapter(mAdapter);
final Random random = new Random();
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = random.nextInt(data.size());
data.set(pos, System.currentTimeMillis());
mAdapter.notifyItemChanged(pos);
Toast.makeText(getApplicationContext(), "更新:" + pos + "," + data.get(pos), Toast.LENGTH_LONG).show();
}
});
}
private class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
public MyAdapter() {
super();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(getApplicationContext()).inflate(android.R.layout.simple_list_item_2, null);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.text1.setText(i + "");
viewHolder.text2.setText(data.get(i) + "");
}
@Override
public int getItemCount() {
return data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView text1;
public TextView text2;
public ViewHolder(View itemView) {
super(itemView);
text1 = itemView.findViewById(android.R.id.text1);
text1.setTextColor(Color.RED);
text2 = itemView.findViewById(android.R.id.text2);
text2.setTextColor(Color.BLUE);
}
}
}
}
事实上RecyclerView的notifyItemChanged的底层调用的是notifyItemRangeChanged:
/**
* Notify any registered observers that the item at <code>position</code> has changed.
* Equivalent to calling <code>notifyItemChanged(position, null);</code>.
*
* <p>This is an item change event, not a structural change event. It indicates that any
* reflection of the data at <code>position</code> is out of date and should be updated.
* The item at <code>position</code> retains the same identity.</p>
*
* @param position Position of the item that has changed
*
* @see #notifyItemRangeChanged(int, int)
*/
public final void notifyItemChanged(int position) {
mObservable.notifyItemRangeChanged(position, 1);
}
该方法使得RecyclerView批量范围内(range)数据更新,notifyItemChanged巧妙的将第二个参数计数器设置为1得以实现。后续再介绍RecyclerView的notifyItemRangeChanged。