Android:添加项目后如何返回到RecyclerView的顶部? [英] Android: how do I return to to top of RecyclerView after adding item?

查看:228
本文介绍了Android:添加项目后如何返回到RecyclerView的顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有CardViews的RecyclerView列表.新的CardView将插入到列表的顶部.

I have a RecyclerView list of CardViews. New CardViews are inserted at the top of the list.

如果视图"当前位于CardView列表的底部,并且单击添加"按钮以创建新的CardView,则希望RecyclerView在列表顶部显示新的CardView.当前,当创建新的CardView时,RecyclerView只是返回到列表底部的上一个View.

If the View is currently at the bottom of the CardView list and I click my "Add" button to create a new CardView, I want the RecyclerView to then show the new CardView at the top of the list. Currently, when the new CardView is created the RecyclerView just returns to the previous View at the bottom of the list.

我已经尝试了很多没有运气的事情,包括:

I've tried many things, without any luck, including:

recyclerView.getLayoutManager().scrollToPosition(0); 
recyclerView.scrollToPosition(0);
linearlayoutmanager.scrollToPositionWithOffset(0,0)

Adapter.java

Adapter.java

public void add(Contact item) {
    if (contactList.size()==0) {
        contactList.clear();
        notifyDataSetChanged();
    }
    contactList.add(item);
    notifyItemInserted(0);
}

public void addAll(List<Contact> contactList) {
    for (Contact contact : contactList) {
        add(contact);
    }
}  

Activity.java

Activity.java

...    
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    EmptyRecyclerView recyclerView = (EmptyRecyclerView)findViewById(R.id.list_recyclerview);
    recyclerView.setHasFixedSize(true);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(layoutManager);               
    recyclerView.setEmptyView(findViewById(R.id.empty_view));
    contactListAdapter = new ContactListAdapter(this);
    contactListAdapter.setOnItemClickListener(this);
    recyclerView.setAdapter(contactListAdapter);        

@Override
protected void onStart() {
    super.onStart();
    loadData();
}

void loadData(){  
    List<Contact> contactList = new ArrayList<>();
    Cursor cursor = sqLiteDB.retrieve();
    Contact contact;

    try {
        if (cursor.getCount() > 0) { 
            cursor.moveToFirst(); 
            while (!cursor.isAfterLast()) { 
                do {
                    contact = new Contact();
                    contact.setI(cursor.getInt(0));
                    contact.setC(cursor.getInt(1));
                    contact.setT(cursor.getString(2));
                    contact.setN1(cursor.getString(3));
                    contact.setN2(cursor.getString(4));
                    contact.setD(cursor.getString(5));
                    contact.setDt(cursor.getString(6));
                    contact.setTm(cursor.getLong(7));
                    // add the item to the top of the List.
                    contactList.add(0, contact);
                } while (cursor.moveToNext());
            }
        }
    } finally {
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
    }
    contactListAdapter.clear();
    contactListAdapter.addAll(contactList);

推荐答案

做到这一点

recyclerview.post(new Runnable() {
        @Override
        public void run() {
           LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
           layoutManager.scrollToPositionWithOffset(0, 0);
        }
    });

如果这不起作用,请检查您在哪里调用此控件,然后控制是否执行该语句.

if this does not work check where are you calling this and control executes this statement or not.

这篇关于Android:添加项目后如何返回到RecyclerView的顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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