简体   繁体   中英

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 . 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 = 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)
Sets the number of spans to be laid out.
If getOrientation() is VERTICAL , this is the number of columns.
If getOrientation() is HORIZONTAL , this is the number of rows.

First create an xml file for recycle

<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. According to your need create you adapter class and then create custom GridRecyclerView class as follow:-

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

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

I think you found your answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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