有这样的布局:
<LinearLayout
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="wrap_content"
android:orientation="vertical">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/dateTV"
tools:context=".DayTasksactivity"
tools:layout_editor_absoluteX="1dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/RWTasksOnDay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="16dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/RWTasksOnDay">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/taskET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName" />
<com.google.android.material.button.MaterialButton
android:id="@+id/addTaskBTN"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:enabled="false"
android:text="@string/add_task" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</LinearLayout>
这个听众:
addTaskBTN.setOnClicklistener(new View.OnClicklistener() {
@Override
public void onClick(View v) {
String task = String.valueOf(taskET.getText());
mDayLab.addTask(mDay,task);
updateRecyclerView();
scrollView.post(new Runnable() {
@Override
public void run() {
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
});
}
});
单击新项目添加到RecyclerView时,需要添加滚动到新项目的ScrollView,向下滚动到底部。完全使用FullScroll可以达到预期的结果,或者做错了什么。 请告诉我如何解决这个问题。