recyclerview 不适合所有屏幕尺寸
recyclerview is not fit for all screen sizes
我正在为每行使用带有卡片视图的 recylerview 和 gridlayout 管理器,我的行视图(子视图)根本没有响应。
我想以这样的方式显示 15 个卡片视图,即在纵向模式下我的所有 15 个子视图都应该可见并且我的 recyclerview 不应该是可滚动的,而在横向模式下它应该反之亦然(应该是可滚动的)
我已经尝试了很多关于 SO 的建议,但似乎没有任何效果。
当前在不同屏幕尺寸上的行为如下
在上面附带的屏幕截图中,第 3 列、第 4 行和第 5 行不可见
在上面给定的屏幕中,我的 ui 非常适合纵向模式,但在横向模式下我看不到所有卡片视图。
在上面所附的屏幕截图中,第 5 行不可见,并且在横向模式下存在一些响应错误。
cardview.xml
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:layout_width="127dp"
android:layout_height="118dp"
android:layout_margin="5dp"
cardview:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/mReminder_Image_Id"
android:layout_width="match_parent"
android:layout_height="90dp"
android:scaleType="fitXY"
android:background="#ffffff"/>
<TextView
android:id="@+id/mReminder_Text_Id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#2d2d2d"
android:textSize="13sp"
android:text="Reminder texts"/>
</LinearLayout>
</android.support.v7.widget.CardView>
fragment_reminders.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Fragments.Reminders">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reminders"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="@color/tab_background"
android:layout_gravity="center" />
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.RecyclerView
android:id="@+id/mRecyclerView_id"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
任何人都可以帮我ui解决这个问题。
谢谢.
你可以使用这个:
recyclerView.setLayoutManager(new RecyclerView.GridLayoutManager(this, span_count)
你可以有一个
res/values/ints.xml
包含 <integer>
元素的文件,为整数提供名称(名称属性)和值(<integer>
节点的文本)。您还可以拥有 res/values-w600dp/ints.xml
或 res/values-land
或资源的其他变体,您可以在其中提供
不同的值用于不同的屏幕尺寸。然后,在运行时调用
getResources().getInteger()
检索要使用的资源的正确值
对于当前设备,并在您的 GridLayoutManager 构造函数中使用它。现在,
您可以通过控制有多少跨度来控制有多少列
提供给构造函数。
https://developer.android.com/training/multiscreen/screensizes
Chiu-Ki Chan 建议的另一种方法是创建一个子类
RecyclerView,您可以在其上为所需的近似值提供自定义属性
列宽。然后,在你的子类的 onMeasure()
方法中,你可以计算
用于为您提供所需列宽的跨度数。
https://developer.android.com/reference/android/support/v7/widget/RecyclerView#setlayoutmanager
创建一个class如下:
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
public class EqualSpacingItemDecoration extends RecyclerView.ItemDecoration {
private final int spacing;
private int displayMode;
public static final int HORIZONTAL = 0;
public static final int VERTICAL = 1;
public static final int GRID = 2;
public EqualSpacingItemDecoration(int spacing) {
this(spacing, -1);
}
public EqualSpacingItemDecoration(int spacing, int displayMode) {
this.spacing = spacing;
this.displayMode = displayMode;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildViewHolder(view).getAdapterPosition();
int itemCount = state.getItemCount();
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
setSpacingForDirection(outRect, layoutManager, position, itemCount);
}
private void setSpacingForDirection(Rect outRect,
RecyclerView.LayoutManager layoutManager,
int position,
int itemCount) {
// Resolve display mode automatically
if (displayMode == -1) {
displayMode = resolveDisplayMode(layoutManager);
}
switch (displayMode) {
case HORIZONTAL:
outRect.left = spacing;
outRect.right = position == itemCount - 1 ? spacing : 0;
outRect.top = spacing;
outRect.bottom = spacing;
break;
case VERTICAL:
outRect.left = spacing;
outRect.right = spacing;
outRect.top = spacing;
outRect.bottom = position == itemCount - 1 ? spacing : 0;
break;
case GRID:
if (layoutManager instanceof GridLayoutManager) {
GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
int cols = gridLayoutManager.getSpanCount();
int rows = itemCount / cols;
outRect.left = spacing;
outRect.right = position % cols == cols - 1 ? spacing : 0;
outRect.top = spacing;
outRect.bottom = position / cols == rows - 1 ? spacing : 0;
}
break;
}
}
private int resolveDisplayMode(RecyclerView.LayoutManager layoutManager) {
if (layoutManager instanceof GridLayoutManager) return GRID;
if (layoutManager.canScrollHorizontally()) return HORIZONTAL;
return VERTICAL;
}
}
并将其与回收站视图一起使用:
1) 对于网格视图:
recyclerView.addItemDecoration(new EqualSpacingItemDecoration(<sizeofpixels>, EqualSpacingItemDecoration.GRID));
2)垂直视图:
recyclerView.addItemDecoration(new EqualSpacingItemDecoration(<sizeofpixels>, EqualSpacingItemDecoration.VERTICAL));
3)对于水平视图:
recyclerView.addItemDecoration(new EqualSpacingItemDecoration(<sizeofpixels>, EqualSpacingItemDecoration.HORIZONTAL));
希望对您有所帮助。
我正在为每行使用带有卡片视图的 recylerview 和 gridlayout 管理器,我的行视图(子视图)根本没有响应。
我想以这样的方式显示 15 个卡片视图,即在纵向模式下我的所有 15 个子视图都应该可见并且我的 recyclerview 不应该是可滚动的,而在横向模式下它应该反之亦然(应该是可滚动的)
我已经尝试了很多关于 SO 的建议,但似乎没有任何效果。
当前在不同屏幕尺寸上的行为如下
在上面附带的屏幕截图中,第 3 列、第 4 行和第 5 行不可见
在上面给定的屏幕中,我的 ui 非常适合纵向模式,但在横向模式下我看不到所有卡片视图。
在上面所附的屏幕截图中,第 5 行不可见,并且在横向模式下存在一些响应错误。 cardview.xml
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:layout_width="127dp"
android:layout_height="118dp"
android:layout_margin="5dp"
cardview:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/mReminder_Image_Id"
android:layout_width="match_parent"
android:layout_height="90dp"
android:scaleType="fitXY"
android:background="#ffffff"/>
<TextView
android:id="@+id/mReminder_Text_Id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#2d2d2d"
android:textSize="13sp"
android:text="Reminder texts"/>
</LinearLayout>
</android.support.v7.widget.CardView>
fragment_reminders.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Fragments.Reminders">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reminders"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="@color/tab_background"
android:layout_gravity="center" />
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.RecyclerView
android:id="@+id/mRecyclerView_id"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
任何人都可以帮我ui解决这个问题。 谢谢.
你可以使用这个:
recyclerView.setLayoutManager(new RecyclerView.GridLayoutManager(this, span_count)
你可以有一个
res/values/ints.xml
包含 <integer>
元素的文件,为整数提供名称(名称属性)和值(<integer>
节点的文本)。您还可以拥有 res/values-w600dp/ints.xml
或 res/values-land
或资源的其他变体,您可以在其中提供
不同的值用于不同的屏幕尺寸。然后,在运行时调用
getResources().getInteger()
检索要使用的资源的正确值
对于当前设备,并在您的 GridLayoutManager 构造函数中使用它。现在,
您可以通过控制有多少跨度来控制有多少列
提供给构造函数。
https://developer.android.com/training/multiscreen/screensizes
Chiu-Ki Chan 建议的另一种方法是创建一个子类
RecyclerView,您可以在其上为所需的近似值提供自定义属性
列宽。然后,在你的子类的 onMeasure()
方法中,你可以计算
用于为您提供所需列宽的跨度数。
https://developer.android.com/reference/android/support/v7/widget/RecyclerView#setlayoutmanager
创建一个class如下:
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
public class EqualSpacingItemDecoration extends RecyclerView.ItemDecoration {
private final int spacing;
private int displayMode;
public static final int HORIZONTAL = 0;
public static final int VERTICAL = 1;
public static final int GRID = 2;
public EqualSpacingItemDecoration(int spacing) {
this(spacing, -1);
}
public EqualSpacingItemDecoration(int spacing, int displayMode) {
this.spacing = spacing;
this.displayMode = displayMode;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildViewHolder(view).getAdapterPosition();
int itemCount = state.getItemCount();
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
setSpacingForDirection(outRect, layoutManager, position, itemCount);
}
private void setSpacingForDirection(Rect outRect,
RecyclerView.LayoutManager layoutManager,
int position,
int itemCount) {
// Resolve display mode automatically
if (displayMode == -1) {
displayMode = resolveDisplayMode(layoutManager);
}
switch (displayMode) {
case HORIZONTAL:
outRect.left = spacing;
outRect.right = position == itemCount - 1 ? spacing : 0;
outRect.top = spacing;
outRect.bottom = spacing;
break;
case VERTICAL:
outRect.left = spacing;
outRect.right = spacing;
outRect.top = spacing;
outRect.bottom = position == itemCount - 1 ? spacing : 0;
break;
case GRID:
if (layoutManager instanceof GridLayoutManager) {
GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
int cols = gridLayoutManager.getSpanCount();
int rows = itemCount / cols;
outRect.left = spacing;
outRect.right = position % cols == cols - 1 ? spacing : 0;
outRect.top = spacing;
outRect.bottom = position / cols == rows - 1 ? spacing : 0;
}
break;
}
}
private int resolveDisplayMode(RecyclerView.LayoutManager layoutManager) {
if (layoutManager instanceof GridLayoutManager) return GRID;
if (layoutManager.canScrollHorizontally()) return HORIZONTAL;
return VERTICAL;
}
}
并将其与回收站视图一起使用:
1) 对于网格视图:
recyclerView.addItemDecoration(new EqualSpacingItemDecoration(<sizeofpixels>, EqualSpacingItemDecoration.GRID));
2)垂直视图:
recyclerView.addItemDecoration(new EqualSpacingItemDecoration(<sizeofpixels>, EqualSpacingItemDecoration.VERTICAL));
3)对于水平视图:
recyclerView.addItemDecoration(new EqualSpacingItemDecoration(<sizeofpixels>, EqualSpacingItemDecoration.HORIZONTAL));
希望对您有所帮助。