如何到达滚动视图的最后一项? [英] How to reach the last items of a scroll view?

查看:64
本文介绍了如何到达滚动视图的最后一项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to display a list in a fixed size scrollview, at first it was the first items who weren't showing and I fixed it but now it's the last ones that aren't reachable.

这是我的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代码,用于填充列表:

and here is my java code to populate the list:

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,但不能到达其他数字,这就像它们在布局下,我真的不知道如何更好地解释.

When I scroll I can reach the number 16 but not the other, it's like they are under the layout I don't really know how to explain better.

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

So the question is how can I scroll my list until the last items ?

推荐答案

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

There are a few issues with your code: first and foremost if you want to display data as a list you should use a RecyclerView instead of a ScrollView. ScrollViews are there to allow the content (or part of it) in your activity/fragment to be scrollable.

第二,最好不要为视图设置特定的大小,尤其是在使用ConstraintLayout时.

Second, it's not a good practice to set specific sizes to your views, especially when using ConstraintLayout.

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

Third, onCreateView is meant to be a method that will simply inflate your fragment's layout and return it as a View. For handling the UI, use onViewCreated. That way you will guarantee that your UI will never be handled before your fragment is actually attached to the activity.

因此,回答您的问题时,请使用RecyclerView而不是ScrollView来将您的商品显示为列表,并且您最好从那里浏览

So answering your question, use a RecyclerView to display your items as a list instead of the ScrollView and you'll be good to go from there

这篇关于如何到达滚动视图的最后一项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆