private List<ContentResponseBean.DataBean> mDatas;
private Context mContext;
public ContentAdapter(Context context) {
this.mContext = context;
mDatas = new ArrayList<>();
}
public ContentAdapter(Context mContext, List<ContentResponseBean.DataBean> datas) {
this.mContext = mContext;
mDatas = datas;
}
//外部调用 更新数据
public void setmDatas(List<ContentResponseBean.DataBean> mDatas) {
this.mDatas = mDatas;
notifyDataSetChanged();
}
@Override public int getCount() {
return mDatas.size();
}
@Override public ContentResponseBean.DataBean getItem(int position) {
return mDatas.get(position);
}
@Override public long getItemId(int position) {
return position;
}
//parent = ListView
@Override public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh;
//复用,不重复创建View
if(convertView == null) {
//convertView = View.inflate(mContext, R.layout.item, null);
convertView = LayoutInflater.from(mContext).inflate(R.layout.item, parent, false);
vh = new ViewHolder();
vh.mIcon = convertView.findViewById(R.id.iv_icon);
vh.mText = convertView.findViewById(R.id.tv_text);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
//TODO 设置图片
//设置 数据
vh.mText.setText(getItem(position).getTitle());
return convertView;
}
class ViewHolder {
private ImageView mIcon;
private TextView mText;
}
ListView适配器
最新推荐文章于 2022-07-22 13:32:56 发布