Switch的原有特性導致在網(wǎng)絡請求數(shù)據(jù)還未成功時采桃,就已經(jīng)切換了狀態(tài)懒熙,可能與真實狀態(tài)不符合丘损,所以需要關閉手動切換的特性,使用clickable屬性有點問題工扎,能夠手動滑動開關
我的解決辦法是攔截組件的觸摸事件徘钥,并手動返回一個點擊事件,就解決了手動點擊開關導致的與真實數(shù)據(jù)不一致的問題
public class MySwitch extends Switch {
OnClickListener l;
public MySwitch(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP)
if (l != null)
l.onClick(this);
return true;
}
@Override
public void setOnClickListener(@Nullable OnClickListener l) {
this.l = l;
}
}