在棒棒糖开关preference回调行为改变 [英] SwitchPreference Callback behaviour change in lollipop

查看:329
本文介绍了在棒棒糖开关preference回调行为改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的项目中,我们使用的是开关$ P $类似的wifi settings.user pference可以通过点击切换按钮和用户可以通过点击标题查看更多选项触发值。

In our project we are using SwitchPreference similar to wifi settings.user can toggle value by clicking on toggle button and user can see more options by clicking on title.

但是,这不是在棒棒糖工作。我可以看到一些棒棒糖行为改变。

But this is not working in lollipop. I can see some behaviour change in lollipop.

在奇巧:

当用户点击切换按钮,在preferenceChanged 回调被调用,当用户点击标题,在preferenceClicked 被称为

When user taps on toggle button, onPreferenceChanged callback is called and when user taps on title, onPreferenceClicked is called.

在棒棒糖:
您在切换按钮或标题在preferenceClicked挖掘总是被调用,并且在preferenceChanged 被调用后。
我怎样才能在棒棒糖一样的行为?这种行为打破了我们的功能。

In lollipop: you tap on toggle button or title onPreferenceClicked is called always and after that onPreferenceChanged is called. How can I get same behaviour in lollipop? This behaviour is breaking our functionality.

推荐答案

我遇到同样的问题,我发现这一点:<一href=\"https://$c$c.google.com/p/android/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=&id=172425\" rel=\"nofollow\">https://$c$c.google.com/p/android/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=&id=172425

I encounter same issue and I found this : https://code.google.com/p/android/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=&id=172425

经过一番尝试,我发现如何实现这一变通方法,对我来说,工作:

After some try, I found how to implement this workaround that work on my case :

public class MySwitchPreference extends SwitchPreference {

/**
 * Construct a new SwitchPreference with the given style options.
 *
 * @param context The Context that will style this preference
 * @param attrs Style attributes that differ from the default
 * @param defStyle Theme attribute defining the default style options
 */
public MySwitchPreference(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

/**
 * Construct a new SwitchPreference with the given style options.
 *
 * @param context The Context that will style this preference
 * @param attrs Style attributes that differ from the default
 */
public MySwitchPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
}

/**
 * Construct a new SwitchPreference with default style options.
 *
 * @param context The Context that will style this preference
 */
public MySwitchPreference(Context context) {
    super(context, null);
}

@Override
protected void onBindView(View view) {
    ViewGroup viewGroup= (ViewGroup)view;
    setSwitchClickable(viewGroup);
    super.onBindView(view);
}

private void setSwitchClickable(ViewGroup viewGroup) {
      if (null == viewGroup) {
      return;
  }

  int count = viewGroup.getChildCount();
  for(int n = 0; n < count; ++n) {
      View childView = viewGroup.getChildAt(n);
      if(childView instanceof Switch) {
          final Switch switchView = (Switch) childView;
          switchView.setClickable(true);
          return;
      } else if (childView instanceof ViewGroup){
        ViewGroup childGroup = (ViewGroup)childView;
        setSwitchClickable(childGroup);
      }
  }

}

然后你只需直接使用自己的是myswi​​tch preference到Switch preference。

Then you just have to use your own "MySwitchPreference" into SwitchPreference directly.

这篇关于在棒棒糖开关preference回调行为改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆