Last active
December 9, 2018 20:09
-
-
Save dynoChris/53f3f4283594c7a9f36e787adc0edc78 to your computer and use it in GitHub Desktop.
How to fix bug with MultiSelectListPreference in android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Many thanks to this author https://github.com/chroaster/MSLPBug | |
//I use adnroidx, but you can use any support libraries | |
//Notice, it issue use deprecated methods, but I don't see problem with performence of app. | |
//********************** | |
package com.example.mslpfixdemo; | |
import android.annotation.TargetApi; | |
import android.app.AlertDialog; | |
import android.content.Context; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.preference.MultiSelectListPreference; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.widget.AbsListView; | |
import android.widget.CheckedTextView; | |
import android.widget.ListView; | |
public class MultiSelectListPreferenceFix extends MultiSelectListPreference { | |
public MultiSelectListPreferenceFix(Context context) { | |
super(context); | |
} | |
public MultiSelectListPreferenceFix(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
public MultiSelectListPreferenceFix(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
} | |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
public MultiSelectListPreferenceFix(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
} | |
@Override | |
protected void showDialog(Bundle state) { | |
super.showDialog(state); | |
AlertDialog dialog = (AlertDialog)getDialog(); | |
if (dialog == null) | |
return; | |
if (Build.VERSION.SDK_INT >= 23) { | |
ListView listView = dialog.getListView(); | |
listView.setOnScrollListener(new AbsListView.OnScrollListener() { | |
@Override | |
public void onScrollStateChanged(AbsListView view, int scrollState) { | |
int size = view.getChildCount(); | |
for (int i = 0; i < size; i++) { | |
View v = view.getChildAt(i); | |
if (v instanceof CheckedTextView) | |
((CheckedTextView)v).refreshDrawableState(); | |
} | |
} | |
@Override | |
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { | |
int size = view.getChildCount(); | |
for (int i = 0; i < size; i++) { | |
View v = view.getChildAt(i); | |
if (v instanceof CheckedTextView) | |
((CheckedTextView)v).refreshDrawableState(); | |
} | |
} | |
}); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.mslpfixdemo; | |
import android.os.Bundle; | |
import android.preference.PreferenceFragment; | |
import androidx.annotation.Nullable; | |
public class MySettingsFragment extends PreferenceFragment { | |
@Override | |
public void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
addPreferencesFromResource(R.xml.preferences); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:android="http://schemas.android.com/apk/res/android"> | |
<com.example.mslpfixdemo.MultiSelectListPreferenceFix | |
android:defaultValue="@array/values_default_true" | |
android:dialogTitle="@string/dialog_title" | |
android:entries="@array/inflate_values" | |
android:entryValues="@array/return_values" | |
android:key="@string/key" | |
android:summary="@string/summary" | |
android:title="@string/title" /> | |
</PreferenceScreen> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<fragment xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/container" | |
android:name="com.oliverstudio.androidxtest1.MySettingsFragment" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
</fragment> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.mslpfixdemo; | |
import android.os.Bundle; | |
import androidx.appcompat.app.AppCompatActivity; | |
public class SettingsActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.settings); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//in values/strings_settings.xml | |
//************ | |
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<!--Genres of Realise ListPreference--> | |
<!--*****************--> | |
<string name="key_genres_multiselectlist_pref">genres_multiselectlist_pref</string> | |
<string name="title_genres_list_pref">Choose genres</string> | |
<string name="summary_genres_list_pref">Сhoose your preferred movie genres</string> | |
<string-array name="inflate_values_genres_multiselect_pref"> | |
<item>Action</item> | |
<item>Adventure</item> | |
<item>Thriller</item> | |
<item>Comedy</item> | |
<item>Drama</item> | |
</string-array> | |
<string-array name="return_values_genres_multiselectlist_pref"> | |
<item>action</item> | |
<item>adventure</item> | |
<item>thriller</item> | |
<item>comedy</item> | |
<item>drama</item> | |
</string-array> | |
<string-array name="values_default_true_genres_multiselectlist_pref"> | |
<item>action</item> | |
<item>adventure</item> | |
<item>thriller</item> | |
<item>comedy</item> | |
<item>drama</item> | |
</string-array> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment