Manager的RecyclerView和具有不同viewHol

Manager的RecyclerView和具有不同viewHol

本文介绍了带有GridLayoutManager的RecyclerView和具有不同viewHolder的第一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用两行GridLayoutManager创建一个recyclerView,并且第一个元素要比其余元素大.结果应如下所示:

I need to create a recyclerView with a GridLayoutManager on two lines, and the first element to be bigger than the rest. The result should look like this:

我设法做到了,但是以一种非常规的方式.在我的recyclerView适配器中,我为第一个元素使用了一个不同的viewHolder,即一个更大的元素.那将是一个很好的解决方案,但是第二个元素将低于第一个元素.因此,我给了一个技巧,使recyclerView的固定高度与第一个元素相同,这样第一个元素和第二个元素将重叠,而我只是简单地使第二个元素的可见性消失了.

I managed to achieve this, but in an unconventional way. In my recyclerView adapter I use a different viewHolder for the first element, a bigger one. That would have been a great solution, but the second element would have gone below the first element. So I made a trick of giving recyclerView a fixed height same as the first element, that way the first element and the second would overlap and I would simply make the visibility of the second to gone.

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    switch (viewType) {
        case 0:
            final View view = inflater.inflate(R.layout.big_item, parent, false);
            return new BigViewHolder(view);
        case 2:
            final View view2 = inflater.inflate(R.layout.normal_item, parent, false);
            return new NormalViewHolder(view2);
        default:
            return null;
    }
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (position == 1) {
        holder.itemView.setVisibility(View.GONE);
    }}

@Override
public int getItemViewType(int position) {
    if (position == 0) {
        return 0;
    } else return 2;
}

但是我并不特别喜欢这种方法.有人对此有更好的主意吗?

But I don't particularly like this approach. Does anyone have a better idea on this?

推荐答案

经过一番挖掘和咨询,我得出了很好的结果.我将其发布在下面:

After some digging and counseling, I came to a pretty nice result. I will post it below:

首先,适配器应该看起来像我在问题中提到的那样,而没有隐藏第二个元素:

First, the adapter should look like I mentioned in the question, without the hiding of the second element:

 public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int TYPE_FIRST_ITEM = 0;
    public static final int TYPE_ITEM = 1;

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_FIRST_ITEM:
                final View view = inflater.inflate(R.layout.big_item, parent, false);
                return new BigViewHolder(view);
            case TYPE_ITEM:
                final View view2 = inflater.inflate(R.layout.normal_item, parent, false);
                return new NormalViewHolder(view2);
            default:
                return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (holder.getItemViewType()) {
            case TYPE_FIRST_ITEM:
                BigViewHolder bigViewHolder = (BigViewHolder) holder;
                // Do what you need for the first item
                break;
            case TYPE_ITEM:
                NormalViewHolder normalViewHolder = (NormalViewHolder) holder;
                // Do what you for the other items
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_FIRST_ITEM;
        } else return TYPE_ITEM;
    }

    final class NormalViewHolder extends RecyclerView.ViewHolder {
        public NormalViewHolder(View itemView) {
            super(itemView);
        }
        // find your views here
    }

    final class BigViewHolder extends RecyclerView.ViewHolder {
        public BigViewHolder(View itemView) {
            super(itemView);
        }
        // find your views here
    }
}

第二,布局管理器应实现一个侦听器,以指定在特定位置应有多少跨度(行):

Second, the layout manager should implement a listener to specify how many spans (rows) should be at a specific position:

RecyclerView mRecyclerView = view.findViewById(R.id.my_recycler_view);
        MyAdapter mAdapter = new MyAdapter();
        GridLayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2, LinearLayoutManager.HORIZONTAL, false);
        mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                switch(mAdapter.getItemViewType(position)){
                    case MyAdapter.TYPE_FIRST_ITEM:
                        return 2;
                    case MyAdapter.TYPE_ITEM:
                        return 1;
                    default:
                        return -1;
                }
            }
        });
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(mLayoutManager);

希望它也会对其他人有帮助.谢谢迈克M. 的支持.

Hope it will help someone else too. Thank you Mike M. for your support.

这篇关于带有GridLayoutManager的RecyclerView和具有不同viewHolder的第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:01