RecyclerView and gridView

北战南征 提交于 2019-12-25 08:57:17

问题


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


回答1:


girditem.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

To add spaces between items set the margin to your desired number of dp




回答2:


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



来源:https://stackoverflow.com/questions/45555970/recyclerview-and-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!