SettingsViewAdapter.java 2.45 KB
package com.lijinji.part.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lijinji.part.R;

import java.util.List;

public class SettingsViewAdapter extends RecyclerView.Adapter<SettingsRecyclerHolder> {

    private List<SettingsBean> mList;
    private Context mContext;
    private RecyclerViewAdapter.OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public SettingsViewAdapter(Context context) {
        mContext = context;
    }

    public List<SettingsBean> getList() {
        return mList;
    }

    public void setList(List<SettingsBean> list) {
        mList = list;
    }

    @NonNull
    @Override
    public SettingsRecyclerHolder onCreateViewHolder(@NonNull ViewGroup parent, int position) {
        int parentHeight = parent.getHeight();
        int parentWidth = parent.getWidth();
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_setting,parent,false);
        SettingsRecyclerHolder holder = new SettingsRecyclerHolder(view);
        ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
        layoutParams.height = parentHeight / 3;
        layoutParams.width = parentWidth / 3;
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull SettingsRecyclerHolder recyclerHolder, final int position) {
        SettingsBean menuBean =  mList.get(position);
        recyclerHolder.getIcon().setBackground(menuBean.getMenu());
        recyclerHolder.getTitleView().setText(menuBean.getName());
        recyclerHolder.getMessageView().setText(menuBean.getMessage());
        if (mOnItemClickListener != null) {
            //绑定点击事件
            recyclerHolder.getLinelayout().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //把条目的位置回调回去
                    mOnItemClickListener.onItemClick(position);
                }
            });
        }
    }

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

    public void setOnItemClickListener(RecyclerViewAdapter.OnRecyclerViewItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    public  interface OnRecyclerViewItemClickListener {
        void onItemClick(int position);
    }
}