最佳答案
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;
}
}
//創(chuàng)建新View,被LayoutManager所調(diào)用
@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;
}
//將數(shù)據(jù)與界面進行綁定的操作
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
Map<String, String> data = dataList.get(position);
viewHolder.itemView.setTag(data);
}
//獲取數(shù)據(jù)的數(shù)量
@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);
}
}
}