简体   繁体   English

如何在recyclerview中仅显示2个适合x和y的网格项?

[英]How to show only 2 grid item in the recyclerview that fit in x and y?

I have a RecyclerView that shows inside the fragment with grid Layoutmanager . 我有一个RecyclerView ,它使用Grid Layoutmanagerfragment内部显示。 My question is how can I show one griditem exactly in half of the screen ? 我的问题是如何在屏幕的一半显示一个网格项目?

I want to show just 2 columns, both of which have same height. 我只想显示两列,这两列的高度相同。

This is my grid item (represent one grid item): 这是我的网格项目(代表一个网格项目):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:id="@+id/cardViewGridItemXml"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        card_view:cardCornerRadius="3dp"
        android:layout_marginTop="3dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        card_view:cardElevation="0.01dp"
        android:layout_marginBottom="0dp">

        <RelativeLayout
            android:id="@+id/topLayoutGridItemXml"
            android:layout_width="165dp"
            android:layout_height="160dp">

            <ImageView
                android:id="@+id/imgTumbnailGridItemXml"
                android:layout_width="wrap_content"
                android:layout_height="150dp"
                android:scaleType="fitXY"
                android:layout_above="@+id/txtTitleGridItemXml"
                android:src="@drawable/leopard"
                />

            <TextView
                android:id="@+id/txtTitleGridItemXml"
                android:layout_width="fill_parent"
                android:layout_height="40dp"
                android:paddingLeft="5dp"
                android:paddingRight="2dp"
                android:layout_gravity="bottom"
                android:background="#ff444444"
                android:textColor="#fff"
                android:textSize="20dp"
                android:text="Test"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                />


        </RelativeLayout>



    </android.support.v7.widget.CardView>

</LinearLayout>

And this is my GridLayoutManager : 这是我的GridLayoutManager

 GridLayoutManager  gridLayoutManager = new GridLayoutManager(getActivity(), 2);
 recyclerView.setLayoutManager(gridLayoutManager);

这是我的屏幕截图

for set fixed no of column 用于设置固定的列数

 GridLayoutManager  gridLayoutManager = new GridLayoutManager(getActivity(), 2);
 gridLayoutManager.setSpanCount(NUMBER_OF_COLUMN_YOU_NEED);
 recyclerView.setLayoutManager(gridLayoutManager);

public void setSpanCount(int spanCount) 公共无效setSpanCount(int spanCount)
Sets the number of spans to be laid out. 设置要布置的跨度数。
If getOrientation() is VERTICAL , this is the number of columns. 如果getOrientation()VERTICAL ,则为列数。
If getOrientation() is HORIZONTAL , this is the number of rows. 如果getOrientation()HORIZONTAL ,则为行数。

First create an xml file for recycle 首先创建一个XML文件进行回收

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:orientation="vertical"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:foreground="?android:attr/selectableItemBackground">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                    android:layout_marginTop="30dp"
                  android:padding="@dimen/spacing_xsmall">


        <com.swoquix.developer.kumbhevents.widgets.SquareImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/black"
            android:scaleType="centerCrop"/>

        <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="@dimen/spacing_large"
            android:textColor="@android:color/white"
            android:background="@android:color/black"/>

    </LinearLayout>

</FrameLayout>

Now it depends on you how you are fetching data just like from JSON or Local Database. 现在,这取决于您如何像从JSON或本地数据库一样获取数据。 According to your need create you adapter class and then create custom GridRecyclerView class as follow:- 根据您的需要创建您的适配器类,然后创建自定义GridRecyclerView类,如下所示:

public class GridRecyclerView extends RecyclerView {

    public GridRecyclerView(Context context) {
        super(context);
    }

    public GridRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public GridRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void setLayoutManager(LayoutManager layout) {
        if (layout instanceof GridLayoutManager) {
            super.setLayoutManager(layout);
        } else {
            throw new ClassCastException("You should only use a GridLayoutManager with GridRecyclerView.");
        }
    }

    @Override
    protected void attachLayoutAnimationParameters(View child, @NonNull ViewGroup.LayoutParams params, int index, int count) {
    if (getAdapter() != null && getLayoutManager() instanceof GridLayoutManager) {

        GridLayoutAnimationController.AnimationParameters animationParams =
                (GridLayoutAnimationController.AnimationParameters) params.layoutAnimationParameters;

        if (animationParams == null) {
            animationParams = new GridLayoutAnimationController.AnimationParameters();
            params.layoutAnimationParameters = animationParams;
        }

        int columns = ((GridLayoutManager) getLayoutManager()).getSpanCount();

        animationParams.count = count;
        animationParams.index = index;
        animationParams.columnsCount = columns;
        animationParams.rowsCount = count / columns;

        final int invertedIndex = count - 1 - index;
        animationParams.column = columns - 1 - (invertedIndex % columns);
        animationParams.row = animationParams.rowsCount - 1 - invertedIndex / columns;

    } else {
        super.attachLayoutAnimationParameters(child, params, index, count);
    }
}

} }

The next you want to do. 下一个你想做的。 Paste code in your MainActivity 将代码粘贴到您的MainActivity中

 recyclerView = (RecyclerView) findViewById(R.id.recycler);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));

I think you found your answer. 我想您找到了答案。

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

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