【问题标题】:RecyclerView - Reverse OrderRecyclerView - 逆序
【发布时间】:2018-02-20 10:27:37
【问题描述】:

我正在以相反的顺序使用回收站视图来显示聊天历史记录。
如果聊天中只有一条消息,则会在底部显示该消息(如电报)。但我需要从顶部显示它。
我被困在这一天。谁能给我一个建议,以 recyclerview 反向顺序(如whatsapp)从顶部​​显示消息。

【问题讨论】:

  • 使用Comparator对数组列表进行简单的降序排序。
  • 也可以使用Collections.reverse(arrayList);的方法倒序显示聊天记录
  • 试试这个 layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); recyclerView.setLayoutManager(layoutManager);
  • 我也试过了,但是每当我打开聊天记录时,它会显示第一条消息,而不是最后一条。所以,我使用 getLayoutManager().scrollToPosition() 到聊天的底部。但是在聊天的分页过程中,我发现屏幕上有一些波动,从一条消息跳到另一条消息。
  • 所以也许试试这个layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, true); developer.android.com/reference/android/support/v7/widget/…

标签: android


【解决方案1】:

我只是在 RecyclerView 适配器中更改了 ViewHolder 计数器顺序。

改变了这个:

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
    viewHolder.yourTextView.setText(mEntry.get(i));
}

至于:

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
    viewHolder.yourTextView.setText(mEntry.get(getItemCount() - i - 1));
}

【讨论】:

    【解决方案2】:

    android recyclerview 在 kotlin 中逆序或逆向布局

    val linearLayoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, true)
    linearLayoutManager.stackFromEnd = true
    

    【讨论】:

      【解决方案3】:

      是的,实现此目的的方法不同,但一个问题是,在添加新项目时,回收站视图不会显示新项目,您必须手动向下滚动列表。这不是用户友好的。为了解决这个问题,我使用了RecyclerView.smoothScrollToPosition

      在 xml 中执行(不要忘记删除您以编程方式附加的 LinearLayoutManager):

      <android.support.v7.widget.RecyclerView
              android:id="@+id/recordItemList"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:clipToPadding="false"
              android:scrollbars="none"
              app:layoutManager="LinearLayoutManager"
              app:stackFromEnd="true"
              app:reverseLayout="true"/>
      

      然后在添加新项目时调用smoothScrollToPosition

      int newIndex = myArraylist.size();
      myArraylist.add(newItem);
      notifyItemInserted(newIndex)
      myRecyclerView.smoothScrollToPosition(newIndex);
      

      或以编程方式反转您的列表:

      Collections.reverse(myArraylist);
      

      就在设置适配器之前。

      并致电smoothScrollToPosition。这一次,newIndex 为 0

      int newIndex = 0
      myArraylist.add(newIndex,newItem);
      notifyItemInserted(newIndex)
      myRecyclerView.smoothScrollToPosition(newIndex);
      

      【讨论】:

        【解决方案4】:

        这是java最短的解决方案:

        LinearLayoutManager lm = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, true); // last argument (true) is flag for reverse layout
        myRecyclerView.setLayoutManager(lm);
        

        【讨论】:

          【解决方案5】:

          使用 Kotlin:

            mList.asReversed()  // mList : List<Modelo>
          

          【讨论】:

            【解决方案6】:

            我以这种方式做了类似的事情:

            layoutManager = LinearLayoutManager(context).apply {stackFromEnd = true}
            

            记录是按时间倒序排列的

            mCursor = contentResolver.query(Uri.parse("content://sms"),
                            arrayOf("body", "date", "type"),
                            "address like ?",
                            arrayOf("%$id"),
                            "date asc")
            

            【讨论】:

              【解决方案7】:

              将此用于您在 onBindViewHolder 中的适配器位置

              final int posRevers = list.size() - (position + 1);

              【讨论】:

                【解决方案8】:

                你也可以这样用:

                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
                linearLayoutManager.setReverseLayout(true);
                linearLayoutManager.setStackFromEnd(true);
                myRecyclerView.setLayoutManager(linearLayoutManager);
                

                【讨论】:

                • 没有linearLayoutManager.setStackFromEnd(true);
                【解决方案9】:

                我遇到了同样的问题,但通过适配器自己解决了。

                当你添加(例如)JSONArray 时,你可以按如下方式反转循环;

                for (int i=pDate.length()-1; i > -1 ; i--) {
                       try {
                            tModelList.add(new tModel(pData.getJSONObject(i)));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                

                【讨论】:

                  【解决方案10】:

                  您可以使用Collections.reverse(arrayList);方法倒序显示聊天记录。
                  试试这个

                  ArrayList<Model> list = getList();
                  Collections.reverse(list);
                  MyRecyclerViewAdapter adapter = new MyRecyclerViewAdapter (YourActivity.this, list);
                  recyclerView.setAdapter(adapter);
                  recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                  

                  【讨论】:

                    【解决方案11】:

                    几天前我遇到了同样的问题。不管我用这种方式解决这个问题。

                    <android.support.v7.widget.RecyclerView
                            android:id="@+id/reyclerview_message_list"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:scrollbars="vertical"
                            app:stackFromEnd="true"
                            app:reverseLayout="true"/>
                    

                    【讨论】:

                    • 如果用户以编程方式设置RecyclerView布局管理器,那么您需要设置stackFromEndreverseLayout,否则通过属性直接在RecyclerView中设置布局管理器app:layoutManager="LinearLayoutManager" 或您选择的非线性布局管理器。
                    猜你喜欢
                    • 1970-01-01
                    • 2015-12-08
                    • 2017-09-01
                    • 2010-10-17
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2014-07-23
                    相关资源
                    最近更新 更多