当adapter. notifydatasetchanged()时,android ListView适配器方法getVi



我希望我的listview加载每10项,不希望我的listview重用项。

1。我加载10项并将适配器设置为listview,并在适配器类中像这样调用getViewTypeCount()方法。(当前itemList是有10)

public int getViewTypeCount() {
    return itemList.size();
}

2。我在getViewTypeCount()中调试它返回值10。

3。然后我添加新的10项到itemList(当前在itemList是有20),并调用adapter.notifyDataSetChanged()

4。当我向下滚动我的listview它工作,但当我向上滚动它是错误的(不幸的)

5。我发现了这个问题,因为getViewTypeCount()在adapter.notifyDataSetChanged()之后没有着火,它不返回20(我知道,因为我在getViewTypeCount()中测试返回500,这是工作而不是错误。)

6。我希望我的列表视图加载每10个项目,我不希望我的列表视图重用位置。如何解决?

编辑

这是适配器类

public class ContentListAdapter extends BaseAdapter {
private Context context;
private ArrayList<ContentInfo> Content;
private LayoutInflater Layf;
public ContentListAdapter(Context context, ArrayList<ContentInfo> content){
    this.context = context;
    this.Content = content;
    this.Layf = LayoutInflater.from(context);
}
@Override
public int getCount() {
    return Content.size();
}
@Override
public Object getItem(int position) {
    return Content.get(position);
}
@Override
public long getItemId(int position) {
    return position;
}
@Override
public int getItemViewType(int position) {
    return position;
}
@Override
public int getViewTypeCount() {
    return Content.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = Layf.inflate(R.layout.content_list_item, null);
    }
    ImageView img_ContentImage = (ImageView) convertView.findViewById(R.id.img_ContentImage);
    TextView tv_ContentName = (TextView) convertView.findViewById(R.id.tv_ContentName);
    TextView tv_ContentDate = (TextView) convertView.findViewById(R.id.tv_ContentDate);
    TextView tv_ContentViews = (TextView) convertView.findViewById(R.id.tv_ContentViews);
    if(img_ContentImage != null){
        if(Content.get(position).getPicture().length() > 0)
            new DownloadImageViewTask(img_ContentImage).execute(Content.get(position).getPicture());
    }
    if(tv_ContentName != null)
        tv_ContentName.setText(Content.get(position).getTitle().replace(" "," ").replace("-", "–"));
    if(tv_ContentDate != null) {
        if(Content.get(position).getPublishDate().length() > 0) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                Date dt = sdf.parse(Content.get(position).getPublishDate());
                String year = new SimpleDateFormat("yyyy", new Locale("th", "TH")).format(dt);
                String byear = (Integer.parseInt(year) + 543) + "";
                tv_ContentDate.setText((new SimpleDateFormat("d MMM yyyy", new Locale("th", "TH")).format(dt)).replace(year, byear));
            }catch (Exception e){
            }
        }
    }
    if(tv_ContentViews != null){
        DecimalFormat formatter = new DecimalFormat("#,##0");
        tv_ContentViews.setText(formatter.format(Content.get(position).getViews()));
    }
    return convertView;
}

}

getViewTypeCount() 仅在调用setAdapter时调用

最新更新