0

我的应用程序正在将最新数据从 firebase 返回到 ListView 的底部。但我希望它在顶部!我已经考虑过了,我认为只有两种可能的方法来做到这一点。

1. 反转列表视图。
我认为这种方式应该是这样做的,但我无法弄清楚。我在网上搜索了很多,但没有适合我的情况的解决方案
这是我的适配器代码

 public void onStart() {
        super.onStart();
        // Setup our view and list adapter. Ensure it scrolls to the bottom as data changes
        final ListView listView = getListView();
        // Tell our list adapter that we only want 50 messages at a time
        mChatListAdapter = new ChatListAdapter(mFirebaseRef.limit(50), this, R.layout.chat_message, mUsername);
        listView.setAdapter(mChatListAdapter);
}

这是扩展特殊列表适配器类ChatListAdapter的自定义列表类的构造函数的代码:ChatListAdapterFirebaseListAdapter

public ChatListAdapter(Query ref, Activity activity, int layout, String mUsername) {
    super(ref, Chat.class, layout, activity);
    this.mUsername = mUsername;
}

[编辑]这是FirebaseListAdapter扩展BaseAdapter类的一些代码

 public FirebaseListAdapter(Query mRef, Class<T> mModelClass, int mLayout, Activity activity) {
    this.mRef = mRef;
    this.mModelClass = mModelClass;
    this.mLayout = mLayout;
    mInflater = activity.getLayoutInflater();
    mModels = new ArrayList<T>();
    mModelKeys = new HashMap<String, T>();
    // Look for all child events. We will then map them to our own internal ArrayList, which backs ListView
    mListener = this.mRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {

            T model = dataSnapshot.getValue(FirebaseListAdapter.this.mModelClass);
            mModelKeys.put(dataSnapshot.getKey(), model);

            // Insert into the correct location, based on previousChildName
            if (previousChildName == null) {
                mModels.add(0, model);
            } else {
                T previousModel = mModelKeys.get(previousChildName);
                int previousIndex = mModels.indexOf(previousModel);
                int nextIndex = previousIndex + 1;
                if (nextIndex == mModels.size()) {
                    mModels.add(model);
                } else {
                    mModels.add(nextIndex, model);
                }
            }
        }

2. 降序查询数据。
第二种方式对我来说是不可能的,因为当我在 Firebase API 文档和网络上搜索时,我无论如何都找不到以降序方式订购回溯数据。
我在 firebase 上的数据如下所示:

glaring-fire-9714 
    chat
        -Jdo7-l9_KBUjXF-U4_c
            author: Ahmed
            message: Hello World
        -Jdo71zU5qsL5rcvBzRl
            author: Osama
            message: Hi!


谢谢你。

4

3 回答 3

2

一个简单的解决方案是手动将新添加的数据移动到listview. 正如您正确注意到的,添加到 a 的新数据listview将自动附加到列表的底部,但是一旦添加条目,您就可以自由移动条目。以下内容将帮助您手动将最新条目移动到列表顶部:

int iSwapCount = listView.getCount() - 1;
int iPosition = listView.getCount() - 1;
for (int j = 0; j < iSwapCount; j++)
{
    Collections.swap(yourlistobject, iPosition, iPosition - 1);
    iPosition = iPosition - 1;
}

上面的代码将首先计算将最后一个列表条目移动到列表顶部所需的交换次数,这取决于列表中元素的数量 - 1。计算最后一个位置也是如此在列表中。从那里Collections.swap将用于将列表中的最后一个元素与它之前的元素交换;这将重复,直到最后一个元素现在是第一个元素,列表中的其余条目保持相同的顺序。每次添加新条目时都必须调用此代码,以便维护列表的整体顺序。

于 2014-12-23T00:31:20.840 回答
2

我意识到你问已经有一段时间了,但我遇到了同样的问题。这里似乎没有直接的答案。

这是对 firebase 适配器的更改,以使新项目位于列表顶部。

注意从 add(...) 到 add(0,...) 和 add(next...) 到 add(prev...) 的变化

寻找评论:

                // prepend instead append

例子:

...

       // Insert into the correct location, based on previousChildName
        if (previousChildName == null) {
            mModels.add(0, model);
            mKeys.add(0, key);
        } else {
            int previousIndex = mKeys.indexOf(previousChildName);
            int nextIndex = previousIndex + 1;

            if (nextIndex == mModels.size()) {
                //mModels.add(model);
                //mKeys.add(key);

                // prepend instead append
                mModels.add(0,model);
                mKeys.add(0,key);
            } else {
                //mModels.add(nextIndex, model);
                //mKeys.add(nextIndex, key);

                // prepend instead append
                mModels.add(previousIndex, model);
                mKeys.add(previousIndex, key);

            }
        }
...
于 2015-09-03T01:16:45.663 回答
1

这是使用 RecyclerView 反转 FirebaseUI 列表的简单方法:

    boolean reverseList = true;
    LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, reverseList);
    if (reverseList) {
        manager.setStackFromEnd(true);
    }
    mRecyclerView.setLayoutManager(manager);
于 2017-07-15T22:32:33.497 回答