重:每个ListItem是属于ListItem自己的,不能够放到ViewHolder中,而是数据源每项的.
package com.yongway.school.adapter; import android.content.Context; import android.graphics.Color; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import com.readystatesoftware.viewbadger.BadgeView; import com.yongway.school.R; import com.yongway.school.entity.MainItemBean; import java.util.List; public class MainListItemAdapter extends BaseAdapter { private Context context; private List<MainItemBean> datas; private LayoutInflater inflater; public MainListItemAdapter(Context context, List<MainItemBean> datas) { this.context = context; this.datas=datas; inflater= LayoutInflater.from(context); } @Override public int getCount() { return datas.size(); } @Override public Object getItem(int i) { return datas.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder holder = null; if(null == view){ view = inflater.inflate(R.layout.list_item_main,viewGroup,false); holder = new ViewHolder(); holder.layout = (RelativeLayout) view.findViewById(R.id.list_item_rel); holder.textViewTile = (TextView)view.findViewById(R.id.li_item_title); holder.textViewContent = (TextView)view.findViewById(R.id.li_item_content); holder.textViewTime = (TextView)view.findViewById(R.id.li_item_time); view.setTag(holder); }else{ holder = (ViewHolder) view.getTag(); } MainItemBean bean = datas.get(i); if(!bean.isRead()){ if(null == bean.getBadgeView()){ BadgeView badgeView = new BadgeView(context, holder.layout); badgeView.setText("New!"); badgeView.setTextColor(Color.BLUE); badgeView.setBadgeBackgroundColor(Color.YELLOW); badgeView.setTextSize(10); bean.setBadgeView(badgeView); bean.getBadgeView().show(); } }else{ if(null != bean.getBadgeView()){ if(bean.getBadgeView().isShown()){ bean.getBadgeView().hide(); } } } holder.textViewTile.setText(bean.getTitle()); holder.textViewContent.setText(bean.getContent()); holder.textViewTime.setText(bean.getTime()); return view; } private class ViewHolder{ private RelativeLayout layout; private ImageView imageView; private TextView textViewTile; private TextView textViewContent; private TextView textViewFee; private TextView textViewTime; private TextView textViewPhone; } public List<MainItemBean> getDatas() { return datas; } public void setDatas(List<MainItemBean> datas) { this.datas = datas; } }
原文:http://www.cnblogs.com/ganchuanpu/p/7875204.html