最佳答案
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.lingnet.app.tv.R;
import com.zhy.autolayout.utils.AutoUtils;
import java.util.linkedList;
import java.util.List;
import java.util.Map;
import butterknife.ButterKnife;
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder> {
private List<Map<String, String>> dataList = null;
Context context;
public MessageAdapter(Context context) {
this.context = context;
}
public void notifyDataSetChanged(List<Map<String, String>> dataList) {
if (dataList == null) {
this.dataList = new linkedList<>();
} else {
if (this.dataList == null) {
this.dataList = new linkedList<>();
}
this.dataList.addAll(dataList);
}
super.notifyDataSetChanged();
}
public void addData(List<Map<String, String>> dataList) {
if (dataList != null) {
this.dataList.addAll(dataList);
} else {
this.dataList = dataList;
}
}
//创建新View,被LayoutManager所调用
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout
.list_item_message, viewGroup, false);
ViewHolder vh = new ViewHolder(view);
return vh;
}
//将数据与界面进行绑定的操作
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
Map<String, String> data = dataList.get(position);
viewHolder.itemView.setTag(data);
}
//获取数据的数量
@Override
public int getItemCount() {
int count = 0;
if (dataList != null) {
count = dataList.size();
}
return count;
}
//自定义的ViewHolder,持有每个Item的的所有界面元素
public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
AutoUtils.autoSize(view);
}
}
}