android使用同一个RecyclerView实现两种不同Item布局

代码:

package com.inpor.fastmeetingcloud.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.inpor.fastmeetingcloud.R;
import com.inpor.fastmeetingcloud.base.BaseRecyclerAdapter;
import com.inpor.log.Logger;
import com.inpor.manager.beans.CompanyUserInfo;
import com.inpor.manager.beans.DepartmentResultDto;
import com.inpor.manager.model.Instantmeeting.InstantMeetingModel;
import com.inpor.onlinecall.bean.OnlineUserInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ContactAdapter extends RecyclerView.Adapter {
    public static final int ITEM_USER_INFO = 0x0030;
    public static final int ITEM_DEPARTMENT_INFO =  0x0031;
    private Context context;
    private List content = new ArrayList<>();
    private OnClickListener onClickListener;

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param notGroupedCompanyUserInfos 数据
     */
    public ContactAdapter(Context context, List notGroupedCompanyUserInfos,
                          List subDepartments) {
        Log.d("RoomListActivity", "ContactAdapter已经执行");
        this.context = context;
        content.addAll(notGroupedCompanyUserInfos);
        content.addAll(subDepartments);
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    @Override
    public int getItemViewType(int position) {
        if (content.get(position) instanceof CompanyUserInfo) {
            return ITEM_USER_INFO ;
        } else {
            return ITEM_DEPARTMENT_INFO;
        }
    }

    public void updateData(List subDepartments, List notGroupedCompanyUserInfos ) {
         this.content.clear();
         this.content.addAll(notGroupedCompanyUserInfos);
         this.content.addAll(subDepartments);
    }


    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == ITEM_USER_INFO) {
            return new ViewHolderAboutStaff(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_staff, parent, false));
        } else {
            return new ViewHolderAboutDepartment(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_department, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        holder.itemView.setTag(position);  // 每个item绑定一个position位置,后面再点击的时候使用
        if (getItemViewType(position) == ITEM_USER_INFO) {
            onBindStaffViewHolder(holder, position);
        } else {
            onBindDepartmentViewHolder(holder, position);
        }
    }

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

    /**
     * 绑定企业员工信息
     */
    public void onBindStaffViewHolder(ViewHolder viewHolder, int position) {
        ViewHolderAboutStaff holder = (ViewHolderAboutStaff) viewHolder;
        holder.contactsStaffName.setText(((CompanyUserInfo)content.get(position)).getDisplayName());
    }

    /**
     * 绑定企业部门信息
     */
    public void onBindDepartmentViewHolder(ViewHolder viewHolder, int position) {
        ViewHolderAboutDepartment holder = (ViewHolderAboutDepartment) viewHolder;
        holder.contactsDepartmentName.setText(((DepartmentResultDto.SubDepartments)content.get(position)).getDepName());
    }


    class ViewHolderAboutStaff extends RecyclerView.ViewHolder implements View.OnClickListener {
        @BindView(R.id.contacts_staff_name)
        TextView contactsStaffName;
        @BindView(R.id.contacts_staff_status)
        ImageView contactsStaffStatus;
        @BindView(R.id.item_staff_ll)
        LinearLayout itemStaff;
        View view;

        public ViewHolderAboutStaff(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.view = itemView;
            this.itemStaff.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            int position = (Integer) this.view.getTag();
            if (onClickListener != null) {
                onClickListener.OnClick(content.get(position), ContactAdapter.this.getItemViewType(position));
            }
        }
    }

    class ViewHolderAboutDepartment extends RecyclerView.ViewHolder implements View.OnClickListener {
        @BindView(R.id.contacts_department_name)
        TextView contactsDepartmentName;
        @BindView(R.id.contacts_department_numbers)
        TextView getContactsDepartmentNumbers;
        @BindView(R.id.item_department_ll)
        LinearLayout itemDepartment;
        View view;

        public ViewHolderAboutDepartment(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.view = itemView;
            this.view.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            int position = (Integer) this.view.getTag();
            if (onClickListener != null) {
                onClickListener.OnClick(content.get(position), ContactAdapter.this.getItemViewType(position));
            }
        }
    }

    public interface OnClickListener{
        void OnClick(Object position, int type);
    }
}

 
  

实现效果:

android使用同一个RecyclerView实现两种不同Item布局_第1张图片

你可能感兴趣的:(Android,RecyclerView,item)