Android:如何返回到新创建的RecyclerView列表? [英] Android: How do I return to newly created RecyclerView list?

查看:97
本文介绍了Android:如何返回到新创建的RecyclerView列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用默认布局创建一个RecyclerView列表.然后,我将一个新项目添加到列表中,并且布局更新以显示新项目.然后,我导航到上一个活动.当我返回到RecyclerView活动时,我将返回到通用的默认列表,并且RecyclerView列表中的新项目消失了.

I create a RecyclerView list with a default layout. I then add one new item to the list and the layout updates to show the new item. I then navigate to a previous activity. When I return to the RecyclerView activity I am returned to the generic, default list and my new item in the RecyclerView list is gone.

那么,如何返回RecyclerView和我创建的新项目,而不是常规列表?我是否需要添加一些代码,说明适配器的大小> 0,然后不使用现有列表创建新列表?我应该在RecyclerView活动中还是在适配器中进行测试?如果不是,是否会因为适配器与RecyclerView活动的saveInstanceState无关而出现问题?

So how do I return to the RecyclerView and the new item that I created rather than the generic list? Do I need to add some code that says if the size of the adapter is > 0 then don't create a new list use the existing one? And should I be doing the test in the RecyclerView activity or in the adapter? If not, does my issue arise because the adapter is not related somehow to the savedInstanceState of the RecyclerView activity?

活动:

public class ListContactsActivity extends AppCompatActivity {

private ListContactsAdapter mContactsAdapter;
private RecyclerView mRecyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_recyclerviewlist);

    final List<Contact> mContacts;
    mContacts = new ArrayList<>();

    mRecyclerView = (RecyclerView) findViewById(R.id.cardList);
    mRecyclerView.setLayoutManager(getLayoutManager());
    mRecyclerView.setHasFixedSize(true);
    mContactsAdapter = new ListContactsAdapter(this, mContacts);

    mRecyclerView.setAdapter(mContactsAdapter);
    ...
}

private RecyclerView.LayoutManager getLayoutManager() {
    LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    return llm;
} 

适配器:

class ListContactsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
...

@Override
public int getItemCount() {
    return mItems.size();
} 

活动中的工具栏代码:

...
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationIcon(R.drawable.ic_action_previous_item);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        onBackPressed();
    }
});    

推荐答案

从活动中回击时,您正在从堆栈中弹出该活动,即活动对象将被销毁. 此处对此进行了更详细的说明.从这张图片可以理解这个概念的症结所在.

When you hit back from an activity you are popping it from the stack i.e the activity object will be destroyed. This is explained in further detail here. The crux of the concept can be understood from this picture.

您可以查看,以了解您的情况应该重新创建您的活动-并,以了解如何将其应用于recyclerview.

You can look at this to see how you should recreate your activity - and this to see how you should apply that to a recyclerview.

这篇关于Android:如何返回到新创建的RecyclerView列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆