StaggeredGridLayoutManager with recyclerview 每页只显示两张图片

StaggeredGridLayoutManager with recyclerview showing only two images per page

我从 https://github.com/chrisbanes/cheesesquare 中获取示例代码作为在 StaggeredGridLayoutManager 中显示图像的基础。 StaggeredGridLayoutManager with recyclerview 每页只显示两张图片。

我尝试了几乎所有方法来在整页中显示多个图像,但它不起作用。 任何帮助都会很棒。

fragment_cheese_list.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.support.android.designlibdemo.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>


list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardUseCompatPadding="true"
        app:cardCornerRadius="4dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="@color/color_primary_teal">
<!--<android.support.v7.widget.CardView-->
    <!--xmlns:card_view="http://schemas.android.com/apk/res-auto"-->
    <!--xmlns:android="http://schemas.android.com/apk/res/android"-->
    <!--android:id="@+id/card_view"-->
    <!--android:layout_width="wrap_content"-->
    <!--android:layout_height="wrap_content"-->
    <!--card_view:cardUseCompatPadding="true"-->
    <!--card_view:cardCornerRadius="8dp"-->
    <!--android:layout_marginBottom="16dp">-->

<!--<RelativeLayout-->
    <!--android:layout_width="match_parent"-->
    <!--android:layout_height="match_parent">-->

    <!--<de.hdodenhof.circleimageview.CircleImageView-->
        <!--android:id="@+id/avatar"-->
        <!--android:layout_width="@dimen/list_item_avatar_size"-->
        <!--android:layout_height="@dimen/list_item_avatar_size"-->
        <!--android:layout_marginRight="16dp"/>-->
    <ImageView
        android:id="@+id/avatar"

        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:adjustViewBounds="true"
        android:scaleType="fitXY"

        android:onClick="getImageDetails" />

    <TextView
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textAppearance="?attr/textAppearanceListItem"/>

<!--</RelativeLayout>-->
    <!--</android.support.v7.widget.CardView>-->

</LinearLayout>
    </android.support.v7.widget.CardView>

    </LinearLayout>


CheeseListFragment.java

package com.support.android.designlibdemo;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class CheeseListFragment extends Fragment {
    //private RecyclerView.StaggeredGridLayoutManager mLayoutManager;

    int cheeseImages[]= {R.drawable.cheese_1,R.drawable.cheese_2,R.drawable.cheese_3,
            R.drawable.cheese_4,R.drawable.cheese_5};

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(R.layout.fragment_cheese_list, container, false);

        RecyclerView rv = (RecyclerView) relativeLayout.findViewById(R.id.recyclerview);
        //RecyclerView rv = (RecyclerView) inflater.inflate(R.layout.fragment_cheese_list, container, false);

        setupRecyclerView(rv);
        return rv;
    }

    private void setupRecyclerView(RecyclerView recyclerView) {
        //recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
        StaggeredGridLayoutManager mLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        mLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setHasFixedSize(true);

        ArrayList<ImageDetails> placeList = getPlaces();

        recyclerView.setAdapter(new SimpleStringRecyclerViewAdapter(getActivity(), getRandomSublist(Cheeses.sCheeseStrings, 30), placeList));
    }

    private List<String> getRandomSublist(String[] array, int amount) {
        ArrayList<String> list = new ArrayList<>(amount);
        Random random = new Random();
        while (list.size() < amount) {
            list.add(array[random.nextInt(array.length)]);
        }
        return list;
    }

    private ArrayList<ImageDetails> getPlaces() {
        ArrayList<ImageDetails> details = new ArrayList<>();
        for (int index=0; index<30;index++){
            details.add(new ImageDetails(Cheeses.getRandomCheeseDrawable()));
        }
        return details;
    }

    public static class SimpleStringRecyclerViewAdapter
            extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> {

        private final TypedValue mTypedValue = new TypedValue();
        private int mBackground;
        private List<String> mValues;
        private ArrayList<ImageDetails> mImagesList;

        public static class ViewHolder extends RecyclerView.ViewHolder {
            public String mBoundString;

            public final View mView;
            public final ImageView mImageView;
            public final TextView mTextView;

            public ViewHolder(View view) {
                super(view);
                mView = view;
                mImageView = (ImageView) view.findViewById(R.id.avatar);
                mTextView = (TextView) view.findViewById(android.R.id.text1);
            }

            @Override
            public String toString() {
                return super.toString() + " '" + mTextView.getText();
            }
        }

        public SimpleStringRecyclerViewAdapter(Context context, List<String> items, ArrayList<ImageDetails> imageList) {
//            context.getTheme().resolveAttribute(R.attr.selectableItemBackground, mTypedValue, true);
//            mBackground = mTypedValue.resourceId;
            mValues = items;
            mImagesList = imageList;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
            //view.setBackgroundResource(mBackground);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, int position) {
            holder.mBoundString = mValues.get(position);
            holder.mTextView.setText(mValues.get(position));

            holder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Context context = v.getContext();
                    Intent intent = new Intent(context, CheeseDetailActivity.class);
                    intent.putExtra(CheeseDetailActivity.EXTRA_NAME, holder.mBoundString);

                    context.startActivity(intent);
                }
            });

            holder.mImageView.setImageResource(mImagesList.get(position).getImageUrl());

//            Glide.with(holder.mImageView.getContext())
//                    .load(Cheeses.getRandomCheeseDrawable())
//                    .fitCenter()
//                    .into(holder.mImageView);
        }

        @Override
        public int getItemCount() {
            return mValues.size();
        }
    }
}

list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

将布局高度更改为 wrap_content:-

android:layout_height="wrap_content"