recyclerview不适用于所有屏幕尺寸 [英] recyclerview is not fit for all screen sizes

查看:105
本文介绍了recyclerview不适用于所有屏幕尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using recylerview and gridlayout manager with cardviews for each row, my row view(childview) is not responsive at all.

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

I want to show 15 cardviews in such a way that in portrait mode all my 15 childview should be visible and my recyclerview should not be scrollable whereas in landscape mode it should act vise versa( should be scrollable)

我已经尝试过许多关于SO的建议,但是似乎没有任何效果.

I have tried many soultions suggested on SO but nothing seems to be working.

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

current behaviour on different screen sizes are as follows

在上面所附的屏幕截图中,第3列,第4和第5行不可见

In the above attached screen shot ,the 3rd column, 4th and 5th row are not visible

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

In the above given screen my ui fits perfectly in portrait mode but in landscape i can't see all the cardviews.

在上面的屏幕快照中,第5行不可见,并且在横向模式下有一些响应错误. cardview.xml

in the screenshot attached above ,5th row is not visible and in the landscape mode there are some responsivness errors. 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

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>

任何人都可以指导我解决此问题. 谢谢.

Can anyone guide me to solve this . Thanks .

推荐答案

您可以使用:

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

您可能会有一个 包含<integer>元素的res/values/ints.xml文件,为整数指定名称(名称属性)和值(<integer>节点的文本).您还可以使用res/values-w600dp/ints.xmlres/values-land或其他资源变体,在其中提供 用于不同屏幕尺寸的不同值.然后,在运行时,调用 getResources().getInteger()检索要使用的资源的正确值 用于当前设备,并在GridLayoutManager构造函数中使用它.现在, 您可以通过控制跨度来控制有多少列 提供给构造函数.

You could have a res/values/ints.xml file with <integer> elements, giving the integer a name (name attribute) and value (text of the <integer> node). You could also have res/values-w600dp/ints.xml or res/values-land or other variations of the resource, where you provide different values to use for different screen sizes. Then, at runtime, call getResources().getInteger() to retrieve the correct value of the resource to use for the current device, and use that in your GridLayoutManager constructor. Now, you are in control over how many columns there are, by controlling how many spans are supplied to the constructor.

https://developer.android.com/training/multiscreen/screensizes

Chiu-Ki Chan 提出的另一种方法,是创建的子类 RecyclerView,您可以在其上提供所需近似值的自定义属性 列宽.然后,在子类的onMeasure()方法中,您可以计算 为您提供所需的列宽的跨度数.

Another approach, suggested by Chiu-Ki Chan, is to create a subclass of RecyclerView, on which you provide a custom attribute for a desired approximate column width. Then, in your subclass’ onMeasure() method, you can calculate the number of spans to use to give you the desired column width.

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

这篇关于recyclerview不适用于所有屏幕尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆