【问题标题】:How to add newly added data from firebase to the top of the recyclerview and not at the bottom? Please see details如何将新添加的数据从firebase添加到recyclerview的顶部而不是底部?请查看详情
【发布时间】:2016-10-27 18:56:46
【问题描述】:

我正在开发一个应用程序,我正在从Firebase 数据库中检索一些数据。

问题是每次我保存一些东西时,它都会被检索并添加到RecyclerView 的底部。

我想要的是我希望新添加的数据被添加到 recyclerview 的顶部而不是底部。

我不知道该怎么做。

请告诉我。

【问题讨论】:

    标签: android firebase android-recyclerview firebase-realtime-database


    【解决方案1】:

    首先问题是关于recyclerview 并且您发布了数据检索的代码。如果您想向回收站视图添加新项目,请使用

    mArrayList.add(position, item);
    notifyItemInserted(position); 
    

    对于要插入第一行的数据,使用

    mArrayList.add(0, item);
    notifyItemInserted(0); 
    

    然后您可以使用mRecyclerView.smoothScrollToPosition(0); 滚动到顶部。

    【讨论】:

    【解决方案2】:

    尝试类似的东西

    Query lastToFirstQuery = databaseReference.orderBy("something_in_your_database").limitToLast(datasnapshot.numChildren());
    
    lastToFirstQuery.addChildEventListener(new ChildEventListener() { 
            @Override 
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                Map<String, String> newRequest = (Map<String, String>) dataSnapshot.getValue();
                imageUID = newRequest.get("imageUIDh");
                hDescription = newRequest.get("hDescription");
                currentLat = newRequest.get("currentLat");
                currentLng = newRequest.get("currentLng");
                postedBy = newRequest.get("postedBy");
                postedAtTime = newRequest.get("postedAtTime");
                postedOnDate = newRequest.get("postedOnDate");
                utcFormatDateTime = newRequest.get("utcFormatDateTime");
    
            } 
    
            @Override 
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
    
            } 
    
            @Override 
            public void onChildRemoved(DataSnapshot dataSnapshot) {
    
            } 
    
            @Override 
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {
    
            } 
    
            @Override 
            public void onCancelled(DatabaseError databaseError) {
                Snackbar snackbar = Snackbar
                        .make(coordinatorLayout, databaseError.getMessage(), Snackbar.LENGTH_LONG);
                snackbar.setDuration(Snackbar.LENGTH_SHORT);
                snackbar.show();
                progressBarLoading.setVisibility(View.INVISIBLE);
            } 
        }); 
    

    【讨论】:

    • M. Waqas的回答解决了这个问题。也谢谢你的回答。你能不能也看看这个:stackoverflow.com/questions/38023511/...拜托?..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    • 2016-12-15
    • 1970-01-01
    • 2017-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多