【问题标题】:Focus/Scroll recyclerView to last Item焦点/滚动recyclerView到最后一个项目
【发布时间】:2019-05-23 22:44:26
【问题描述】:

我的应用程序具有主要的聊天风格活动,由 RecycleView 组成,并由列表填充。用户插入新项目(文本输入,照片/视频捕获)后,回收应滚动/聚焦到列表的最后一项,但即使使用 recyclerView.scrollToPosition(CustomList.size ()-1)。

完整代码见:https://github.com/MikeSys/ChatApp


主要活动

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

    // Elementi Grafici-----------------------------------------

    RecyclerView recyclerView = findViewById(R.id.recyclerView);
    Button video = findViewById(R.id.video);
    Button camera = findViewById(R.id.camera);
    Button send = findViewById(R.id.send);
    videoView =findViewById(R.id.rec);
    final EditText editText = findViewById(R.id.editText);

    // Media Controller----------------------------------------



    // Layout Manager------------------------------------------------

    linearLayoutManager = new LinearLayoutManager(MainActivity.this);
    linearLayoutManager.setStackFromEnd(true);
    recyclerView.setLayoutManager(linearLayoutManager);


    // Adapter-----------------------------------------

    if(dati.size()> 1){  
        adapter =  new myAdapter(dati, this);
        adapter.notifyDataSetChanged();
        adapter.notifyItemInserted(dati.size()-1);
        recyclerView.setAdapter(adapter);
        //Setted recyclerView to scroll to the last item of the List
        recyclerView.scrollToPosition(dati.size()-1);
    }


    else{
        adapter =  new myAdapter(dati,this);
        recyclerView.setAdapter(adapter);
    }

MainActivity 布局

<android.support.constraint.ConstraintLayout 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:orientation="vertical"
tools:context=".MainActivity">


<android.support.v7.widget.RecyclerView
    android:background="@drawable/sfondo"
    android:foregroundGravity="center"
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toTopOf="@+id/editText"
    app:layout_constraintTop_toTopOf="parent"
    tools:layout_editor_absoluteX="0dp"
    app:stackFromEnd="true"/>


<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginEnd="160dp"
    android:background="#0003A9F4"
    android:hint="Scrivi"
    android:padding="20dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_weight="3"
    app:layout_constraintStart_toStartOf="parent" />


<Button
    android:id="@+id/send"
    android:layout_width="55dp"
    android:layout_height="64dp"
    android:background="@drawable/send"
    app:layout_constraintBottom_toBottomOf="@+id/editText"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toEndOf="@+id/editText"
    app:layout_constraintTop_toTopOf="@+id/editText"
    app:layout_constraintVertical_bias="0.0"/>

<Button
    android:id="@+id/camera"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="16dp"
    android:background="@drawable/camera"
    app:layout_constraintBottom_toBottomOf="@+id/editText"
    app:layout_constraintEnd_toStartOf="@+id/send"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="@+id/editText"
    app:layout_constraintTop_toTopOf="@+id/editText"
    app:layout_constraintVertical_bias="0.0" />

<Button
    android:id="@+id/video"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:background="@drawable/video"
    app:layout_constraintEnd_toStartOf="@+id/camera"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="@+id/editText"
    app:layout_constraintTop_toBottomOf="@+id/recyclerView" />


截图

【问题讨论】:

  • 你试过用LinearLayoutManager.setReverseLayout(true)
  • 尝试注释您滚动到位置的代码,只需保留 setreverselayout。
  • 你设置的是linearLayoutManager.setStackFromEnd(true); 而不是setReverseLayout 两者都不同。
  • 问题是我需要 settackFromEnd 否则物品会从回收站的顶部生成

标签: java android android-recyclerview


【解决方案1】:

使用

recyclerView.smoothScrollToPosition(位置);

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

【讨论】:

猜你喜欢
  • 2021-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-20
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
相关资源
最近更新 更多