在 RecyclerView 中更新数据是如何工作的?
How updating data inside RecyclerView works?
众所周知java是传值调用,这里我们传递一个LinkedList给Adapterclass。像这样
// this snippet is belongs to mainActivity
private final LinkedList<String> mWordList = new LinkedList<>();
createDataSet(); // Let's create a data set by calling this method
mRecyclerView = findViewById(R.id.recycler_view);
// lets create an adapter
mWordListAdapter = new WordListAdapter(this,mWordList);
mRecyclerView.setAdapter(mWordListAdapter); // connect the adapter to the view
mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); // pass a layout manager
并且适配器 class 拥有自己的 linkedList,它获取我们刚刚从 MainActivity 传递的 linkedList 的数据。
// Adapter class
public class WordListAdapter extends RecyclerView.Adapter<WordListAdapter.WordViewHolder> {
private LinkedList<String> mwordList;
private LayoutInflater mInflater;
// get the data from mainActivity and the layout inflator
public WordListAdapter(Context context, LinkedList<String> wordList) {
mInflater = LayoutInflater.from(context);
mwordList = wordList;
}
// only showing up the needed snippets
}
并设置一个 onclicklistner 以在单击此类项目时修改列表
@Override
public void onClick(View v) {
// get the position of the item that was clicked
int mPosition = getLayoutPosition();
// use this position to get the affected item in the wordlist
String element = mwordList.get(mPosition);
// change the data in the wordList
mwordList.set(mPosition,"cicked!"+element);
// notify the adapter that the data has been chagned
// so recyclerVeiw can update it
mWordListAdapter.notifyDataSetChanged();
}
并且在 MainActivity 中还有一个用于插入新数据的 fab 按钮
binding.fab.setOnClickListener(new View.OnClickListener() {
@Override // adding new word to the lsit
public void onClick(View view) {
int wordListSize = mWordList.size() + 1;
mWordList.add("+"+"word"+wordListSize); // adding a new string element
mRecyclerView.getAdapter().notifyItemInserted(wordListSize); // notify dataset changed
mRecyclerView.smoothScrollToPosition(wordListSize); // scroll screen where we added new data
}
现在我的问题是在不将更新的 LinkedList 传递给适配器 class 的情况下,这些数据是如何变化的,因为我们只在 notifyItemIntserted 中传递更新列表的大小.
我想知道后台进程,因为它在工作,这意味着有些东西超出了我的理解范围。
关于参考。您的适配器和您的 Activity 具有相同的 ArrayList
引用,因此如果您修改它,它将反映在两者中。
public WordListAdapter(Context context, LinkedList<String> wordList) {
mInflater = LayoutInflater.from(context);
mwordList = wordList;
}
使用这段代码 mwordList = wordList;
,您将同一个对象分配给新的引用变量 mwordList
。引用变量不同,但它们指向的对象是相同的,即您在 main Activity.
中创建的对象
对于其他部分,如果你像下面那样做,这只是为了解释区别。
public WordListAdapter(Context context, LinkedList<String> wordList) {
mInflater = LayoutInflater.from(context);
mwordList = new LinkedList<>(wordList);
}
这不会反映您在 Activity 列表中所做的更改,原因是现在两者都指向不同的对象。我们使用 new
关键字为 mwordList
创建一个新对象。我希望这是有道理的。
众所周知java是传值调用,这里我们传递一个LinkedList给Adapterclass。像这样
// this snippet is belongs to mainActivity
private final LinkedList<String> mWordList = new LinkedList<>();
createDataSet(); // Let's create a data set by calling this method
mRecyclerView = findViewById(R.id.recycler_view);
// lets create an adapter
mWordListAdapter = new WordListAdapter(this,mWordList);
mRecyclerView.setAdapter(mWordListAdapter); // connect the adapter to the view
mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); // pass a layout manager
并且适配器 class 拥有自己的 linkedList,它获取我们刚刚从 MainActivity 传递的 linkedList 的数据。
// Adapter class
public class WordListAdapter extends RecyclerView.Adapter<WordListAdapter.WordViewHolder> {
private LinkedList<String> mwordList;
private LayoutInflater mInflater;
// get the data from mainActivity and the layout inflator
public WordListAdapter(Context context, LinkedList<String> wordList) {
mInflater = LayoutInflater.from(context);
mwordList = wordList;
}
// only showing up the needed snippets
}
并设置一个 onclicklistner 以在单击此类项目时修改列表
@Override
public void onClick(View v) {
// get the position of the item that was clicked
int mPosition = getLayoutPosition();
// use this position to get the affected item in the wordlist
String element = mwordList.get(mPosition);
// change the data in the wordList
mwordList.set(mPosition,"cicked!"+element);
// notify the adapter that the data has been chagned
// so recyclerVeiw can update it
mWordListAdapter.notifyDataSetChanged();
}
并且在 MainActivity 中还有一个用于插入新数据的 fab 按钮
binding.fab.setOnClickListener(new View.OnClickListener() {
@Override // adding new word to the lsit
public void onClick(View view) {
int wordListSize = mWordList.size() + 1;
mWordList.add("+"+"word"+wordListSize); // adding a new string element
mRecyclerView.getAdapter().notifyItemInserted(wordListSize); // notify dataset changed
mRecyclerView.smoothScrollToPosition(wordListSize); // scroll screen where we added new data
}
现在我的问题是在不将更新的 LinkedList 传递给适配器 class 的情况下,这些数据是如何变化的,因为我们只在 notifyItemIntserted 中传递更新列表的大小. 我想知道后台进程,因为它在工作,这意味着有些东西超出了我的理解范围。
关于参考。您的适配器和您的 Activity 具有相同的 ArrayList
引用,因此如果您修改它,它将反映在两者中。
public WordListAdapter(Context context, LinkedList<String> wordList) {
mInflater = LayoutInflater.from(context);
mwordList = wordList;
}
使用这段代码 mwordList = wordList;
,您将同一个对象分配给新的引用变量 mwordList
。引用变量不同,但它们指向的对象是相同的,即您在 main Activity.
对于其他部分,如果你像下面那样做,这只是为了解释区别。
public WordListAdapter(Context context, LinkedList<String> wordList) {
mInflater = LayoutInflater.from(context);
mwordList = new LinkedList<>(wordList);
}
这不会反映您在 Activity 列表中所做的更改,原因是现在两者都指向不同的对象。我们使用 new
关键字为 mwordList
创建一个新对象。我希望这是有道理的。