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

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

问题描述

我现在有一个FirebaseListAdapter,它使用最后5个项目来填充一个ListView:

  @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,它被添加到列表的底部。我希望在列表顶部的最新项目。



是否有可能实现这一目标?



帮助非常感谢,谢谢!

解决方案

这不是你的问题的确切解决方案顺序,但无论如何,我们还有其他的工作。

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

要实现这个行为,您需要将firebase数据放入列表中,然后在将其传递给适配器之前,您必须先将其自动反转。简单!

第二个解决方法很简单,我想如果你使用 RecyclerView ,它可以做到这一点对你而言,我认为这是你做这份工作最简单的方法。

这里是我用 RecyclerView

  //首先声明RecyclerView和LinearLayoutManager $ b $ private private RecyclerView listView; 
私有LinearLayoutManager mLayoutManager;

...

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

//在这里使用FirebaseRecyclerAdapter

//在这里您修改LinearLayoutManager
mLayoutManager = new LinearLayoutManager(MainActivity.this);
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);

//现在将布局管理器和适配器设置为RecyclerView
listView.setLayoutManager(mLayoutManager);
listView.setAdapter(adapter);



$ b

通过设置 mLayoutManager.setReverseLayout(true); - 您正在逆转布局,并且 mLayoutManager.setStackFromEnd(true); 将视图置于列表顶部。

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

 <?xml version =1.0encoding =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_horizo​​ntal_margin
android:paddingRight =@ dimen / activity_horizo​​ntal_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

 依赖项{
compile'c​​om.android.support:recyclerview-v7:23.4.0'
}

您可以在这里找到 FirebaseRecyclerAdapter FirebaseUI 库中。注意:不要使用 RecyclerView.LayoutManager 作为 RecyclerView.LayoutManager 中找不到> setReverseLayout setStackFromEnd 函数。如上所述使用 LinearLayoutManager

更新

以下是您可以如何处理项目的点击事件列表。

您必须声明一个 ViewHolder 来实现 RecyclerView 对吗?只需在下面的例子中在 ViewHolder 类中添加另一个函数,然后在 setText 函数之后调用这个函数那里。

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

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

$ b $ public void setClickEvent(){
//在mView
上设置onClickListener // mView.setOnClickListener(new OnClickListener)...
}
}


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

}

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.

Is it possible to achieve this?

Any help is greatly appreciated, thanks!

解决方案

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.

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!

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.

Here's I'm modifying some of my code with 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);
}

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

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>

And in your build.gradle

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

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

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

Update

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

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天全站免登陆