Android - FirebaseListAdapter 以相反的顺序? [英] Android - FirebaseListAdapter in reverse order?

查看:35
本文介绍了Android - FirebaseListAdapter 以相反的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个 FirebaseListAdapter,它用最后 5 个项目填充 ListView:

I currently have a FirebaseListAdapter that populates a ListView with the last 5 items:

@Override
public void onViewCreated(View view, Bundle savedInstanceState){

    FirebaseListAdapter<HashMap> adapter = new FirebaseListAdapter<HashMap>(getParentFragment().getActivity(),HashMap.class,R.layout.list_item,firebase.limitToLast(5)) {
        @Override
        protected void populateView(View view, HashMap hashMap, int i) {
            String title = hashMap.get("title").toString();
            String body = hashMap.get("body").toString();

            TextView title_txt = (TextView)view.findViewById(R.id.title);
            TextView body_txt = (TextView)view.findViewById(R.id.body);

            title_txt.setText(title);
            body_txt.setText(body);
        }
    };

    listView.setAdapter(adapter);
}

我遇到的问题是,当一个新项目被推送到 Firebase 时,它​​会被添加到列表的底部.我想要列表顶部的最新项目.

The problem I have is that when a new item is pushed to Firebase, it is added to the bottom of the list. I want the newest items at the top of the list.

有可能实现吗?

非常感谢任何帮助,谢谢!

Any help is greatly appreciated, thanks!

推荐答案

这不是以相反顺序从 firebase 获取数据问题的确切解决方案,但无论如何,我们还有其他解决方法.

This is not the exact solution of your problem of getting the data from firebase in a reverse order, but anyway, we've other work arounds.

在为您的列表使用自定义适配器的评论中提到了第一个解决方法.

The first work around is mentioned in a comment of using a custom adapter for your list.

要实现您需要在列表中获取 firebase 数据的行为,然后您必须在将其传递给适配器之前自己将其反转.简单的!

To achieve the behaviour you need to get the firebase data in a list and then you've to reverse it yourself before passing it to an adapter. Simple!

第二个解决方法很简单,我认为如果您使用 RecyclerView,它可以为您解决问题,我认为这是您完成工作的最简单方法.

The second work around is easy as pie and I think if you're using RecyclerView it could do the trick for you and I see it as the simplest way you can do the job.

这是我用 RecyclerView

// Declare the RecyclerView and the LinearLayoutManager first
private RecyclerView listView;
private LinearLayoutManager mLayoutManager;

...

@Override
public void onViewCreated(View view, Bundle savedInstanceState){

    // Use FirebaseRecyclerAdapter here

    // Here you modify your LinearLayoutManager
    mLayoutManager = new LinearLayoutManager(MainActivity.this);
    mLayoutManager.setReverseLayout(true);
    mLayoutManager.setStackFromEnd(true);

    // Now set the layout manager and the adapter to the RecyclerView
    listView.setLayoutManager(mLayoutManager);
    listView.setAdapter(adapter);
}

通过设置 mLayoutManager.setReverseLayout(true); - 您正在反转布局并且 mLayoutManager.setStackFromEnd(true); 将视图定位到列表的顶部.

By setting mLayoutManager.setReverseLayout(true); - you're reversing your layout and mLayoutManager.setStackFromEnd(true); positions the view to the top of your list.

迁移到 RecyclerView 很简单.您的布局将是这样的

Migrating to RecyclerView is simple. Your layout will be something like this

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

在你的 build.gradle

dependencies {
    compile 'com.android.support:recyclerview-v7:23.4.0'
}

您需要在 FirebaseUI 库中找到 FirebaseRecyclerAdapter.

You need to have FirebaseRecyclerAdapter can be found here in FirebaseUI library.

注意:不要使用RecyclerView.LayoutManager,因为setReverseLayoutsetStackFromEnd 函数不会被找到在 RecyclerView.LayoutManager 中.如上所述使用 LinearLayoutManager.

Note: Do not use RecyclerView.LayoutManager as the setReverseLayout and setStackFromEnd functions won't be found in RecyclerView.LayoutManager. Use LinearLayoutManager as stated.

更新

这里介绍了如何处理列表中项目的点击事件.

Here's how you can handle the click events of your items in the list.

你必须声明一个 ViewHolder 来实现 RecyclerView 对吧?只需在您的 ViewHolder 类中添加另一个函数,如下例所示,并在您获得的 setText 函数之后调用该函数.

You had to declare a ViewHolder to implement the RecyclerView right? Just add another function inside your ViewHolder class like the example below and call this function after the setText functions you've got there.

public static class MyViewHolder extends RecyclerView.ViewHolder {
    View mView;

    public MyViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
    }

    public void setClickEvent() {
        // Set the onClickListener on mView
        // mView.setOnClickListener(new OnClickListener)...
    }
}

这篇关于Android - FirebaseListAdapter 以相反的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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