我有一個Custom adapter
延伸BaseAdapter
,一切正常,除了getView
方法中的項目返回位置看起來不正確。列表後的first scroll
它給了我最後顯示的項目的索引。獲取錯誤的項目位置ListView
首先滾動從上到下:0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - 11 - 12
首先滾動自下而上:12 - 11 - 10 - 9 - 8 - 7 - 6
第二滾動從上到下:6 - 7 - 8 - 9 - 10 - 11 - 12
,開始是相當隨機的。
這是我的適配器代碼:
public abstract class FourComponentsAdapter extends BaseAdapter {
private UIViewImpl context;
private ArrayList<FourComponentsListItem> listItems;
private int layoutId;
private int itemPosition;
public FourComponentsAdapter (Context context, ArrayList<FourComponentsListItem> listItems, int layouId){
this.context = context;
this.listItems = listItems;
this.layoutId = layouId;
}
@Override
public int getCount() {
return listItems.size();
}
@Override
public Object getItem (int position) {
return listItems.get (position);
}
@Override
public long getItemId (int position) {
return position;
}
@Override
public View getView (int position, View convertView, ViewGroup parent) {
View rowView = convertView;
ViewHolder viewHolder;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate (layoutId, null);
viewHolder = new ViewHolder();
UIImage thumbnail = findThumbnail (rowView, viewHolder);
UILabel title = findTitle (rowView, viewHolder);
UILabel subTitle = findSubTitle (rowView, viewHolder);
UIIcon actions = findIconAction (rowView, viewHolder);
if (thumbnail != null) {
viewHolder.iconThumbnail = thumbnail;
}
if (title != null) {
viewHolder.title = title;
}
if (subTitle != null) {
viewHolder.subTitle = subTitle;
}
if (actions != null) {
viewHolder.iconAction = actions;
}
rowView.setTag (viewHolder);
} else {
viewHolder = (ViewHolder)rowView.getTag();
}
itemPosition = position;
if (viewHolder.iconThumbnail != null) {
// Image or text
viewHolder.iconThumbnail.setOnClickListener (new OnClickListener() {
@Override
public void onClick (View view) {
onThumbnailClick (view, itemPosition);
}
});
}
if (viewHolder.title != null) {
viewHolder.title.setText (listItems.get (position).getTitle());
viewHolder.title.setOnClickListener (new OnClickListener() {
@Override
public void onClick (View view) {
onTitleClick (view, itemPosition);
}
});
}
if (viewHolder.subTitle != null) {
viewHolder.subTitle.setText (listItems.get (position).getSubTitle());
}
if (viewHolder.iconAction != null) {
viewHolder.iconAction.setText (listItems.get (position).getIconAction());
viewHolder.iconAction.setTag (String.valueOf (position));
viewHolder.iconAction.setOnClickListener (new OnClickListener() {
@Override
public void onClick (View view) {
onActionClick (view, itemPosition);
}
});
}
System.out.println ("POSITION ::::: " + itemPosition);
return rowView;
}
protected abstract UIImage findThumbnail (View rowView, ViewHolder viewHolder);
protected abstract UILabel findTitle (View rowView, ViewHolder viewHolder);
protected abstract UILabel findSubTitle (View rowView, ViewHolder viewHolder);
protected abstract UIIcon findIconAction (View rowView, ViewHolder viewHolder);
protected abstract void onThumbnailClick (View view, int position);
protected abstract void onTitleClick (View view, int position);
protected abstract void onActionClick (View view, int position);
public static class ViewHolder {
public UIImage iconThumbnail;
public UILabel title;
public UILabel subTitle;
public UIIcon iconAction;
}
}
我的動態代碼:
private ArrayList<FourComponentsListItem> listItems = new ArrayList<FourComponentsListItem>();
....
UIListView listView = (UIListView) view.findViewById (R.id.list));
listView.setAdapter (
new FourComponentsAdapter (UserActivityView.this, listItems, listId) {
@Override
protected UIImage findThumbnail (View rowView, ViewHolder viewHolder) {
return (UIImage)rowView.findViewById (R.id.thumbnail);
}
@Override
protected UILabel findTitle (View rowView, ViewHolder viewHolder) {
return (UILabel)rowView.findViewById (R.id.name);
}
@Override
protected UILabel findSubTitle (View rowView, ViewHolder viewHolder) {
return (UILabel)rowView.findViewById (R.id.subName);
}
@Override
protected UIIcon findIconAction (View rowView, ViewHolder viewHolder) {
return (UIIcon)rowView.findViewById (R.id.actions);
}
@Override
protected void onThumbnailClick (View view, int position) {
}
@Override
protected void onTitleClick (View view, int position) {
}
@Override
protected void onActionClick (View view, int position) {
// Code here
}
);
我缺少的東西?謝謝。
您是否有6個可見項? 僅當項目變得可見時調用'getView'。所以我沒有看到任何問題... – Suvitruf
你添加了任何headerview到你的列表視圖 –
顯示你所說的「漂亮隨機」 – tana