在Android的列表视图按钮点击问题(Listview button click issue in

2019-10-22 07:30发布

我使用的是列表视图,有一个名为列表项“订单”按钮。 我想显示“嘀”的形象当用户按下按钮,当用户再次按下按钮将其隐藏。

我的问题是,当我点击的第一个项目按钮,选中第4,8项的图像也出现了。

这是onClickListener的调节器,

    viewHolderItem.btnOrder.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            boolean isSelected = shoe.isSelected();

            if(isSelected){
                viewHolderItem.ivTick.setImageDrawable(null);
                isSelected = false;
            }else{
                viewHolderItem.ivTick.setImageDrawable(mContext.getResources().getDrawable(R.drawable.tick));
                isSelected = true;
            }

            shoeList.get(position).setSelected(isSelected);
            obj.getAdapter().notifyDataSetChanged();
        }
    });

什么是这个问题的原因是什么?

更新:这是我的adapte getview方法

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

    final ViewHolderItem viewHolderItem;

    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.component_order_shoe_row,null);

        viewHolderItem = new ViewHolderItem();

        viewHolderItem.tvName = (TextView)convertView.findViewById(R.id.tv_component_order_shoe_name);
        viewHolderItem.tvPrice = (TextView)convertView.findViewById(R.id.tv_component_order_shoe_price);
        viewHolderItem.tvDesc = (TextView)convertView.findViewById(R.id.tv_component_order_shoe_description);
        viewHolderItem.ivPic = (ImageView)convertView.findViewById(R.id.iv_component_order_shoe_pic);
        viewHolderItem.ivTick = (ImageView)convertView.findViewById(R.id.iv_component_order_shoe_tick);
        viewHolderItem.spinnerSize = (Spinner)convertView.findViewById(R.id.spinner_component_order_shoe_size);
        viewHolderItem.etQty = (EditText)convertView.findViewById(R.id.et_component_order_shoe_qty);
        viewHolderItem.btnOrder = (Button)convertView.findViewById(R.id.btn_component_order_shoe_order);

        convertView.setTag(viewHolderItem);

    }else{
        viewHolderItem = (ViewHolderItem)convertView.getTag();
    }

    final Shoe shoe = shoeList.get(position);
    viewHolderItem.tvName.setText(shoe.getShoeName());

    String text = String.valueOf(shoe.getPrice());
    int integerPlaces = text.indexOf('.');
    int decimalPlaces = text.length() - integerPlaces - 1;
    if(decimalPlaces==1){
        viewHolderItem.tvPrice.setText("Rs."+ text + "0");
    }else{
        viewHolderItem.tvPrice.setText("Rs."+ text);
    }
    viewHolderItem.tvDesc.setText(shoe.getDesc());

    Bitmap bmp = AppControl.convertStringToBitmap(shoe.getImg());
    viewHolderItem.ivPic.setImageBitmap(bmp);

    int smallest = shoe.getSmallestSize();
    int largest = shoe.getLargestSize();

    Integer[] arr = getSizeArray(smallest,largest);
    ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(mContext, android.R.layout.simple_spinner_dropdown_item,arr);
    viewHolderItem.spinnerSize.setAdapter(adapter);


    viewHolderItem.btnOrder.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            boolean isSelected = shoe.isSelected();

            if(isSelected){
                viewHolderItem.ivTick.setImageDrawable(null);
                isSelected = false;
            }else{
                viewHolderItem.ivTick.setImageDrawable(mContext.getResources().getDrawable(R.drawable.tick));
                isSelected = true;
            }

            shoeList.get(position).setSelected(isSelected);
            obj.getAdapter().notifyDataSetChanged();
        }
    });

    return convertView;
}

Answer 1:

你必须设置标签的时候,第一次使创建视图:

if(convertView==null){
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    //code

    viewHolderItem.btnOrder= (ImageButton)rowView.findViewById(R.id.imageButton);

    viewHolderItem.btnOrder.setTag(viewHolder); //set the tag for the button
    convertView.setTag(viewHolder);
} else {
    viewHolderItem = (ViewHolder) convertView.getTag();
}

并得到它的点击监听器里:

viewHolderItem.btnOrder.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {
         viewHolderItem = (ViewHolder) v.getTag(); //get the tag inside on click
         boolean isSelected = shoe.isSelected();

         if(isSelected){
            viewHolderItem.ivTick.setImageDrawable(null);
            isSelected = false;
         }else{
            viewHolderItem.ivTick.setImageDrawable(mContext.getResources().getDrawable(R.drawable.tick));
            isSelected = true;
         }

         shoeList.get(position).setSelected(isSelected);
         obj.getAdapter().notifyDataSetChanged();
    }
});


Answer 2:

您应该添加标签的每一行:

在getView mehtod:

viewHolderItem.setTag(viewHolderItem);

在显示类添加getId()方法,并返回ID

改变的onclick监听器,如:

viewHolderItem.btnOrder.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        boolean isSelected = shoe.isSelected();

        if(isSelected){
            viewHolderItem.ivTick.setImageDrawable(null);
            isSelected = false;
        }else{
            viewHolderItem.ivTick.setImageDrawable(mContext.getResources().getDrawable(R.drawable.tick));
            isSelected = true;
        }

        showList(viewHolderItem.getTag()).setSelected(isSelected);
        obj.getAdapter().notifyDataSetChanged();
    }
});

出现该问题的becuse当你向上/向下滚动,getView()被调用的每一行即行为4,5等。所以。 该position值的变化非常频繁。 所以我们应该用viewHolderItem.getTag()这将解决这个问题值



文章来源: Listview button click issue in android