RecyclerViewAdapter.java
2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.lijinji.call.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.call.R;
import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerHolder> {
private List<MenuBean> mList;
private Context mContext;
private OnRecyclerViewItemClickListener mOnItemClickListener = null;
public RecyclerViewAdapter(Context context) {
mContext = context;
}
public List<MenuBean> getList() {
return mList;
}
public void setList(List<MenuBean> list) {
mList = list;
}
@NonNull
@Override
public RecyclerHolder onCreateViewHolder(@NonNull ViewGroup parent, int position) {
int parentHeight = parent.getHeight();
int parentWidth = parent.getWidth();
View view = LayoutInflater.from(mContext).inflate(R.layout.layout_content,parent,false);
RecyclerHolder holder = new RecyclerHolder(view);
ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
layoutParams.height = parentHeight / 3;
layoutParams.width = parentWidth / 3;
return holder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerHolder recyclerHolder, final int position) {
MenuBean menuBean = mList.get(position);
recyclerHolder.getIcon().setBackground(menuBean.getMenu());
recyclerHolder.getTextView().setText(menuBean.getName());
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(OnRecyclerViewItemClickListener listener) {
mOnItemClickListener = listener;
}
public interface OnRecyclerViewItemClickListener {
void onItemClick(int position);
}
}