-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainAdapter.java
69 lines (68 loc) · 1.93 KB
/
MainAdapter.java
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
package com.example.msi.myapplication;
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.List;
/**
* Created by Rowandjj on 2015/3/25.
*/
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder>
{
private List<String> mDataSet = null;
private OnItemClickListener mListener;
public MainAdapter(List<String> dataSet)
{
this.mDataSet = dataSet;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
{
final View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item,viewGroup,false);
itemView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(mListener != null)
mListener.onItemClick(v, (String) itemView.getTag());
}
});
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i)
{
String data = mDataSet.get(i);
viewHolder.bindData(data);
viewHolder.itemView.setTag(data);
}
@Override
public int getItemCount()
{
return mDataSet.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder
{
private TextView tv;
public ViewHolder(View itemView)
{
super(itemView);
tv = (TextView) itemView.findViewById(R.id.info_text);
}
public void bindData(String s)
{
if(s != null)
tv.setText(s);
}
}
public interface OnItemClickListener
{
public void onItemClick(View view,String data);
}
public void setOnItemClickListener(OnItemClickListener listener)
{
this.mListener = listener;
}
}