Show up-Button in actionBar in subscreen preferences

后端 未结 3 823
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-31 09:30

I\'ve implemented my preferences like shown in the official guidelines.

I have a PreferenceActivity which creates the PreferenceFragment like this:

          


        
3条回答
  •  既然无缘
    2020-12-31 10:10

    I finally got it to work :D. It's quite hacky but it works.

    The problem is, that using subscreens in xml-layouts results in some 'code magic'. A new activity/dialog is started for the subscreen and you don't have direct access to it.

    To get access to the actionbar and the OnClickListener of the home/up-button you need to get a reference to your PreferenceScreen and get its parent Dialog in order to access the actionbar and its home/up button.

    This is how it is done inside my PreferenceFragment:

     @Override
     public void onCreate(Bundle savedInstanceState) 
     {
    ...
    
     final PreferenceScreen preferenceScreen = (PreferenceScreen) findPreference(getString(R.string.keyPrefScreenDynamicWidgetDetails));
      preferenceScreen.setOnPreferenceClickListener(new OnPreferenceClickListener() 
      {
           public boolean onPreferenceClick(Preference preference) 
           {
               preferenceScreen.getDialog().getActionBar().setDisplayHomeAsUpEnabled(true);
               final Dialog dialog = preferenceScreen.getDialog();
    
               View homeBtn = dialog.findViewById(android.R.id.home);
               if (homeBtn != null) 
               {
                   OnClickListener dismissDialogClickListener = new OnClickListener()
                   {
                       @Override
                       public void onClick(View v) 
                       {
                          dialog.dismiss();
                       }
                   };
    
                   // Prepare yourselves for some hacky programming
                   ViewParent homeBtnContainer = homeBtn.getParent();
    
                   // The home button is an ImageView inside a FrameLayout
                   if (homeBtnContainer instanceof FrameLayout) {
                       ViewGroup containerParent = (ViewGroup) homeBtnContainer.getParent();
    
                       if (containerParent instanceof LinearLayout) {
                           // This view also contains the title text, set the whole view as clickable
                           ((LinearLayout) containerParent).setOnClickListener(dismissDialogClickListener);
                       } else {
                           // Just set it on the home button
                           ((FrameLayout) homeBtnContainer).setOnClickListener(dismissDialogClickListener);
                       }
                   } else {
                       // The 'If all else fails' default case
                       homeBtn.setOnClickListener(dismissDialogClickListener);
                   }                   
               }
               return true;
           }
      });
    ...
    
    }
    

    Following link gave me the final hints and code to solve my problem:

    Action Bar Home Button not functional with nested PreferenceScreen

提交回复
热议问题