【问题标题】:How to reach the last items of a scroll view?如何到达滚动视图的最后一项?
【发布时间】:2020-03-03 15:17:15
【问题描述】:

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

这是我的 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】:

    您的代码存在一些问题:首先,如果您想将数据显示为列表,您应该使用 RecyclerView 而不是 ScrollView。 ScrollView 允许您的活动/片段中的内容(或其中的一部分)可滚动。

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

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

    所以回答你的问题,使用 RecyclerView 将你的项目显示为列表而不是 ScrollView,你会很高兴从那里开始

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多