RecyclerView多布局加接口回调点击分享

package com.example.umshares;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by 吕楠 on 2017/12/7.
 */

public class Adapter extends RecyclerView.Adapter{
    private ArrayList arr;
    private final static int ITEM_ONE = 1;
    private final static int ITEM_TOW = 2;
    private Onclick mOnclick = null;
    public Adapter(ArrayList arr) {
        this.arr = arr;
    }
    static interface Onclick{
        void OnclickListener(int position);
    }
    public void setOnClickss(Onclick listener){
        this.mOnclick = listener;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view;
        RecyclerView.ViewHolder holder = null;
        if (ITEM_ONE  == viewType){
            view = layoutInflater.inflate(R.layout.itemtow,parent,false);
            holder = new HolderOne(view);
        }else {
            view = layoutInflater.inflate(R.layout.itemone,parent,false);
            holder = new HolderTow(view);
        }

        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof HolderOne){
            ((HolderOne) holder).textView.setText(arr.get(position));
        }else {
            ((HolderTow)holder).textView.setText(arr.get(position));
            ((HolderTow)holder).textView2.setText(arr.get(position));
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnclick.OnclickListener(position);
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        if (position % 2 == 0){
            return ITEM_ONE;
        }else {
            return ITEM_TOW;
        }
    }
    @Override
    public int getItemCount() {
        return arr.size();
    }
    class HolderOne extends RecyclerView.ViewHolder {
        private TextView textView;
        public HolderOne(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.adapter_linear_text);
        }
    }
    class HolderTow extends RecyclerView.ViewHolder {
        private TextView textView;
        private TextView textView2;
        public HolderTow(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.adapter_two_1);
            textView2 = (TextView) itemView.findViewById(R.id.adapter_two_2);
        }
    }
}

你可能感兴趣的:(RecyclerView多布局加接口回调点击分享)