RecyclerView和gridView [英] RecyclerView and gridView
本文介绍了RecyclerView和gridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用recyclerview实现了gridview,问题在于图像显示不正确,因为图像之间有太多空间,这里是主要活动xml和griditem.xml。
mainactivity.xml
< RelativeLayout xmlns:android =http://schemas.android.com/apk/res/android
xmlns:tools =http://schemas.android.com/tools
xmlns:app =http://schemas.android.com/apk/res-auto
android:layout_width =match_parent
android:layout_height =match_parent>
< android.support.v7.widget.Toolbar
android:id =@ + id / toolbar
android:layout_width =match_parent
android :layout_height =?attr / actionBarSize
android:background =?attr / colorPrimary
android:elevation =4dp
app:popupTheme =@ style / ThemeOverlay.AppCompat。光/>
< android.support.v7.widget.RecyclerView
android:id =@ + id / rv
android:layout_below =@ + id / toolbar
android:layout_width =match_parent
android:scrollbars =vertical
android:layout_height =match_parent/>
< / RelativeLayout>
griditem.xml:
< LinearLayout xmlns:android =http://schemas.android.com/apk/res/android
android:layout_width =wrap_content
android:layout_height = WRAP_CONTENT >
android:layout_width =200dp
android:layout_height =200dp
android:layout_gravity =center
android: ID = @ + ID / IV/>
< / LinearLayout>
在主要活动中:
GridLayoutManager grid = new GridLayoutManager(this,2);
recyclerView.setAdapter(new MovieRecyclerAdapter());
recyclerView.setLayoutManager(grid);
,结果如下
你可以看到两列图像之间有空格,我怎么去掉这些空格?
内容
解决方案
这样做:
< RelativeLayout xmlns:android =http://schemas.android.com / apk / res / android
xmlns:tools =http://schemas.android.com/tools
xmlns:app =http://schemas.android.com/apk/res -auto
android:layout_width =match_parent
android:layout_height =match_parent>
< android.support.v7.widget.Toolbar
android:id =@ + id / toolbar
android:layout_width =match_parent
android :layout_height =?attr / actionBarSize
android:background =?attr / colorPrimary
android:elevation =4dp
app:popupTheme =@ style / ThemeOverlay.AppCompat。光/>
< android.support.v7.widget.RecyclerView
android:id =@ + id / rv
android:layout_below =@ + id / toolbar
android:layout_width =match_parent
android:scrollbars =vertical
android:layout_height =wrap_content/>
< / RelativeLayout>
您的商品xml外部布局的高度也应该为 wrap_content
I implemented gridview using recyclerview the problem is that images are not showing properly like there is so much space between images, here is xml of mainactivity and griditem.xml
mainactivity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_below="@+id/toolbar"
android:layout_width="match_parent"
android:scrollbars="vertical"
android:layout_height="match_parent"/>
</RelativeLayout>
griditem.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:id="@+id/iv"/>
</LinearLayout>
And in mainactivity:
GridLayoutManager grid = new GridLayoutManager(this,2);
recyclerView.setAdapter(new MovieRecyclerAdapter());
recyclerView.setLayoutManager(grid);
and the result is like this
you can see there is space around two columns of image, how can i remove these spaces?
After height to wrap content
解决方案
Do this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_below="@+id/toolbar"
android:layout_width="match_parent"
android:scrollbars="vertical"
android:layout_height="wrap_content"/>
</RelativeLayout>
Your item xml outer layout should also have the height as wrap_content
这篇关于RecyclerView和gridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文