问题描述
我使用 recylerview 和 gridlayout 管理器,每行都有卡片视图,我的行视图(子视图)根本没有响应.
I am using recylerview and gridlayout manager with cardviews for each row, my row view(childview) is not responsive at all.
我想以这样的方式显示 15 个卡片视图,以便在纵向模式下我所有的 15 个子视图都应该可见,并且我的回收视图不应该是可滚动的,而在横向模式下它应该反之(应该是可滚动的)
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
在上面给定的屏幕中,我的 ui 非常适合纵向模式,但在横向模式下,我看不到所有卡片视图.
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)
你可以有一个res/values/ints.xml
文件与 元素,给整数一个名称(名称属性)和值(
;
节点).您还可以拥有 res/values-w600dp/ints.xml
或 res/values-land
或资源的其他变体,您可以在其中提供不同的值用于不同的屏幕尺寸.然后,在运行时,调用getResources().getInteger()
检索要使用的资源的正确值对于当前设备,并在您的 GridLayoutManager 构造函数中使用它.现在,您可以通过控制跨度数来控制有多少列提供给构造函数.
You could have ares/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 providedifferent values to use for different screen sizes. Then, at runtime, callgetResources().getInteger()
to retrieve the correct value of the resource to usefor 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 spansare 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 ofRecyclerView, on which you provide a custom attribute for a desired approximatecolumn width. Then, in your subclass’ onMeasure()
method, you can calculate thenumber of spans to use to give you the desired column width.
https://developer.android.com/reference/android/support/v7/widget/RecyclerView#setlayoutmanager
这篇关于recyclerview 不适合所有屏幕尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!