我想在屏幕中间显示 RecyclerView 网格项目(水平和垂直),当项目展开时, View 不会滚动,但它应该调整元素大小以适应新的可用屏幕尺寸。 我希望这张图片能够通过短信表达我想要的内容:
我当前的回收器代码是
<androidx.recyclerview.widget.RecyclerView
android:padding="16dp"
android:id="@+id/recyclerView"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
以及项目 View
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="54dp"
android:layout_height="54dp"
android:src="@mipmap/ic_launcher"
android:layout_centerHorizontal="true"
android:id="@+id/appIcon"
android:layout_below="@+id/appName"
android:contentDescription="icon" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="name"
android:fontFamily="sans-serif-condensed"
android:textSize="14dp"
android:textColor="#000"
android:singleLine="true"
android:layout_marginTop="20dp"
android:layout_marginBottom="5dp"
android:layout_centerHorizontal="true"
android:id="@+id/appName"/>
</RelativeLayout>
Java代码:
recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
recyclerView.setHasFixedSize(true);
recyclerView.setItemViewCacheSize(20);
recyclerView.setDrawingCacheEnabled(true);
recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
recyclerView.setAdapter(adapter);
欢迎除 RecyclerView
(如 GridLayout 或 FlexLayout)之外的任何其他选项
最佳答案
为recyclerview添加一个母 View ,并给我们一个重力/布局重力到中心,就像这样:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<androidx.recyclerview.widget.RecyclerView
android:padding="16dp"
android:id="@+id/recyclerView"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
关于java - Android RecyclerView 网格水平和垂直居中项目而不滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57350356/