如何将新物品放在recyclerView的顶部? [英] How do I place new items at the top of a recyclerView?

查看:109
本文介绍了如何将新物品放在recyclerView的顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将内容添加到我的数据库( Firebase )中,这些内容应该被加载到我的recyclerview中.但是新内容总是出现在recyclerview的底部.有什么方法可以使它们出现在顶部而不是出现在底部

I'm adding content to my database (Firebase) which are suppose to be loaded into my recyclerview. But the new content always appear at the bottom of the recyclerview. Is there any way to make them appear at the top rather than appear at the bottom

推荐答案

我终于解决了我需要做的所有事情,就是将一个整数变量初始化为0,并将其与要添加的模型类的对象一起添加从Firebase数据库中获取,最后通知插入适配器的项目.代码会更好地说明

I finally solved the issue all i needed to do was to initialize an integer variable as 0,add it along side with the object of the model class that is to be gotten from the Firebase database and finally notifying an item inserted to the adapter.The code will explain better

int position = 0;
ArrayList<Places> data = new ArrayList<>();
LinearLayoutManager lg = new LinearLayoutManager(this);

然后最后的代码是这个

valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Places content = ds.getValue(Places.class);
    data.add(position,content);
adapter = new YourAdapter(data, getApplicationContext);
      recyclerview.setLayoutManager(new Line);
      recyclerview.setAdapter(adapter);
      adapter.notifyItemInserted(position);
       adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
     // Handle error code here
   }
     });

这篇关于如何将新物品放在recyclerView的顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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