[英]How to show only 2 grid item in the recyclerview that fit in x and y?
我有一個RecyclerView
,它使用Grid Layoutmanager
在fragment
內部顯示。 我的問題是如何在屏幕的一半顯示一個網格項目?
我只想顯示兩列,這兩列的高度相同。
這是我的網格項目(代表一個網格項目):
<?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>
這是我的
GridLayoutManager
:
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(gridLayoutManager);
用於設置固定的列數
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
gridLayoutManager.setSpanCount(NUMBER_OF_COLUMN_YOU_NEED);
recyclerView.setLayoutManager(gridLayoutManager);
公共無效setSpanCount(int spanCount)
設置要布置的跨度數。
如果getOrientation()
為VERTICAL
,則為列數。
如果getOrientation()
為HORIZONTAL
,則為行數。
首先創建一個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>
現在,這取決於您如何像從JSON或本地數據庫一樣獲取數據。 根據您的需要創建您的適配器類,然后創建自定義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);
}
}
}
下一個你想做的。 將代碼粘貼到您的MainActivity中
recyclerView = (RecyclerView) findViewById(R.id.recycler);
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
我想您找到了答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.