Android SwitchPreference组件 非Swit
2022-10-31 本文已影响0人
学海摆渡人
背景
在做一个设置页面的时候,通过PreferenceCategory来动态添加SwitchPreference来做设置,但是QA反馈说容易误触修改状态,需要对其进行优化
分析
https://blog.csdn.net/vipclx/article/details/8493900 通过该篇文章分析,然后并不适用,通过查看源码并没有好的处理方式。
先开始从SwitchPreference各个视图做点击事件屏蔽,先重写onBindViewHolder函数,对itemView进行点击事件置空
点击事件回调2次问题处理
通过打印调用栈
10-31 15:20:52.100 7520 7520 E SettingsExt: java.lang.Throwable
10-31 15:20:52.100 7520 7520 E SettingsExt: at com.android.settings.ext.SettingsActivity.onPreferenceChange(SettingsActivity.java:289)
10-31 15:20:52.100 7520 7520 E SettingsExt: at androidx.preference.Preference.callChangeListener(Preference.java:1118)
10-31 15:20:52.100 7520 7520 E SettingsExt: at androidx.preference.TwoStatePreference.onClick(TwoStatePreference.java:67)
10-31 15:20:52.100 7520 7520 E SettingsExt: at androidx.preference.Preference.performClick(Preference.java:1182)
10-31 15:20:52.100 7520 7520 E SettingsExt: at androidx.preference.Preference.performClick(Preference.java:1166)
10-31 15:20:52.100 7520 7520 E SettingsExt: at androidx.preference.SwitchPreference.performClick(SwitchPreference.java:195)
10-31 15:20:52.100 7520 7520 E SettingsExt: at androidx.preference.Preference$1.onClick(Preference.java:181)
10-31 15:20:52.100 7520 7520 E SettingsExt: at android.view.View.performClick(View.java:6294)
10-31 15:20:52.100 7520 7520 E SettingsExt: at android.widget.CompoundButton.performClick(CompoundButton.java:134)
10-31 15:20:52.100 7520 7520 E SettingsExt: at android.view.View$PerformClick.run(View.java:24770)
10-31 15:20:52.100 7520 7520 E SettingsExt: at android.os.Handler.handleCallback(Handler.java:790)
10-31 15:20:52.100 7520 7520 E SettingsExt: at android.os.Handler.dispatchMessage(Handler.java:99)
10-31 15:20:52.100 7520 7520 E SettingsExt: at android.os.Looper.loop(Looper.java:164)
10-31 15:20:52.100 7520 7520 E SettingsExt: at android.app.ActivityThread.main(ActivityThread.java:6553)
10-31 15:20:52.100 7520 7520 E SettingsExt: at java.lang.reflect.Method.invoke(Native Method)
10-31 15:20:52.100 7520 7520 E SettingsExt: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:558)
10-31 15:20:52.100 7520 7520 E SettingsExt: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
10-31 15:20:54.954 7520 7520 E SettingsExt: xx
10-31 15:20:54.954 7520 7520 E SettingsExt: java.lang.Throwable
10-31 15:20:54.954 7520 7520 E SettingsExt: at com.android.settings.ext.SettingsActivity.onPreferenceChange(SettingsActivity.java:289)
10-31 15:20:54.954 7520 7520 E SettingsExt: at androidx.preference.Preference.callChangeListener(Preference.java:1118)
10-31 15:20:54.954 7520 7520 E SettingsExt: at androidx.preference.SwitchPreference$Listener.onCheckedChanged(SwitchPreference.java:234)
10-31 15:20:54.954 7520 7520 E SettingsExt: at android.widget.CompoundButton.setChecked(CompoundButton.java:171)
10-31 15:20:54.954 7520 7520 E SettingsExt: at android.widget.Switch.setChecked(Switch.java:1075)
10-31 15:20:54.954 7520 7520 E SettingsExt: at android.widget.Switch.toggle(Switch.java:1070)
10-31 15:20:54.954 7520 7520 E SettingsExt: at android.widget.CompoundButton.performClick(CompoundButton.java:132)
10-31 15:20:54.954 7520 7520 E SettingsExt: at android.view.View$PerformClick.run(View.java:24770)
10-31 15:20:54.954 7520 7520 E SettingsExt: at android.os.Handler.handleCallback(Handler.java:790)
10-31 15:20:54.954 7520 7520 E SettingsExt: at android.os.Handler.dispatchMessage(Handler.java:99)
10-31 15:20:54.954 7520 7520 E SettingsExt: at android.os.Looper.loop(Looper.java:164)
10-31 15:20:54.954 7520 7520 E SettingsExt: at android.app.ActivityThread.main(ActivityThread.java:6553)
10-31 15:20:54.954 7520 7520 E SettingsExt: at java.lang.reflect.Method.invoke(Native Method)
10-31 15:20:54.954 7520 7520 E SettingsExt: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:558)
10-31 15:20:54.954 7520 7520 E SettingsExt: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
仅需继承SwitchPreference是屏蔽onClick函数就行
示例
public class AppPreference extends SwitchPreference {
private static final String TAG = GlobalConstant.TAG + ".AppPreference";
public AppPreference(Context context) {
super(context);
}
public AppPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
holder.itemView.setOnClickListener(null);
View.OnClickListener onClickListener = null;
try {
Field field = Preference.class.getDeclaredField("mClickListener");
field.setAccessible(true);
onClickListener = (View.OnClickListener)
field.get(this);
} catch (Exception e) {
Utils.logError(TAG, "onBindViewHolder", e);
}
if (onClickListener != null) {
View switchView = holder.findViewById(android.R.id.switch_widget);
switchView.setOnClickListener(onClickListener);
}
}
@Override
protected void onClick() {
//super.onClick();
}