android - 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 个元素。
我是RecyclerView
inCoordinatorLayout
的新手,我看到的教程都没有帮助我解决我的问题。
谁能告诉我如何解决这个问题或给我一个处理 a RecyclerView
in 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();
}
}
解决方案
您尚未向 RecyclerView 添加滚动标志。尝试使用下面的代码添加它,使用适合您要求的滚动值。
app:layout_scrollFlags="scroll|enterAlways"
推荐阅读
- python - 在 Python 中使用用户输入来索引列表
- webpack - Microsoft.AspNetCore.SpaServices.Extensions 的文档在哪里?
- antlr4 - 用于简单命令处理器的 Lexer 和 Parser 规则
- mysql - Ejabberd 无法连接到“localhost”:3306:{error,“连接失败:连接被拒绝”}
- python - 如何使用 JSON 中前一个值的最后一个数字继续新值?
- gcc - GCC 是否有用于 AVX512 操作的内置函数?
- android - Flutter LockTask
- javascript - 一种在悬停而不是单击时触发子菜单的方法?
- python - 将 pandas 数据框中的值添加到列表中
- google-app-maker - Google 应用制作工具表查询 _notContains 不起作用