Android:如何在添加项目后返回到RecyclerView的顶部?

时间:2017-01-16 22:41:18

标签: android android-recyclerview recycler-adapter

我有一个CardViews的RecyclerView列表。新的CardViews插在列表的顶部。

如果视图当前位于CardView列表的底部,我单击我的"添加"按钮创建一个新的CardView,我希望RecyclerView然后在列表的顶部显示新的CardView。目前,当创建新的CardView时,RecyclerView只返回列表底部的上一个View。

我尝试过很多东西,没有运气,包括:

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

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

...    
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);

2 个答案:

答案 0 :(得分:2)

喜欢这个

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

如果这不起作用,请检查您在何处调用此控件并且控件是否执行此语句。

答案 1 :(得分:0)

我使用“滚动到顶部”按钮,您可以尝试使用它:

LinearLayoutManager mlayoutManager = (LinearLayoutManager) rv.getLayoutManager();
    mlayoutManager.scrollToPositionWithOffset(0, 0);