如何到达滚动视图的最后一项?

问题描述 投票:0回答:1

我正在尝试以固定大小的滚动视图显示列表,起初它是第一个未显示的项目,我已将其修复,但现在它是无法访问的最后一个项目。

这是我的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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    tools:context=".ui.home.AperoDetailFragment">

    <TextView
        android:id="@+id/name_apero"
        android:layout_width="156dp"
        android:layout_height="53dp"
        android:textSize="18sp"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <TextView
        android:id="@+id/date_apero"
        android:layout_width="238dp"
        android:layout_height="53dp"
        android:textSize="18sp"
        android:ems="10"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.907"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <ScrollView
        android:id="@+id/ingredient_apero"
        android:layout_width="410dp"
        android:layout_height="607dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ingredient_title_apero"
        app:layout_constraintVertical_bias="0.0">

        <LinearLayout
            android:id="@+id/vertical_layout_ingredient"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />
    </ScrollView>

    <TextView
        android:id="@+id/ingredient_title_apero"
        android:layout_width="115dp"
        android:layout_height="28dp"
        android:text="Liste d'achat:"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.005"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.125" />
</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());
        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;
    }
}  

当我滚动时,我可以到达数字16,但不能到达其他数字,这就像它们在布局下,我真的不知道如何更好地解释。

所以问题是如何滚动列表直到最后一个项目?

android android-listview android-scrollview
1个回答
0
投票

您的代码存在一些问题:首先,如果要以列表形式显示数据,则应使用RecyclerView而不是ScrollView。 ScrollViews可让您的活动/片段(或其一部分)滚动。

第二,对视图设置特定大小不是一个好习惯,尤其是在使用ConstraintLayout时。

第三,onCreateView意味着将简单地增加片段的布局并将其作为View返回。要处理UI,请使用onViewCreated。这样,您将保证在将片段实际附加到活动之前永远不会处理UI。

所以回答您的问题,请使用RecyclerView将您的项目显示为列表而不是ScrollView,然后从那里开始您会很好的]]

© www.soinside.com 2019 - 2024. All rights reserved.