【问题标题】:Trouble setting custom view in my ListAdapter在我的 ListAdapter 中设置自定义视图时遇到问题
【发布时间】:2015-01-23 18:11:18
【问题描述】:

我有一个包含各种元素的列表视图,并且更新了很多次。 我想在一些随机位置之后添加一条分隔线,例如在第 3 个元素、第 7 个元素、第 17 个元素、第 19 个元素等之后。

所以我设计了一个包含分隔符的 xml 文件,并使用自定义 SimpleAdapter 在这些位置上填充它。但是,当我运行我的代码时,分隔符被添加到奇怪的位置,使我的列表看起来太难看,有时会给出Unfortunately, yourApp has stopped 错误

我以前从未使用过自定义适配器,也不知道我做错了什么。

这是我的简单代码,我已经添加了 cmets 进行解释,

public class SpecialAdapter extends SimpleAdapter {

    Context context;
    private LayoutInflater mInflater;


    public SpecialAdapter(Context context, ArrayList<HashMap<String, Object>> newsList, int resource,String[] from, int[] to) {
        super(context, newsList, resource, from, to);

        this.context = context;

        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = super.getView(position, convertView, parent);
        // TestApp.endPos holds the positions (it keep changing) after which i want to add                
        // separator
        if (position == TestApp.endPos){
            /* my Arraylist is of type HashMap<String, Object>
             * I have added an empty element and add the seperator view in                  
             * it.
             */
            TestApp.arrayList.add(new HashMap<String, Object>());
            // heads is an xml file of my separator
            view = mInflater.inflate(R.layout.heads, null);
        }

        return view;
    }

}

谁能帮助我如何简化我的问题以及我做错了什么?

【问题讨论】:

    标签: android listview android-listview simpleadapter


    【解决方案1】:

    super.getView 一次膨胀尽可能多的视图。稍后它将重新使用它们。

    因为您稍后会扩充自己的布局,所以它会缓存在顶部。最后super.getView 开始在随机位置返回您的自定义视图。

    要克服这个问题,您需要告诉适配器有两种类型的视图,以便它分别缓存它们。

    以下是示例方法(未经测试但应该可以使用):

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == TestApp.endPos) {
            // Handle separator views manually
            TestApp.arrayList.add(new HashMap<String, Object>());
    
            // If separator is not yet cached, create the view
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.heads, null);
            }
        } else {
            // Normal views are handled by the adapter
            convertView = super.getView(position, convertView, parent);
        }
    
        return convertView;
    }
    
    @Override
    public int getViewTypeCount() {
        // There are 2 types: separator and normal
        return 2;
    }
    
    @Override
    public int getItemViewType(int position) {
        if (position == TestApp.endPos) {
            // Separator view
            return 0;
        } else {
            // Normal view
            return 1;
        }
    }
    

    注意:有 2 种类型,意味着 2 种类型的视图将被缓存和重复使用。

    【讨论】:

    • 更好的解决方案!
    【解决方案2】:

    在您的 getView 方法中,您应该确定是否需要带有 seperator 的视图,然后为该项目扩展该视图:

    if (view == null) {
          if (showSeperatorItem)
              view = _activity.getLayoutInflater().inflate(R.layout.item__row_layout_with_seperator, parent, false);
          else
              view = _activity.getLayoutInflater().inflate(R.layout.item__row_layout, parent, false);
    }
    

    【讨论】:

    • 仍然会失败,除非每次调用getView 时都膨胀,否则效率会非常低。
    • @user3249477 同意,我不知道您在上面的答案中发布了getItemViewType。每天都是上学日。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    • 2014-08-21
    • 1970-01-01
    相关资源
    最近更新 更多