簡體   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