listview 点击错乱

最近太忙了,不知道以后能不能从容一点,有点时间做自己的事情。但是在忙也阻挡不了我的贫嘴浪费时间。今天是要先挖一个坑,放在这里,以后要在来填。

我像往常一样在写一普通的逻辑,用到了ListView并且适配了些数据进去,ListView中item内部会有一些Button的点击事件。一气呵成写完后,并不能点击,在IDE有报错的情况下,点击就是不响应,偶尔响应一下也是点击此Item,其他的Item响应,拉动的过程中有可能也响应,简直是乱起八糟,心里是郁闷的。后续上网查找原因,基本是一种说法,两大类做法,就是Item的点击事件和Item内部的控件的点击事件冲突了,第一种做法是,在Item的根布局上用
android:descendantFocusability=”blocksDescendants”意思是先屏蔽子View的焦点
android:descendantFocusability=”afterDescendants”等子View获取完焦点,再获取
android:descendantFocusability=”beforeDescendants”在子View之前获取焦点
这种做法,奏效的人很多,据说是perfect solution,结果在我这里不管用。
然后依照stack overflow上的答案,做了第二种做法:
利用回调接口,在activity里面响应点击事件,其实这是同一种,只是形式不一样,终归是一种思路;可以我点击事件已经错乱;

万般无奈之下,我不是从头开始检查我的程序,也不是把程序删了重来,我开始在错的做法上挣扎缝补,于是:
我在点击事件下,记录下点击的Item的position和点击空间的类型(区别于其他的控件点击)

public void onClick(View v) {
                // TODO Auto-generated method stub
                int position = (Integer) v.getTag();
                switch(v.getId()){
                case R.id.iv_cart_item_select:
                //将postion和点击控件类型存储到全局的ShopApplication字段中
                    ShopApplication.cartAdapterItemPosition = position;
                    ShopApplication.clickType = "check";
                    //刷新适配器
                    cartListAdapter.notifyDataSetChanged();
                    break;
    //点击按钮前,现在adapter中为View带上位置tag,点击按钮后,取出tag获取点击点击item的位置,
    //记录下这个position,在刷新适配器数据的时候,getView()的过程中,判断如果点击的按钮则改变布局展示,
                case R.id.btn_cart_item_quantity_add:
                    //能响应到这个事件
                    //Toast.makeText(CartActivity.this, "点击了按钮"+position, 0).show();
                    ShopApplication.cartAdapterItemPosition = position;
                    ShopApplication.clickType = "add";
                    cartListAdapter.notifyDataSetChanged();
                    break;

                case R.id.btn_cart_item_quantity_reduce:
                    ShopApplication.cartAdapterItemPosition = position;
                    ShopApplication.clickType = "reduce";
                    cartListAdapter.notifyDataSetChanged();
                    break;

                default:
                    break;
                }
            }

在适配器getView( )方法下:

//在每次返回绘制的View的时候,检查全局字段是否符合要求,符合则做出改变
if(position == ShopApplication.cartAdapterItemPosition 
                && "add".equals(ShopApplication.clickType)){
                //获取到EditText中的数字内容
            int count= Integer.parseInt(holder.editQuantity.getText().toString());
            count++;//自增,点击一下,增一
            holder.editQuantity.setText(count+"");
        }



        if(position == ShopApplication.cartAdapterItemPosition 
                && "reduce".equals(ShopApplication.clickType)
                && Integer.parseInt(holder.editQuantity.getText().toString())>0){

            int count = Integer.parseInt(holder.editQuantity.getText().toString());
            count--;
            holder.editQuantity.setText(count+"");
        }

        if(position == ShopApplication.cartAdapterItemPosition 
                && "check".equals(ShopApplication.clickType)){

                Log.d("---------", position+"-------");
                if(ShopApplication.checkedTag){

                    holder.selectBox.setImageResource(R.drawable.cart_not_select_icon);
                    ShopApplication.checkedTag = false;

                }else{
                    holder.selectBox.setImageResource(R.drawable.cart_select_icon);
                    ShopApplication.checkedTag = true;
    }
                }

结果当然是可以奏效点击事件,可以有效的响应,但是有没有后遗症,回答是肯定的,会产生,listview回收问题,题主,当时直接喷血了,天要完我啊!

最后我想说的错误其实不在这里:
造成所有解决方案,不能奏效的原因在这里:

public class CartListAdapter extends BaseAdapter{


    private Handler handler;

    private Context context;
    private List<Product> products;

    private int selectTag;
    //罪魁祸首,不该在这里声明啊;
    **private ViewHolder holder**;




    private OnClickListener onWidgetListener;


    public CartListAdapter(Context context, List<Product> products, Handler handler){
        this.context = context;
        this.products = products;
        this.handler = handler;
    }

................................


    public View getView(int position, View convertView, ViewGroup parent){
        //在这里声明,世界都和平了,你造吗!
        **final ViewHolder holder;**
        if(convertView == null){
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.show_cart_item, null);

            **holder = new ViewHolder();**
            holder.picture = (ImageView)convertView.findViewById(R.id.iv_cart_item_picture);
            holder.name = (TextView)convertView.findViewById(R.id.tv_cart_product_name);
            holder.price = (TextView)convertView.findViewById(R.id.tv_show_cart_product_price);
            holder.priceDecimal =(TextView)convertView.findViewById(R.id.tv_show_cart_product_price_decimal);
            holder.quantity = (TextView)convertView.findViewById(R.id.tv_quantity);
        ....后面略去

你问我为什么,我很想知道,菜鸟的我又搜了下资料,只是大致知道是由于,viewholder是保持控件的实例的,避免每次都findViewById( ),和回收联系紧密,可是声明在getView( )方法外面会出现这种情况,我先把坑放这里。。。。。未完待续。。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值