RecyclerView中的GridLayout

时间:2017-05-27 14:32:06

标签: android user-interface gridview android-recyclerview

我有一个RecyclerView,布局管理器设置为GirdLayout管理器,但我目前面临列中对象间距的问题。有没有办法像在GridView中那样在列之间设置间距(比如使用android:strechMode)?

这是单元格的布局

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffff0000"
    tools:layout_editor_absoluteY="0dp"
    tools:layout_editor_absoluteX="0dp">

    <ImageView
        android:id="@+id/movie_poster"
        android:layout_width="150dp"
        android:layout_height="188dp"
        android:layout_marginBottom="4dp"
        android:layout_marginEnd="2dp"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp"
        android:layout_marginStart="2dp"
        android:layout_marginTop="4dp"
        android:contentDescription="@string/image_desc"
        android:scaleType="centerCrop"
        app:layout_constraintBottom_toTopOf="@+id/movie_title"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:srcCompat="@mipmap/ic_launcher"/>

    <TextView
        android:id="@+id/movie_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:layout_marginEnd="2dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="2dp"
        android:layout_marginStart="8dp"
        android:text="@string/app_name"
        android:textAppearance="@style/TextAppearance.AppCompat.Headline"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>

这是主视图布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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"
    tools:context="com.demoapp.MainActivity">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/movies_list"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scrollbars="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        tools:listitem="@layout/movie_list_layout"
        app:layoutManager="GridLayoutManager"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>

主要活动代码:

公共类MainActivity扩展了AppCompatActivity {

RecyclerView mMovieListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mMovieListView = (RecyclerView)findViewById(R.id.movies_list);

    GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
    mMovieListView.setLayoutManager(gridLayoutManager);
    mMovieListView.setAdapter(new MovieListAdapter(this));
    mMovieListView.addItemDecoration(new SpacesItemDecoration(getResources().getDimensionPixelSize(R.dimen.cardview_default_radius)));
}}

装饰者类

public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
    private int mGapInPixels;
    public SpacesItemDecoration(int pixels) {
        this.mGapInPixels = pixels;
    }
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        outRect.left = mGapInPixels;
        outRect.right = mGapInPixels;
        outRect.bottom = mGapInPixels;

        // Add top margin only for the first item to avoid double space between items
        if (parent.getChildLayoutPosition(view) == 0 || parent.getChildLayoutPosition(view) == 1) {
            outRect.top = mGapInPixels;
        } else {
            outRect.top = 0;
        }
    }
}

0 个答案:

没有答案