滚动时,Android列表项会发生变化

时间:2014-10-29 03:55:54

标签: android listview custom-lists

我有一个列表应该让每个项目具有相同的布局,除了应该具有单独布局的第一个和最后一个项目。第一个项目正确显示其独特的布局,但是当我在正确的一个和第一个项目的布局之间滚动进出视图时,最后一个项目会发生变化。这是我的适配器:

public class ListAdapterApp extends ArrayAdapter<App> {

    Context context;
    int layoutResourceId;
    List<App> appList;

    public ListAdapterApp(Context context, int layoutResourceId, List<App> appList) {
        super(context, layoutResourceId, appList);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.appList = appList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        AppHolder holder = null;

        if(row == null)
        {


            holder = new AppHolder();
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            if(position == 0){
                holder.rowLayoutID = R.layout.list_column_appheaderitem;
            } else if(position == appList.size() - 1){
                holder.rowLayoutID = R.layout.list_column_appfooteritem;
            } else {
                holder.rowLayoutID = layoutResourceId;
            }
            row = inflater.inflate(holder.rowLayoutID, parent, false);//holder.packBackground = (LinearLayout)row.findViewById(R.id.packBackground);

            row.setTag(holder);
        }
        else
        {
            holder = (AppHolder)row.getTag();
        }

        App app = appList.get(position);

        return row;
    }

    static class AppHolder
    {
        public int rowLayoutID;
    }
}

4 个答案:

答案 0 :(得分:2)

问题是ListView在为最后一项调用getView()时回收非页脚行视图。您需要在适配器中实施getItemViewType()getViewTypeCount(),以便ListView提示可以为这些项目回收哪些视图。

private static final int VIEW_TYPE_HEADER = 0;
private static final int VIEW_TYPE_FOOTER = 1;
private static final int VIEW_TYPE_DEFAULT = 2;
private static final int VIEW_TYPE_COUNT = 3;

@Override
public int getViewTypeCount() {
    // The total number of row types your adapter supports.
    // This should NEVER change at runtime.
    return VIEW_TYPE_COUNT;
}

@Override
public int getItemViewType(int position) {
    // return a value from zero to (viewTypeCount - 1)
    if (position == 0) {
        return VIEW_TYPE_HEADER;
    } else if (position == getCount() - 1) {
        return VIEW_TYPE_FOOTER;
    }
    return VIEW_TYPE_DEFAULT;
}

getView()内,您可以致电getItemViewType()并使用switch语句构建您的行:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    switch(getItemViewType(position)) {
    case VIEW_TYPE_HEADER:
        return getHeaderView(position, convertView, parent); // TODO implement this
    case VIEW_TYPE_FOOTER:
        return getFooterView(position, convertView, parent); // TODO implement this
    case VIEW_TYPE_DEFAULT:
        return getDefaultView(position, convertView, parent); // TODO implement this
    }
}

答案 1 :(得分:2)

我遇到了同样的问题然后我解决了这个问题,就像不检查if (row==null)然后实现其他删除if else块并将整个代码放入{{1}方法然后运行它你的问题就解决了。

答案 2 :(得分:0)

您应该将if-else position支票放在if(row == null)的正文旁边,如:

        if(row == null)
        {
            holder = new AppHolder();
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();

            row = inflater.inflate(holder.rowLayoutID, parent, false);

            row.setTag(holder);
        }
        else
        {
            holder = (AppHolder)row.getTag();
        }

          if(position == 0){
                holder.rowLayoutID = R.layout.list_column_appheaderitem;
            } else if(position == appList.size() - 1){
                holder.rowLayoutID = R.layout.list_column_appfooteritem;
            } else {
                holder.rowLayoutID = layoutResourceId;
            }

还要在instance of AppHolderconstructor添加Context context; int layoutResourceId; List<App> appList; AppHolder holder; public ListAdapterApp(Context context, int layoutResourceId, List<App> appList) { super(context, layoutResourceId, appList); this.layoutResourceId = layoutResourceId; this.context = context; this.appList = appList; holder = new AppHolder(); }

{{1}}

答案 3 :(得分:0)

我有类似的问题,我解决了this。我遇到了RadioGroup和Radio BUttons的问题,但问题的其余部分与你的相似。