RecyclerView没有保持状态

时间:2017-12-13 23:21:28

标签: android android-fragments android-recyclerview

我正在使用Recycler View来加载片段上的项目列表。可以使用application/octet-stream替换此片段。

当我更改片段,然后返回到此片段时,将按预期维护Recycler View卷轴;这是Bottom Bar Navigation方法的代码:

onCreteView()

这里,重要的部分是@Override public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View cl = inflater.inflate(R.layout.fragment_contents, container, false); contentList = (RecyclerView) cl.findViewById(R.id.contentList); RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext()); contentList.setLayoutManager(mLayoutManager); contentList.setItemAnimator(new DefaultItemAnimator()); contentList.addItemDecoration(new DividerItemDecoration(getActivity().getApplicationContext(), LinearLayoutManager.VERTICAL)); SharedPreferences sharedPref = getActivity().getSharedPreferences(MainActivity.PACKAGE_NAME, Context.MODE_PRIVATE); boolean contentsLoaded = sharedPref.getBoolean("contentsLoaded", false); swipeRefreshLayout = (SwipeRefreshLayout) cl.findViewById(R.id.contents_swipe); swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { content_list.getContents().clear(); loadContents(getActivity().getApplicationContext()); } }); contentList.addOnItemTouchListener( new ChannelRecyclerItemClickListener(getActivity().getApplicationContext(), contentList ,new ChannelRecyclerItemClickListener.OnItemClickListener() { @Override public void onItemClick(View view, int position) { Intent contentActivity = new Intent(getActivity().getApplicationContext(), ContentActivity.class ); Content content = (Content) view.getTag(); Channel channel = content_list.findChannelById(content.getChannelId()); Town town = content_list.findTownById(content.getTownId()); User author = content_list.findUserById(content.getAuthorId()); contentActivity.putExtra("id", content.getId()); contentActivity.putExtra("title", content.getTitle()); contentActivity.putExtra("lastEdit", new SimpleDateFormat("dd/MM/yy").format(content.getLastEdit())); contentActivity.putExtra("description", content.getDescription()); contentActivity.putExtra("channelName", channel.getName()); contentActivity.putExtra("channelPic", channel.getPicture()); contentActivity.putExtra("townName", town.getName()); contentActivity.putExtra("townPic", town.getPicture()); contentActivity.putExtra("authorName", author.getName()); contentActivity.putExtra("authorSurname", author.getSurname()); startActivity(contentActivity); } @Override public void onLongItemClick(View view, int position) { // do whatever } }) ); if(contentsLoaded) { LoadFromCache(); return cl; } loadContents(getActivity().getApplicationContext()); SharedPreferences.Editor editor = sharedPref.edit(); editor.putBoolean("contentsLoaded", true); editor.apply(); return cl; } 调用,它从数据库中检索项目并重新加载RecyclerView。

当我将此代码放在我的LoadFromCache()方法上时出现问题:

onResume()

然后,当我在片段之间切换时,滚动被重置为顶部。我无法理解为什么 @Override public void onResume() { super.onResume(); LoadFromCache(); } 方法工作正常,而不是我的onCreateView()方法,因为它是同一个调用。

更多信息:

LoadFromCache()函数:

onResume()

LoadContentsFromDatabase类:

public void LoadFromCache(Void...voids){
    new LoadContentsFromDatabase(contentList).execute();
}

编辑:我如何在片段之间切换

我的家庭活动负责管理片段切换:

public class LoadContentsFromDatabase extends AsyncTask<Void, Void, ContentList> {

@SuppressLint("StaticFieldLeak")
RecyclerView contentList;

public LoadContentsFromDatabase(RecyclerView contentList) {
    this.contentList = contentList;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();

    //Perform pre-adding operation here.
}

@Override
protected ContentList doInBackground(Void...voids) {

    //Select data from DB
    List<Town> towns = db.townDao().getAll();
    List<Channel> channels = db.channelDao().getAll();
    List<User> users = db.userDao().getAll();
    List<Content> contents = db.contentDao().getAll();

    return new ContentList(new ArrayList(contents), new ArrayList(towns), new ArrayList(channels), new ArrayList(users), null);

}

@Override
protected void onPostExecute(ContentList list) {
    super.onPostExecute(list);

    //To after addition operation here.

    contentAdapter = new ContentAdapter(list);
    contentList.setItemAnimator(new DefaultItemAnimator());
    contentList.setAdapter(contentAdapter);
    contentAdapter.notifyDataSetChanged();

    ContentsFragment.content_list = list;

}
}

0 个答案:

没有答案