【问题标题】:Shift recyclerview focus to last element in the list将 recyclerview 焦点转移到列表中的最后一个元素
【发布时间】:2015-12-16 08:23:41
【问题描述】:

我正在使用 recyclerview 在聊天窗口中显示消息。但是,当正在编辑新消息时,回收站视图的焦点应该在最后一个元素上。我希望有一些方法可以将 recyclerview 的焦点转移到最后一个元素而不是第一个元素。

【问题讨论】:

  • 使用 recyclerView.smoothScrollToPosition(position); 位置是您插入的最后一个项目的索引。
  • 您可以将其添加为答案,因为它解决了我的问题。
  • @VipulAsri 请把您的评论作为答案,因为它解决了这个问题。你会帮助其他人

标签: android android-layout android-recyclerview


【解决方案1】:

使用recyclerView.smoothScrollToPosition(position);

position 是您最后插入的项目的索引。这会将您的 RecyclerView 焦点移到最后一个元素上。

【讨论】:

    【解决方案2】:

    使用可以在您的 LinearLayoutManager 对象上添加两行中的任何一行,以将您的 recyclerview 滚动到底部...

    llm.setStackFromEnd(true);
    

    llm.setReverseLayout(true);
    

    【讨论】:

    • 它从底部填充列表。这不是我要找的东西。 Vipul的回答解决了我的问题。顺便说一句,谢谢。
    【解决方案3】:

    使用

    recyclerView.smoothScrollToPosition(position)
    adapter.notifyDataSetChanged();
    

    其中位置是最后一个元素的索引

    recyclerView.getAdapter().getItemCount()-1
    

    使用它来获取最后一个元素。

    这对我有用。

    【讨论】:

    • 最佳且必须详细的答案。
    【解决方案4】:

    有两种方法可以做到。

    1:在为 RecyclerView 创建任何 LayoutManager 时,将其构造函数中的最后一个参数设为true

    LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this,LinearLayoutManager.HORIZONTAL,true);
    

    2:只需告诉 RecyclerView 滚动到最后一个位置。

    recyclerView.smoothScrollToPosition(lastPosition);
    

    【讨论】:

      【解决方案5】:

      确保此位置是您要查看的项目的位置。

      recyclerView.smoothScrollToPosition(position);
      

      【讨论】:

        【解决方案6】:

        您可以简单地使用以下代码。这里 recyclerView.getBottom() 定义了要聚焦的位置

        recyclerView.smoothScrollToPosition(recyclerView.getBottom());
        

        【讨论】:

          【解决方案7】:

          使用layoutManager.setStackFromEnd(true); 而不是这个。

          示例

              LinearLayoutManager layoutManager = new LinearLayoutManager(this);
              RecyclerView recyclerView = findViewById(R.id.list);
              layoutManager.setStackFromEnd(true);
              recyclerView.setLayoutManager(layoutManager);
          

          【讨论】:

            【解决方案8】:

            您可以在回收站视图 XML 代码中使用 app:stackFromEnd="true",例如:

             <android.support.v7.widget.RecyclerView
              android:id="@+id/message_list"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_above="@id/message_input"
              android:layout_alignParentTop="true"
              app:stackFromEnd="true" />
            

            【讨论】:

            • 使用 layoutManager.setStackFromEnd(true); 而不是这个。
            猜你喜欢
            • 2022-07-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-02-16
            • 1970-01-01
            • 2016-06-30
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多