RecyclerView仅滚动1个项目

时间:2018-12-21 11:12:32

标签: android xml layout

我的布局是:

<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>

enter image description here

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

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

有人可以教我如何解决此问题,也可以给我示范处理RecyclerView中的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();
    }
}

2 个答案:

答案 0 :(得分:1)

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

app:layout_scrollFlags="scroll|enterAlways"

答案 1 :(得分:0)

为什么要将RecyclerView放在AppBarLayout内?有必要吗?如果没有,请使用此选项:

<?xml version="1.0" encoding="utf-8"?>
<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:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <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.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/controllers_picker_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"
        android:background="@color/pureWhite" />

    <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>

或者您可以尝试以下一种方法:

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <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="match_parent"
            android:background="@color/pureWhite"
            app:layout_scrollFlags="scroll" />
    </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>