分段和异质的Recyclerview组合

时间:2018-04-13 11:55:00

标签: android android-recyclerview recycler-adapter heterogeneous

你好,这对我来说似乎是一个非常复杂的布局,如果有人可以一起帮我处理分段和异构的Recyclerview,那对我来说将是非常好的和有用的。

enter image description here

1 个答案:

答案 0 :(得分:0)

我已经实现了类似的截面,但我没有找到我所做的链接。我有代码可能你明白了。我已经采取了2回收视图。一个用于标题,第二个用于描述部分。我甚至为描述部分制作了itemclicklistner。

标题的SectionRecyclerViewAdapter

public class SectionRecyclerViewAdapter extends RecyclerView.Adapter<SectionRecyclerViewAdapter.SectionViewHolder> {


    class SectionViewHolder extends RecyclerView.ViewHolder {
        private TextView header;
        private RecyclerView itemRecyclerView;


        public SectionViewHolder(View itemView) {
            super(itemView);
            header= (TextView) itemView.findViewById(R.id.section_label);
            itemRecyclerView = (RecyclerView)itemView.findViewById(R.id.item_recycler_view);
        }
    }

    private Context context;
    private List<SectionModel> sectionModelArrayList;
    private LabelDataSearch labelDataSearch;

    public SectionRecyclerViewAdapter(Context applicationContext, List<SectionModel> dataLists, LabelDataSearch labelDataSearchClass) {
        this.context = applicationContext;
        this.sectionModelArrayList = dataLists;
        this.labelDataSearch=labelDataSearchClass;

    }

    @Override
    public SectionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.section_custom_row_layout, parent, false);
        return new SectionViewHolder(view);
    }

    @Override
    public void onBindViewHolder(SectionViewHolder holder, int position) {
        final SectionModel sectionModel = sectionModelArrayList.get(position);
        if(sectionModel.itemArrayList.size()>0) {
            holder.sectionLabel.setVisibility(View.VISIBLE);
            holder.sectionLabel.setText(sectionModel.sectionLabel);
        }
        else {
            holder.sectionLabel.setVisibility(View.VISIBLE);
            holder.sectionLabel.setText(sectionModel.sectionLabel+"\n No Results");
        }

        //recycler view for items
        holder.itemRecyclerView.setHasFixedSize(true);
        holder.itemRecyclerView.setNestedScrollingEnabled(false);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        holder.itemRecyclerView.setLayoutManager(linearLayoutManager);

        ImageListAdapter adapter = new ImageListAdapter(context, sectionModel.itemArrayList,new ImageListAdapter.OnItemClickListener(){
            @Override
            public void onItemClick(LabelDataList item, int position) {
                labelDataSearch.performAction(item.pageurl);

            }
        });
        holder.itemRecyclerView.setAdapter(adapter);

    }

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


}

用于描述的ImageListAdapter

public class ImageListAdapter extends RecyclerView.Adapter<ImageListAdapter.MyViewHolder> {

    List<LabelDataList> dataLists;
    private OnItemClickListener listener;
    private Context ctx;
    public static int sCorner = 15;
    public static int sMargin = 2;
    public static int sBorder = 10;
    public static String sColor = "#7D9067";

    public ImageListAdapter(Context context, ArrayList<LabelDataList> itemArrayList, OnItemClickListener onItemClickListener) {
        this.dataLists = itemArrayList;
        this.listener = onItemClickListener;
        this.ctx = context;
    }

    public interface OnItemClickListener {
        void onItemClick(LabelDataList item, int position);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_lebel, parent, false);
        return new MyViewHolder(v);
    }


    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.bind(dataLists.get(position), listener, position);
        final LabelDataList labelData = dataLists.get(position);
       // Your views include check box and text view or radio buttons
    }

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

    /**
     * View holder class
     */
    public class MyViewHolder extends RecyclerView.ViewHolder {

        private final TextView tvCategory;
        public TextView tvLabel;
        public RelativeLayout relTopLayout;
        public ImageView imgProduct;

        public MyViewHolder(View child) {
            super(child);
            tvLabel = (TextView) child.findViewById(R.id.tv_label);
            tvCategory = (TextView) child.findViewById(R.id.tv_category);
            imgProduct = (ImageView) child.findViewById(R.id.img_product);
            //    relTopLayout = (RelativeLayout) child.findViewById(R.id.rel_top_layout);
        }

        public void bind(final LabelDataList item, final OnItemClickListener listener, final int position) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onItemClick(item, position);
                }
            });
        }
    }
}

希望对你有所帮助。