我使用listfragment和自定义适配器构建一个列表!我希望第25行填充不同的视图!并且我的代码在第一次显示视图时工作,当向上滚动列表并排出屏幕时获取FC!如果我评论这一行==> if(row == null){代码工作正常但没问题但是,列表中有很多项目,列表很慢,因为列表没有优化
public class MyListAdapter extends ArrayAdapter{
Context myContext;
private ArrayList mList;
public MyListAdapter(Context context, int textViewResourceId,ArrayList list) {
super(context, textViewResourceId, list);
myContext = context;
list=this.mList;
}
@Override
public View getView( final int position, View convertView, ViewGroup parent) {
View row=convertView;
if(row == null){
if(position!=20)
{
LayoutInflater inflater = (LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row, parent, false);
ViewHolder holder = new ViewHolder();
// holders
holder.txt_name = (TextView)row.findViewById(R.id.txt_name);
holder.txt_sms = (TextView)row.findViewById(R.id.txt_sms);
holder.txt_vote = (TextView)row.findViewById(R.id.txt_vote);
holder.btn_favorite=(ImageView)row.findViewById(R.id.btn_favorite);
holder.btn_send=(Button)row.findViewById(R.id.btn_send);
row.setTag(holder);
}
else{
LayoutInflater inflater = (LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row2, parent, false);
}
}
if(position!=20)
{
ViewHolder holder = (ViewHolder)row.getTag();
holder.txt_sms.setText(mysms.get(position).gettext());
holder.txt_name.setText(mysms.get(position).getname());
holder.txt_vote.setText(mysms.get(position).getVotenum()+"");
holder.btn_favorite.setImageResource(R.drawable.icon);
holder.btn_send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
}
});
}
return row;
}
}
static class ViewHolder {
TextView txt_sms;
TextView txt_name;
TextView txt_vote;
ImageView btn_favorite;
Button btn_send;
}