如何修复android中列表视图的回收

时间:2013-06-04 11:26:24

标签: android listview scroll android-arrayadapter

我有一个包含多行的自定义列表视图。每行都有一个标题作为标题,上面有一些名称。当布局上的名称对于下一行相同时,我将此布局可见性设置为GONE。否则将可见性设置为VISIBLE。第一次列表视图加载其工作正常。但是当我滚动列表视图时出现问题,VISIBILITY会更改为每个滚动。

GetView方法:

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

    if (convertView == null) {

        LayoutInflater inflator = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflator.inflate(R.layout.product_item_details, null);

        viewHolder = new ViewHolder();

        viewHolder.imgBtnLv=(ImageView) convertView.findViewById(R.id.imgBtnLv);
        viewHolder.categBar=(LinearLayout) convertView.findViewById(R.id.categBar);
        viewHolder.tvTitleCateg=(TextView) convertView.findViewById(R.id.tvTitleCateg);
        viewHolder.item_detail_layout=(RelativeLayout) convertView.findViewById(R.id.item_detail_layout);
        viewHolder.titlebar2=(RelativeLayout) convertView.findViewById(R.id.titlebar2);


        convertView.setTag(viewHolder);
    }
    else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
        if (position == 0) {
        ProductsActivity p=(ProductsActivity) mContext;
         System.out.println("edit text value >>>-= "+p.edtSearch.getText().toString()); 
         String searchText=p.edtSearch.getText().toString();
         if(searchText==null||searchText.equalsIgnoreCase("")){
             viewHolder.titlebar2.setBackgroundColor(Color.parseColor("#e9ecfa"));
         }else{

             viewHolder.titlebar2.setBackgroundColor(Color.CYAN);

          }

    } else {
        // set color back to original color
        viewHolder.titlebar2.setBackgroundColor(Color.parseColor("#e9ecfa"));
    }





    viewHolder.tvTitleCateg.setText(creditItems.get(position).getBrand());
    viewHolder.tvPrice.setText(creditItems.get(position).getPrice());
    viewHolder.tvQuantity.setText(creditItems.get(position).getInstock());
    viewHolder.tvDiscount.setText(creditItems.get(position).getDiscount());

    String currBrand =  creditItems.get(position).getBrand();

    viewHolder.categBar.setVisibility(View.VISIBLE);

    notifyDataSetChanged();         

    if(prevBrand.equalsIgnoreCase(currBrand)){

        viewHolder.categBar.setVisibility(View.GONE);
        System.out.println("view Gone "+position);
        notifyDataSetChanged();
    }else{
        viewHolder.categBar.setVisibility(View.VISIBLE);
    System.out.println("view Visible"+position);
        notifyDataSetChanged();
    }


    prevBrand= currBrand;

当我滚动列表视图时,在log cat中消息是

6-04 16:10:44.090: I/System.out(12883): view Visible0
06-04 16:10:44.113: I/System.out(12883): view Visible1
06-04 16:10:44.129: I/System.out(12883): view Gone 2
06-04 16:10:44.145: I/System.out(12883): view Visible3
06-04 16:10:44.160: I/System.out(12883): view Gone 4
06-04 16:10:44.176: I/System.out(12883): view Gone 5
06-04 16:10:44.192: I/System.out(12883): view Gone 6
06-04 16:10:44.207: I/System.out(12883): view Gone 7
06-04 16:10:44.223: I/System.out(12883): view Visible8
06-04 16:10:44.238: I/System.out(12883): view Visible9
06-04 16:10:52.270: I/System.out(12883): view Gone 10
06-04 16:10:52.285: I/System.out(12883): edit text value >>>-= 
06-04 16:10:52.285: I/System.out(12883): view Visible0
06-04 16:10:52.293: I/System.out(12883): view Visible1
06-04 16:10:52.301: I/System.out(12883): view Gone 2
06-04 16:10:52.301: I/System.out(12883): view Visible3
06-04 16:10:52.309: I/System.out(12883): view Gone 4
06-04 16:10:52.309: I/System.out(12883): view Gone 5
06-04 16:10:52.317: I/System.out(12883): view Gone 6
06-04 16:10:52.317: I/System.out(12883): view Gone 7
06-04 16:10:52.324: I/System.out(12883): view Visible8
06-04 16:10:52.465: I/System.out(12883): view Visible1
06-04 16:10:52.973: I/System.out(12883): view Visible0
06-04 16:10:52.981: I/System.out(12883): view Visible2
06-04 16:10:52.988: I/System.out(12883): view Visible3

如何修复可见性或如何解决此问题..提前感谢

1 个答案:

答案 0 :(得分:1)

看起来prevBrand是某种全局变量。确保Android不应按顺序创建视图。如果滚动到顶部,将立即创建更高位置的视图。 所以你需要获得类似这样的prevBrand

String prevBrand =  position > 0? creditItems.get(position - 1).getBrand(): "";

另请不要在getView方法中调用notifyDataSetChanged()