PreferenceFragment with library support

I am developing an application in which I used fragments of a support library and stumbled upon this problem and I can not add PreferencesFragment (for parameters) using this library?

I found some tips for using v7 PreferenceFragmentCompat , but for some reason I cannot add the v7 support library to my build path, so I cannot find PreferenceFragmentCompat ...

I tried rewriting the code to use regular snippets instead of the ones in the support library, but I had some problems with this.

In case you are interested, I am developing a support library, because when reading the Big Nerd Ranch book on Android programming, somewhere early they advise you to always use the support library for fragments.

Any suggestions for workarounds, or should I just upgrade to a version without support?

Here are the dependencies on my build.gradle:

 dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:design:23.1.1' } 
+5
source share
1 answer

The appcompat v7 library actually uses the v4 support library, so you need to explicitly import the v7 support library components that you need.

In your case, you just need to add compile 'com.android.support:preference-v7:23.1.1' to the build.gradle file:

 dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:design:23.1.1' compile 'com.android.support:preference-v7:23.1.1' } 

Then it will work:

 import android.os.Bundle; import android.support.v7.preference.PreferenceFragmentCompat; import android.view.View; public class MyPreferenceFragment extends PreferenceFragmentCompat { public MyPreferenceFragment() { // Required empty public constructor } @Override public void onCreatePreferences(Bundle bundle, String s) { addPreferencesFromResource(R.xml.fragment_settings_pref); } } 
+15
source

Source: https://habr.com/ru/post/1246753/


All Articles