使用listviews的BaseAdapter getView(int position)问题

时间:2013-09-25 13:37:54

标签: android android-layout android-listview android-adapter

我为listviews写了一些适配器。我需要根据属性绘制具有不同布局的那些视图的一些元素。

所以我将两个ArrayList数据和ArrayList属性传递给适配器,并在getView(int position)中检查property.get(position)

问题在于,位置似乎与整个列表无关,而只是部分位置根据当前视图而变化(例如:滚动它)

如何获得所需的效果? 为什么这个问题不会影响数据arraylist?

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

    /**
     * Holder mantiene una reference all'albero delle views per evitare chiamate continue di findiViewById
     */
    ViewHolder holder;

    if (view == null){

        /** si usa inflate solo se è null */
         holder = new ViewHolder();



         if (!lemon.get(position)){
             view = mInflater.inflate(R.layout.contact_layout, null);
             holder.name = (TextView) view.findViewById(R.id.contact_name);
         }else {
             view = mInflater.inflate(R.layout.contact_lemon_layout, null);
             holder.name = (TextView) view.findViewById(R.id.contact_name);
         }

         view.setTag(holder);


    }else {


        holder = (ViewHolder) view.getTag();
    }


    holder.name.setText(names.get(position));

    return view;
}

1 个答案:

答案 0 :(得分:1)

使用getItemViewTypegetViewTypeCount

        @Override
        public int getViewTypeCount() {
            return 2;
        }
        @Override
        public int getItemViewType(int position) {
            return lemon.get(position)?0:1;
        }

您的代码现在可以在getView中使用。

相关问题