[之前我问了一个问题about how to reach the last item of a scrollview,有人指出我应该使用NestedScrollView,起初它可以工作,但现在不是我想要的。
我想让我的项目列表适合ScrollView,以便只能滚动屏幕的一部分,而其他部分留在原处(3 TextView)
所以基本上我的xml文件是这样的:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/cl_framgnent_detail_apero"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
tools:context=".ui.home.AperoDetailFragment">
<TextView
android:id="@+id/name_apero"
/>
<TextView
android:id="@+id/date_apero"
/>
<TextView
android:id="@+id/ingredient_title_apero"
/>
<ScrollView
android:id="@+id/rv_apero_ingredient"
android:layout_width="408dp"
android:layout_height="603dp"
android:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/ingredient_title_apero"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ingredient_title_apero"
app:layout_constraintVertical_bias="1.0">
<LinearLayout
android:id="@+id/vertical_layout_ingredient"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
并且在我的Java代码中,我尝试使用以下代码填充列表:
public class AperoDetailFragment extends Fragment {
private View root;
private Apero detailApero;
public AperoDetailFragment(Apero apero) {
this.detailApero = apero;
}
@Override
public View onCreateView(@NonNull final LayoutInflater inflater,
final ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_detail_apero, container, false);
TextView name = (TextView) root.findViewById(R.id.name_apero);
name.setText(detailApero.getName());
TextView date = (TextView) root.findViewById(R.id.date_apero);
date.setText(detailApero.getDate());
LinearLayout ll = (LinearLayout) root.findViewById(R.id.vertical_layout_ingredient);
LinearLayout a = new LinearLayout(root.getContext());
ConstraintLayout.LayoutParams lparams = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT);
a.setLayoutParams(lparams);
a.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i < 20; i++) {
Button b = new Button(root.getContext());
b.setText("Button " + i);
a.addView(b);
}
ll.addView(a);
return root;
}
}
问题是该项目覆盖了整个屏幕,而不是停留在父容器(ScrollView)中:
我如何使我的物品清单适合留在父母中?
如果这是您的实际代码,那么也可能有助于对文本视图设置约束。
此外,如果ScrollView具有固定的高度,则应删除顶部或底部约束。因此,如果您希望它坚持到底,请删除顶部约束。
我通过删除ScrollView并使用ListView解决了此问题