滚动时Android列表项正在更改 [英] Android list items are changing when scrolling

查看:88
本文介绍了滚动时Android列表项正在更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a list that should have each item the same layout except for the first and last item which should have separate layouts. The first item shows its unique layout correctly, but the last one changes when I scroll it in and out of view between the correct one and the layout of the first item. Here is my adapter:

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;
    }
}

推荐答案

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

The problem is ListView is recycling a non-footer row view when it calls getView() for the last item. You need to implement getItemViewType() and getViewTypeCount() in your adapter, to give ListView a hint about what views can be recycled for those items.

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语句来构建行:

Inside of getView() you can call getItemViewType() and use a switch statement to build your rows:

@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
    }
}

这篇关于滚动时Android列表项正在更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆