如何停止列表回收在列表滚动视图中,在自定义游标适配器android中

时间:2013-02-07 05:42:26

标签: android listview scroll

我创建了一个按类别划分的列表,用于使用View Holder在列表视图中传递视图(使用简单游标适配器)。因此,在我的自定义简单光标适配器中,我可以在列表的每个项目上设置单个条形图的可见性。 但是滚动时,该栏的可见性对于列表项不固定,它会随机上下变化。检查我在此处发布的代码。

当我从普通活动调用我的自定义Cursor列表适配器的构造函数时,只有我传递所有参数(视图,光标),而我只是在绑定视图而不是newview。

请帮助我。

活动是:

 catCount = cursorCat.getCount();
int i=0;
cursorCat.moveToFirst();
do {
categ  =  cursorCat.getString(cursorCat.getColumnIndex("category_Name")); 
cursorCatItems = db.rawQuery("SELECT _id, product_code, product_name, product_category, in_stock, price FROM ProductDetails WHERE _id || ' ' || product_name || product_code LIKE ? ",
new String[] { "%" +searchValue+ "%"});

adapter = new MforceAdapterCat(this, R.layout.item_details,
cursorCatItems, new String[] { "product_code", "product_name","product_category","in_stock","price" }, new int[] {
R.id.tvCode, R.id.tvItemName,R.id.tvItemType,R.id.tvQuantity,R.id.tvPrice });

listView.setAdapter(adapter);

i++;
} while (cursorCat.moveToNext() && i<catCount-1);              

cursorCat.close();

// down here is the code for list view items

      @SuppressLint("NewApi")
      public class MforceAdapterCat extends SimpleCursorAdapter {
        String prevCat="";
    int count=0;
    private final String TAG = this.getClass().getSimpleName();

    protected ListAdapter adapter;

    public MforceAdapterCat(Context context, int layout, Cursor c,
            String[] from, int[] to, int flags) {
        super(context, layout, c, from, to, flags);
        // TODO Auto-generated constructor stub
    }


@SuppressWarnings("deprecation")
public MforceAdapterCat(Context context, int layout, Cursor c,
        String[] from, int[] to) {
    super(context, layout, c, from, to);
    // TODO Auto-generated constructor stub
}


@Override
public int getItemViewType(int position) {
    // TODO Auto-generated method stub
    return super.getItemViewType(position);
}


   @Override
    public void bindView(View view, Context context, Cursor cursor) {
        super.bindView(view, context, cursor);

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

        holder = new ViewHolder();

            holder.tvCode = (TextView) view.findViewById(R.id.tvCode);
            holder.tvItemName = (TextView) view.findViewById(R.id.tvItemName);
            holder.tvItemType = (TextView) view.findViewById(R.id.tvItemType);
            holder.tvPrice = (TextView) view.findViewById(R.id.tvPrice);
            holder.tvQuantity = (TextView) view.findViewById(R.id.tvQuantity);
            holder.imgBtnLv=(ImageButton) view.findViewById(R.id.imgBtnLv);
            holder.categBar=(LinearLayout) view.findViewById(R.id.categBar);
            holder.tvTitleCateg=(TextView) view.findViewById(R.id.tvTitleCateg);

            holder.item_detail_layout=(RelativeLayout) view.findViewById(R.id.item_detail_layout);


            String currCategory =   cursor.getString(cursor.getColumnIndex("product_category"));

         holder.tvTitleCateg.setText(currCategory);

         if(prevCat.equalsIgnoreCase(currCategory)){


             holder.categBar.setVisibility(View.GONE);


         }else{

                    holder.categBar.setVisibility(View.VISIBLE);

         }

         prevCat= currCategory;

         OnClickListener mOnImagebtnClickListener = new OnClickListener() {
                @Override
                public void onClick(View v) {
                    final int position = ProductsActivity.listView.getPositionForView((View) v.getParent());
                    Log.v(TAG, "Image button in list clicked, row ="+position);

                    ListAdapter la=     ProductsActivity.listView.getAdapter();
                       System.out.println("list adapter from get is ="+la);

                    int cnt =     getPositionFromRowId(la, position, 0, la.getCount());

                     System.out.println("info from new mthod ="+cnt);

                }
            };

            holder.imgBtnLv.setOnClickListener(mOnImagebtnClickListener);

            view.setTag(holder);


    }

    static class ViewHolder {
        TextView tvCode;
        TextView tvItemName,tvItemType,tvPrice,tvQuantity;
         RelativeLayout item_detail_layout;
         ImageButton imgBtnLv;
         LinearLayout categBar;
         TextView tvTitleCateg;
    }

1 个答案:

答案 0 :(得分:0)

使用ViewHolder的方式毫无意义。这是没用的恕我直言。看这里: http://www.jmanzano.es/blog/?p=166

您不会在此处夸大任何观看次数,因此您不需要它。 ViewHolder应该用于删除冗余视图膨胀和重用视图,但在Cursor适配器重用视图已经完成。所以你可以删除它。

相关问题