Java 每次重新创建滚动位置后,如何将其保存在回收器视图中?

Java 每次重新创建滚动位置后,如何将其保存在回收器视图中?,java,android,android-recyclerview,android-listview,Java,Android,Android Recyclerview,Android Listview,下面是每次单击按钮并创建包含新数据的列表时调用的方法 public void fire(View v) { String temp = editText.getText().toString(); myRecyclerView = new MyRecyclerView(this, 500, temp); recyclerView.setAdapter(myRecyclerView); recyclerView.setLayoutManager(new Linea

下面是每次单击按钮并创建包含新数据的列表时调用的方法

public void fire(View v) {
    String temp = editText.getText().toString();

    myRecyclerView = new MyRecyclerView(this, 500, temp);
    recyclerView.setAdapter(myRecyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    myRecyclerView.setClickListener(this);
} 

如何保存滚动位置,以便下次单击按钮时,列表仍保持在同一位置

好的,每次重新创建时都使用它

linearLayoutManager.scrollToPositionWithOffset(pos, 0);     
代码:


为什么每次都要创建一个新视图?这里的用例是什么?我正在制作一个应用程序,用户可以在其中输入任何文本,并将其转换为不同的字体,这就是为什么我必须重新创建整个列表的原因。这样,您就不必每次都创建一个新的视图。更改数据集应该足够了。我该怎么做?谢谢,我明白你的意思,但是你能告诉我每次单击按钮时如何获取pos值吗?我尝试在适配器类中声明一个方法,并尝试在其中调用getAdapterPosition(),但我想我不能在那里使用此方法,你有什么想法吗?请使用“循环视图适配器”在创建位置时获取该位置。谢谢,我做到了。
public void fire(View v) {
    String temp = editText.getText().toString();

    myRecyclerView = new MyRecyclerView(this, 500, temp);
    recyclerView.setAdapter(myRecyclerView);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(linearLayoutManager);
    myRecyclerView.setClickListener(this);
    //assuming th layout is loaded. set pos as the position you want to scroll to
    int pos=0;
    linearLayoutManager.scrollToPositionWithOffset(pos, 0); 

}