繁体   English   中英

recyclerview不适用于所有屏幕尺寸

[英]recyclerview is not fit for all screen sizes

我正在使用每行卡片视图的recylerview和gridlayout管理器,我的行视图(childview)完全没有响应。

我想以这样的一种方式显示15张卡片视图:在纵向模式下,我所有15个子视图都应该可见,并且recyclerview不能滚动,而在横向模式下,它应该起作用(反之亦然)

我已经尝试过许多建议,但似乎没有任何效果。

不同屏幕尺寸下的当前行为如下

在此处输入图片说明

在上面的屏幕快照中,第三列,第四行和第五行不可见

在此处输入图片说明

在上面给定的屏幕上,我的用户界面非常适合纵向模式,但是在横向模式下,我看不到所有卡片视图。

在此处输入图片说明

在上面所附的屏幕快照中,第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>

谁能指导我解决这个问题。 谢谢 。

您可以使用此:

recyclerView.setLayoutManager(new RecyclerView.GridLayoutManager(this, span_count)

您可能有一个带有<integer>元素的res/values/ints.xml文件,为整数提供了一个名称(名称属性)和值( <integer>节点的文本)。 您还可以使用res/values-w600dp/ints.xmlres/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/GridLayoutManager.html#GridLayoutManager(android.content.Context,%20int)

https://developer.android.com/reference/android/support/v7/widget/RecyclerView#setlayoutmanager

创建一个如下的类:

    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));

希望对您有帮助。

暂无
暂无

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

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