本文共 3894 字,大约阅读时间需要 12 分钟。
/** * Notify any registered observers that the示例代码:itemCount
items starting at * positionpositionStart
have changed. * Equivalent to callingnotifyItemRangeChanged(position, itemCount, null);
. * *This is an item change event, not a structural change event. It indicates that * any reflection of the data in the given position range is out of date and should * be updated. The items in the given range retain the same identity.
* * @param positionStart Position of the first item that has changed * @param itemCount Number of items that have changed * * @see #notifyItemChanged(int) */ public final void notifyItemRangeChanged(int positionStart, int itemCount) { mObservable.notifyItemRangeChanged(positionStart, itemCount); }
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 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 count = random.nextInt(data.size()); for (int i = 0; i < count; i++) { data.set(i, System.currentTimeMillis()); } mAdapter.notifyItemRangeChanged(0, count); } }); } private class MyAdapter extends RecyclerView.Adapter附录: 1,《Android RecyclerView更新子项目notifyItemChanged》链接:http://blog.csdn.net/zhangphil/article/details/78565738{ 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); } } }}