Android RecyclerView更新子項目notifyItemChanged
在過去Android的ListView中,如果ListView中某一個或者一批項目發生變化,需要通過adapter的notifyDataSetChanged更新數據到最新,但是從ListView演進到現在的RecyclerView,數據的更新不需要像ListView那樣notifyDataSetChanged,而是要采用RecyclerView的notifyItemChanged,傳入一個pos位置,通知RecyclerView該pos位置已經失效,需要重新繪制和UI更新。現在給出一個例子,上面一個Button按鈕,下面一個RecyclerView:
每次點擊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 { 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 position has changed.
* Equivalent to calling notifyItemChanged(position, null);.
*
*
This is an item change event, not a structural change event. It indicates that any * reflection of the data at
position is out of date and should be updated. * The item at
position retains the same identity.
* * @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。