在RecyclerView

时间:2018-04-18 16:25:34

标签: android android-recyclerview android-imageview gridlayoutmanager

我有一个包含大量图像的Recyclerview。您可以通过更改gridlayoutmanager的跨度计数将列表从网格更改为列表,但每次在一个或另一个之间切换时,我的一些视图将变为不可见。如果我能在recyclerview中找到它们,我仍然可以点击它们。

用于更改范围计数的代码:

public void initViews() {
        findViewById(R.id.photoGrid).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gridLayoutManager.setSpanCount(3);
                gridLayoutManager.requestLayout();
                SharedPreferences sharedPref = mAct.getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putString("menuLayout", "grid");
                editor.apply();
            }
        });
        findViewById(R.id.photoList).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gridLayoutManager.setSpanCount(1);
                gridLayoutManager.requestLayout();
                SharedPreferences sharedPref = mAct.getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putString("menuLayout", "list");
                editor.apply();
            }
        });

    }

项目布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/galleryTile"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:cardElevation="3dp"
    app:cardUseCompatPadding="true"
    card_view:cardPreventCornerOverlap="false">

        <ImageView
            android:id="@+id/galleryImageView"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:adjustViewBounds="true"
            android:background="#E1E1E1"
            android:scaleType="centerCrop"
            android:src="@color/white" />
        <!--android:src="@drawable/smittyplaceholder" />-->

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

适配器:

package com.ocv.starterpack.imagegallery;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.ocv.starterpack.OCVItem;
import com.ocv.starterpack.R;

import java.io.File;
import java.util.ArrayList;

public class ImageGalleryAdapter extends RecyclerView.Adapter<ImageGalleryAdapter.CustomViewHolder> {

    //Basic concept: grab blog items, strip out images, load images into recycler, allow a detail view with zooming and the like
    //pretty straightforward no?
    //this is the recycler adapter

    protected Context mContext;
    //showDate = 2, shows date and time
    //show date = 1, shows date exclusively
    //show date = 0, shows neither date nor time

    protected int showDate = 2;
    protected Activity mAct;
    protected RequestOptions options = new RequestOptions().error(R.drawable.nopicture);
    protected Bitmap bitmap;
    protected File file;
    private ArrayList<OCVItem> feedItemList;
    private ArrayList<OCVItem> feedItemListCopy;

    ImageGalleryAdapter(Context context, Activity mAct, ArrayList<OCVItem> items) {
        this.feedItemList = items;
        this.mContext = context;
        this.mAct = mAct;
        feedItemListCopy = feedItemList;
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.image_gallery_item, null);
        return new CustomViewHolder(view);
    }

    //I wanted to get rid of this position in the constructor, but I cannot override the constructor
    //without it so I'm using it regardless.
    @Override
    public void onBindViewHolder(final CustomViewHolder holder, final int position) {
        final OCVItem item = feedItemList.get(position);
        Glide.with(mContext).load(item.getLargeURL()).apply(options).thumbnail(.95f).into(holder.galleryImage);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ImageGalleryDetailDialog detailDialog = new ImageGalleryDetailDialog(mContext, feedItemList, mAct, position, showDate);
                detailDialog.openImageDialog(feedItemList, position, mContext);
            }
        });
    }

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

    public class CustomViewHolder extends RecyclerView.ViewHolder {
        CardView galleryView;
        ImageView galleryImage;

        public CustomViewHolder(View view) {
            super(view);
            this.galleryView = view.findViewById(R.id.galleryTile);
            this.galleryImage = view.findViewById(R.id.galleryImageView);

        }

    }

    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
                String charString = charSequence.toString().toLowerCase();
                ArrayList<OCVItem> filteredList = new ArrayList<>();
                if (charSequence.equals("")) {
                    feedItemList.clear();
                    filteredList.addAll(feedItemList);
                }
                for (OCVItem message : feedItemListCopy) {
                    if (message.getTitle().toLowerCase().contains(charString) || message.getCategories().contains(charString)) {
                        filteredList.add(message);
                    }
                    else {
                        ArrayList<String> categories = message.getCategories();
                        for (String category : categories) {
                            if (category.toLowerCase().contains(charString)) {
                                filteredList.add(message);
                            }
                        }
                    }

                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = filteredList;


                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                feedItemList = (ArrayList<OCVItem>) filterResults.values;
                notifyDataSetChanged();
            }
        };
    }


}

1 个答案:

答案 0 :(得分:0)

在这个问题上经过很多天,我发现在下面的答案中发布的内容是我正在做的事情的解决方案。

https://stackoverflow.com/a/26575808/3486469