0

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

获取视图方法:

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

如何解决可见性或如何解决此问题.. 提前致谢

4

1 回答 1

1

它看起来像是prevBrand某种全局变量。当然,Android 不应该按顺序创建视图。如果您滚动到顶部,将更快地创建更高位置的视图。所以你需要得到 prevBrand 这样的东西

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

也请不要调用notifyDataSetChanged()getView 方法

于 2013-06-04T11:43:01.710 回答