简体   繁体   English

具有固定行数的RecyclerView

[英]RecyclerView with fixed row count

I want to have a recyclerView with fixed row count not scrollable and every row has a fixed height as well to fill all screen. 我想要一个带有不可滚动固定行数的recyclerView,并且每一行都具有固定高度,以填满所有屏幕。 For example, I had 4 rows so row height should be recyclerView height /4. 例如,我有4行,因此行高度应为recyclerView height / 4。

Row layout .xml 行布局.xml

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:clickable="true"

>
<ImageView
    android:id="@+id/ivImage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:scaleType="centerCrop"
    android:src="@drawable/carte_recto"
    app:layout_collapseMode="parallax"
    app:layout_collapseParallaxMultiplier="0.7"
    android:layout_margin="0dp"
    android:padding="0dp"
    android:background="@color/colorPrimary"/>

RecyclerView: RecyclerView:

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="0dp"
    android:padding="0dp"
    android:scrollbars="none" />

java code : Java代码:

 mRecyclerView.setHasFixedSize(true);
    GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(),1, LinearLayoutManager.VERTICAL, false);
    mRecyclerView.setLayoutManager(gridLayoutManager);
    mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), onItemClickListener));
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    mRecyclerView.setAdapter(storeAdapter);
    storeAdapter.notifyDataSetChanged();

Any ideas? 有任何想法吗?

You could use a Grid Layout manager with a span of 4. 您可以使用跨度为4的网格布局管理器。

layoutManager = new GridLayoutManager(getContext(), **4**, GridLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(layoutManager);

In your Adapter 在您的适配器中

 @Override
public int getItemCount() {
    return 4;
}

by returning a specific number, you can fix the number of iterations. 通过返回一个特定的数字,您可以确定迭代次数。

If you want to make it not scrollable, then return 1 in getCount() and define 4 items in your MyRow.xml and use weight_sum . 如果要使其不可滚动,则在getCount() return 1并在MyRow.xml定义4个项目,并使用weight_sum

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM