首页 > 解决方案 > RecyclerView 只滚动 1 个项目

问题描述

我的布局是:

<android.support.design.widget.CoordinatorLayout 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/pureWhite"
    tools:context=".ControllerPickerActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        android:fitsSystemWindows="true">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="#00cc3d"
            app:layout_scrollFlags="scroll|enterAlways"/>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/controllers_picker_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/pureWhite"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </android.support.design.widget.AppBarLayout>


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@drawable/ic_plus_30dp" />

</android.support.design.widget.CoordinatorLayout>

在此处输入图像描述

我的问题是我的RecyclerView视图不能正确滚动。我有 20 个项目,但它在第一个屏幕上显示 9 个元素,我只能滚动 1 个项目,而不再滚动。我只能到达第 10 个元素。

我是RecyclerViewinCoordinatorLayout的新手,我看到的教程都没有帮助我解决我的问题。

谁能告诉我如何解决这个问题或给我一个处理 a RecyclerViewin a的例子CoordinatorLayout

我的回收站查看项目:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    android:orientation="vertical"
    android:paddingBottom="@dimen/row_padding_vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/row_padding_vertical">

    <TextView
        android:id="@+id/controller_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:textColor="@color/pureBlack"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/controller_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/pureBlack"
        android:layout_below="@id/controller_title" />

我的适配器:

public class ControllerPickerAdapter extends RecyclerView.Adapter<ControllerPickerAdapter.MyViewHolder> {
    private List<Controller> controllerList;

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView title,id;
        public MyViewHolder(View v) {
            super(v);
            title = v.findViewById(R.id.controller_title);
            id = v.findViewById(R.id.controller_id);
        }
    }

    public ControllerPickerAdapter(List<Controller> controllerList) {
        this.controllerList = controllerList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.controller_list_row, parent, false);
        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Controller controller = controllerList.get(position);
        holder.title.setText(controller.getTitle());
        holder.id.setText("" + controller.getId());

    }

    @Override
    public int getItemCount() {
        return controllerList.size();
    }
}

标签: androidxmllayout

解决方案


您尚未向 RecyclerView 添加滚动标志。尝试使用下面的代码添加它,使用适合您要求的滚动值。

app:layout_scrollFlags="scroll|enterAlways"

推荐阅读