开关按钮 - 点击可以工作,但在安卓系统中滑动开关无法工作

3
这是我的开关按钮代码片段。
productChildViewHolder.switchBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                //Utility.displayToast("here");
                //Is the switch is on?
                boolean on = ((Switch) v).isChecked();
                if (on) {
                    productVariant.setAvailable(1);
                } else {
                    productVariant.setAvailable(0);
                }

                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("Type", "UPDATE_AVAILABLE");
                    jsonObject.put("ProductID", productVariant.getProductID());
                    jsonObject.put("VariantID", productVariant.getVariantID());
                    jsonObject.put("Available", productVariant.getAvailable());
                    new UpdateProductVariantTask().execute(jsonObject.toString());

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

对我来说,单击按键的工作方式符合预期。甚至单击可以切换按钮。但是滑动只会切换它,但不会触发功能。

如何使滑动和单击以相同方式工作以切换按钮?


如果您尝试使用 setOnCheckedChangeListener 呢? - M D
如果我使用这个函数,在回收视图中上下滚动时会触发它。因为我基于这个值设置了一个字段。 - Devesh Agrawal
2个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
3

Replace the setOnClickListener to setOnCheckedChangeListener.
And inside your listener check if buttonView.isPressed() to be sure it was pressed by userer (and not changed by the framework detection)

Code:

productChildViewHolder.switchBtn.setOnCheckedChangeListener(new OnCheckedChangeListener(

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                if(buttonView.isPressed() && isChecked){
                //Your code here
                }
            }
        });

Why is this happening -

The ViewHolder pattern renders the "base" view and inflate the changes for better performance.
Unfortunately implemented very poorly by android developers and causes many weird problems which are very hard to detect.

What our options?

If possible - use a simple list view.
If the demand is more complicated and will cause performance issues look for open source library here.


谢谢。这就是我在寻找的东西。 :) - Devesh Agrawal
嘿,我观察到这段代码有一些不寻常的行为。事件并不总是被检测到。你知道为什么吗? - Devesh Agrawal
一般来说,viewHolder模式被认为是最佳实践,但在实际应用中纯粹地实现它会导致许多“奇怪”的问题,我的建议是不要使用它。 - Nir Duan

0
你可以试试这个。
     productChildViewHolder.switchBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                productVariant.setAvailable(1);
            } else {
                productVariant.setAvailable(0);
            }
                JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("Type", "UPDATE_AVAILABLE");
                jsonObject.put("ProductID", productVariant.getProductID());
                jsonObject.put("VariantID", productVariant.getVariantID());
                jsonObject.put("Available", productVariant.getAvailable());
                new UpdateProductVariantTask().execute(jsonObject.toString());

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });

希望我已经帮到了你。 祝你好运。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,