Created
November 6, 2016 21:12
-
-
Save franciscojunior/3f3ad2dd190180817d195b8f9a661a9a to your computer and use it in GitHub Desktop.
Patch to add ExpandableCheckboxesLayout
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
diff --git a/app/src/main/java/name/vampidroid/ui/widget/CardFilters.java b/app/src/main/java/name/vampidroid/ui/widget/CardFilters.java | |
index 4133e99..3ff912e 100644 | |
--- a/app/src/main/java/name/vampidroid/ui/widget/CardFilters.java | |
+++ b/app/src/main/java/name/vampidroid/ui/widget/CardFilters.java | |
@@ -19,6 +19,9 @@ import android.widget.LinearLayout; | |
import android.widget.SeekBar; | |
import android.widget.TextView; | |
+import java.util.Arrays; | |
+import java.util.List; | |
+ | |
import name.vampidroid.R; | |
@@ -144,21 +147,84 @@ public class CardFilters extends LinearLayout { | |
setupCryptDisciplinesHandler(); | |
- setupClansHandler(); | |
+ final ExpandableLayoutCheckBoxes clansExpandableLayout = (ExpandableLayoutCheckBoxes) findViewById(R.id.expandableCryptClans); | |
+ | |
+ setupExpandableLayoutCheckBoxes( | |
+ clansExpandableLayout, | |
+ "Clans:", | |
+ Arrays.asList(getResources().getStringArray(R.array.clans)), | |
+ new ExpandableLayoutCheckBoxes.OnTextCheckBoxRowClickListener() { | |
+ @Override | |
+ public void onClick(TextView rowText, CheckBox rowCheckBox) { | |
+ numberOfClansFiltersApplied += rowCheckBox.isChecked() ? 1 : -1; | |
+ | |
+ updateExpandableFilterHeaderStatus(clansExpandableLayout, numberOfClansFiltersApplied); | |
+ | |
+ if (cardFiltersChangeListener != null && !isResetting) { | |
+ cardFiltersChangeListener.onClansChanged(rowText.getText().toString(), rowCheckBox.isChecked()); | |
+ } | |
+ } | |
+ } | |
+ | |
+ ); | |
+ | |
+ final ExpandableLayoutCheckBoxes libraryTypesExpandableLayout = (ExpandableLayoutCheckBoxes) findViewById(R.id.expandableLibraryTypes); | |
+ | |
+ setupExpandableLayoutCheckBoxes( | |
+ libraryTypesExpandableLayout, | |
+ "Card Types:", | |
+ Arrays.asList(getResources().getStringArray(R.array.types)), | |
+ new ExpandableLayoutCheckBoxes.OnTextCheckBoxRowClickListener() { | |
+ @Override | |
+ public void onClick(TextView rowText, CheckBox rowCheckBox) { | |
+ numberOfCardTypesFiltersApplied += rowCheckBox.isChecked() ? 1 : -1; | |
- setupCardTypesHandler(); | |
+ updateExpandableFilterHeaderStatus(libraryTypesExpandableLayout, numberOfCardTypesFiltersApplied); | |
+ | |
+ if (cardFiltersChangeListener != null && !isResetting) { | |
+ cardFiltersChangeListener.onCardTypeChanged(rowText.getText().toString(), rowCheckBox.isChecked()); | |
+ } | |
+ } | |
+ } | |
+ | |
+ ); | |
+ | |
+ final ExpandableLayoutCheckBoxes libraryDisciplinesExpandableLayout = (ExpandableLayoutCheckBoxes) findViewById(R.id.expandableLibraryDisciplines); | |
+ | |
+ setupExpandableLayoutCheckBoxes( | |
+ libraryDisciplinesExpandableLayout, | |
+ "Library Disciplines:", | |
+ Arrays.asList(getResources().getStringArray(R.array.disciplineslibrary)), | |
+ new ExpandableLayoutCheckBoxes.OnTextCheckBoxRowClickListener() { | |
+ @Override | |
+ public void onClick(TextView rowText, CheckBox rowCheckBox) { | |
+ numberOfLibraryDisciplineFiltersApplied += rowCheckBox.isChecked() ? 1 : -1; | |
+ | |
+ updateExpandableFilterHeaderStatus(libraryDisciplinesExpandableLayout, numberOfLibraryDisciplineFiltersApplied); | |
+ | |
+ if (cardFiltersChangeListener != null && !isResetting) { | |
+ cardFiltersChangeListener.onLibraryDisciplineChanged(rowText.getText().toString(), rowCheckBox.isChecked()); | |
+ } | |
+ } | |
+ } | |
+ | |
+ ); | |
- setupLibraryDisciplinesHandler(); | |
setupCapacitiesHandler(); | |
setupExpandLayout(cryptDisciplinesHeader, cryptDisciplinesLayout, (ImageView) findViewById(R.id.imgDisciplinesLayoutArrow)); | |
- setupExpandLayout(clansHeader, clansLayout, (ImageView) findViewById(R.id.imgClansLayoutArrow)); | |
+ } | |
+ | |
+ private void setupExpandableLayoutCheckBoxes(ExpandableLayoutCheckBoxes expandableLayout, CharSequence headerText, List<String> checkboxItems, ExpandableLayoutCheckBoxes.OnTextCheckBoxRowClickListener checkBoxRowClickListener) { | |
+ | |
+ expandableLayout.setItemsList(checkboxItems); | |
- setupExpandLayout(cardTypesHeader, cardTypesLayout, (ImageView) findViewById(R.id.imgCardTypesLayoutArrow)); | |
+ expandableLayout.setHeaderText(headerText); | |
+ | |
+ expandableLayout.setTextCheckBoxRowClickListener(checkBoxRowClickListener); | |
- setupExpandLayout(libraryDisciplinesHeader, libraryDisciplinesLayout, (ImageView) findViewById(R.id.imgLibraryDisciplinesLayoutArrow)); | |
} | |
@@ -249,9 +315,14 @@ public class CardFilters extends LinearLayout { | |
numberOfCardTypesFiltersApplied = bundle.getInt("numberOfCardTypesFiltersApplied"); | |
numberOfLibraryDisciplineFiltersApplied = bundle.getInt("numberOfLibraryDisciplineFiltersApplied"); | |
- updateFilterHeaderStatus((TextView) findViewById(R.id.textClans), numberOfClansFiltersApplied); | |
- updateFilterHeaderStatus((TextView) findViewById(R.id.textCardTypes), numberOfCardTypesFiltersApplied); | |
- updateFilterHeaderStatus((TextView) findViewById(R.id.textLibraryDisciplines), numberOfLibraryDisciplineFiltersApplied); | |
+// updateFilterHeaderStatus((TextView) findViewById(R.id.textClans), numberOfClansFiltersApplied); | |
+// updateFilterHeaderStatus((TextView) findViewById(R.id.textHeader), numberOfCardTypesFiltersApplied); | |
+// updateFilterHeaderStatus((TextView) findViewById(R.id.textLibraryDisciplines), numberOfLibraryDisciplineFiltersApplied); | |
+ | |
+ updateExpandableFilterHeaderStatus((ExpandableLayoutCheckBoxes) findViewById(R.id.expandableCryptClans), numberOfClansFiltersApplied); | |
+ updateExpandableFilterHeaderStatus((ExpandableLayoutCheckBoxes) findViewById(R.id.expandableLibraryTypes), numberOfCardTypesFiltersApplied); | |
+ updateExpandableFilterHeaderStatus((ExpandableLayoutCheckBoxes) findViewById(R.id.expandableLibraryDisciplines), numberOfLibraryDisciplineFiltersApplied); | |
+ | |
} else { | |
super.onRestoreInstanceState(state); | |
@@ -268,6 +339,16 @@ public class CardFilters extends LinearLayout { | |
} | |
} | |
+ | |
+ void updateExpandableFilterHeaderStatus(ExpandableLayoutCheckBoxes expandableLayoutCheckBoxes, int value) { | |
+ if (value > 0) { | |
+ expandableLayoutCheckBoxes.setHeaderColor(ContextCompat.getColor(getContext(), R.color.colorAccent)); | |
+ } else { | |
+ expandableLayoutCheckBoxes.setHeaderColor(ContextCompat.getColor(getContext(), android.R.color.black)); | |
+ } | |
+ | |
+ } | |
+ | |
// Reference: http://stackoverflow.com/questions/11358121/how-to-handle-the-checkbox-ischecked-and-unchecked-event-in-android | |
private void setupGroupsHandler() { | |
@@ -348,7 +429,7 @@ public class CardFilters extends LinearLayout { | |
private void setupCardTypesHandler() { | |
- final TextView cardTypesHeaderText = (TextView) findViewById(R.id.textCardTypes); | |
+ final TextView cardTypesHeaderText = (TextView) findViewById(R.id.textHeader); | |
setupTextCheckBoxRowHandler((ViewGroup) cardTypesLayout, new OnTextCheckBoxRowHandlerClickListener() { | |
@Override | |
@@ -507,9 +588,14 @@ public class CardFilters extends LinearLayout { | |
numberOfCardTypesFiltersApplied = 0; | |
- updateFilterHeaderStatus((TextView) findViewById(R.id.textClans), 0); | |
- updateFilterHeaderStatus((TextView) findViewById(R.id.textCardTypes), 0); | |
- updateFilterHeaderStatus((TextView) findViewById(R.id.textLibraryDisciplines), 0); | |
+// updateFilterHeaderStatus((TextView) findViewById(R.id.textClans), 0); | |
+// updateFilterHeaderStatus((TextView) findViewById(R.id.textHeader), 0); | |
+// updateFilterHeaderStatus((TextView) findViewById(R.id.textLibraryDisciplines), 0); | |
+ | |
+ updateExpandableFilterHeaderStatus((ExpandableLayoutCheckBoxes) findViewById(R.id.expandableCryptClans), 0); | |
+ updateExpandableFilterHeaderStatus((ExpandableLayoutCheckBoxes) findViewById(R.id.expandableLibraryTypes), 0); | |
+ updateExpandableFilterHeaderStatus((ExpandableLayoutCheckBoxes) findViewById(R.id.expandableLibraryDisciplines), 0); | |
+ | |
isResetting = false; | |
diff --git a/app/src/main/java/name/vampidroid/ui/widget/ExpandableLayoutCheckBoxes.java b/app/src/main/java/name/vampidroid/ui/widget/ExpandableLayoutCheckBoxes.java | |
new file mode 100644 | |
index 0000000..51876ce | |
--- /dev/null | |
+++ b/app/src/main/java/name/vampidroid/ui/widget/ExpandableLayoutCheckBoxes.java | |
@@ -0,0 +1,167 @@ | |
+package name.vampidroid.ui.widget; | |
+ | |
+import android.annotation.TargetApi; | |
+import android.content.Context; | |
+import android.os.Build; | |
+import android.support.constraint.ConstraintLayout; | |
+import android.util.AttributeSet; | |
+import android.util.Log; | |
+import android.view.LayoutInflater; | |
+import android.view.View; | |
+import android.widget.CheckBox; | |
+import android.widget.ImageView; | |
+import android.widget.LinearLayout; | |
+import android.widget.TextView; | |
+ | |
+import java.util.List; | |
+import java.util.concurrent.atomic.AtomicInteger; | |
+ | |
+import name.vampidroid.R; | |
+ | |
+/** | |
+ * Created by fxjr on 05/11/16. | |
+ */ | |
+ | |
+public class ExpandableLayoutCheckBoxes extends LinearLayout { | |
+ | |
+ private static final String TAG = "ExpandableLayoutCheckBo"; | |
+ LinearLayout rowsLayout; | |
+ OnTextCheckBoxRowClickListener onTextCheckBoxRowClickListener; | |
+ private int headerColor; | |
+ private View headerLayout; | |
+ private TextView headerTextView; | |
+ private ImageView imgHeaderArrow; | |
+ | |
+ public ExpandableLayoutCheckBoxes(Context context, AttributeSet attrs) { | |
+ super(context, attrs); | |
+ init(); | |
+ } | |
+ | |
+ public ExpandableLayoutCheckBoxes(Context context) { | |
+ super(context); | |
+ init(); | |
+ } | |
+ | |
+ @TargetApi(Build.VERSION_CODES.HONEYCOMB) | |
+ public ExpandableLayoutCheckBoxes(Context context, AttributeSet attrs, int defStyleAttr) { | |
+ super(context, attrs, defStyleAttr); | |
+ init(); | |
+ } | |
+ | |
+ @TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
+ public ExpandableLayoutCheckBoxes(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
+ super(context, attrs, defStyleAttr, defStyleRes); | |
+ init(); | |
+ } | |
+ | |
+ private void init() { | |
+ | |
+ | |
+ LayoutInflater.from(getContext()).inflate(R.layout.widget_expandable_layout_check_boxes, this, true); | |
+ | |
+ headerLayout = findViewById(R.id.headerLayout); | |
+ | |
+ headerTextView = (TextView) findViewById(R.id.textHeader); | |
+ | |
+ rowsLayout = (LinearLayout) findViewById(R.id.rowsLayout); | |
+ | |
+ imgHeaderArrow = (ImageView) findViewById(R.id.imgArrow); | |
+ | |
+ | |
+ headerLayout.setOnClickListener(new OnClickListener() { | |
+ @Override | |
+ public void onClick(View view) { | |
+ if (rowsLayout.isShown()) { | |
+ rowsLayout.setVisibility(View.GONE); | |
+ imgHeaderArrow.setImageResource(R.drawable.ic_expand_more_black_24dp); | |
+ } else { | |
+ rowsLayout.setVisibility(View.VISIBLE); | |
+ imgHeaderArrow.setImageResource(R.drawable.ic_expand_less_black_24dp); | |
+ } | |
+ | |
+ } | |
+ }); | |
+ | |
+ } | |
+ | |
+ public void setItemsList(List<String> itemsList) { | |
+ | |
+ | |
+ for (int i = 0; i < itemsList.size(); i++) { | |
+ | |
+ View row = LayoutInflater.from(getContext()).inflate(R.layout.widget_expandable_layout_check_boxes_row, null); | |
+ | |
+ final TextView rowText = (TextView) row.findViewById(R.id.textViewRow); | |
+ final CheckBox rowCheckBox = (CheckBox) row.findViewById(R.id.checkBoxRow); | |
+ | |
+ rowText.setText(itemsList.get(i)); | |
+ | |
+ rowCheckBox.setId(generateId()); | |
+ Log.d(TAG, "setItemsList: " + rowCheckBox.getId()); | |
+ rowsLayout.addView(row); | |
+ | |
+ | |
+ row.setOnClickListener(new OnClickListener() { | |
+ @Override | |
+ public void onClick(View view) { | |
+ | |
+ rowCheckBox.toggle(); | |
+ | |
+ onTextCheckBoxRowClickListener.onClick(rowText, rowCheckBox); | |
+ | |
+ } | |
+ }); | |
+ | |
+ | |
+ | |
+ | |
+ } | |
+ | |
+ } | |
+ | |
+ public void setTextCheckBoxRowClickListener(OnTextCheckBoxRowClickListener listener) { | |
+ | |
+ onTextCheckBoxRowClickListener = listener; | |
+ } | |
+ | |
+ public void setHeaderColor(int color) { | |
+ headerTextView.setTextColor(color); | |
+ } | |
+ | |
+ public void setHeaderText(CharSequence headerText) { | |
+ headerTextView.setText(headerText); | |
+ } | |
+ | |
+ interface OnTextCheckBoxRowClickListener { | |
+ void onClick(TextView rowText, CheckBox rowCheckBox); | |
+ } | |
+ | |
+ | |
+ | |
+ // Reference: http://stackoverflow.com/questions/1714297/android-view-setidint-id-programmatically-how-to-avoid-id-conflicts/ | |
+ | |
+ private static final AtomicInteger sNextGeneratedId = new AtomicInteger(5000); | |
+ | |
+ /** | |
+ * Generate a value suitable for use in {@link #setId(int)}. | |
+ * This value will not collide with ID values generated at build time by aapt for R.id. | |
+ * | |
+ * @return a generated ID value | |
+ */ | |
+ static int generateId() { | |
+ | |
+ | |
+ for (; ; ) { | |
+ final int result = sNextGeneratedId.get(); | |
+ // aapt-generated IDs have the high byte nonzero; clamp to the range under that. | |
+ int newValue = result + 1; | |
+ if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0. | |
+ if (sNextGeneratedId.compareAndSet(result, newValue)) { | |
+ return result; | |
+ } | |
+ } | |
+ | |
+ | |
+ | |
+ } | |
+} | |
diff --git a/app/src/main/res/layout/widget_card_filters_layout.xml b/app/src/main/res/layout/widget_card_filters_layout.xml | |
index 44afeb4..c460c5a 100644 | |
--- a/app/src/main/res/layout/widget_card_filters_layout.xml | |
+++ b/app/src/main/res/layout/widget_card_filters_layout.xml | |
@@ -15,16 +15,30 @@ | |
android:orientation="vertical" | |
android:visibility="gone"> | |
- <include layout="@layout/widget_card_filters_library_types" /> | |
+ <!--<include layout="@layout/widget_card_filters_library_types" />--> | |
+ | |
+ <name.vampidroid.ui.widget.ExpandableLayoutCheckBoxes | |
+ android:id="@+id/expandableLibraryTypes" | |
+ android:layout_width="match_parent" | |
+ android:layout_height="wrap_content"> | |
+ | |
+ </name.vampidroid.ui.widget.ExpandableLayoutCheckBoxes> | |
<View | |
style="@style/NavigationViewDivider" | |
android:layout_width="match_parent" /> | |
- <include | |
- layout="@layout/widget_card_filters_library_disciplines" | |
- /> | |
+ <!--<include--> | |
+ <!--layout="@layout/widget_card_filters_library_disciplines"--> | |
+ <!--/>--> | |
+ | |
+ <name.vampidroid.ui.widget.ExpandableLayoutCheckBoxes | |
+ android:id="@+id/expandableLibraryDisciplines" | |
+ android:layout_width="match_parent" | |
+ android:layout_height="wrap_content"> | |
+ | |
+ </name.vampidroid.ui.widget.ExpandableLayoutCheckBoxes> | |
<View | |
style="@style/NavigationViewDivider" | |
@@ -52,7 +66,13 @@ | |
style="@style/NavigationViewDivider" | |
android:layout_width="match_parent" /> | |
- <include layout="@layout/widget_card_filters_crypt_clans" /> | |
+ <!--<include layout="@layout/widget_card_filters_crypt_clans" />--> | |
+ <name.vampidroid.ui.widget.ExpandableLayoutCheckBoxes | |
+ android:id="@+id/expandableCryptClans" | |
+ android:layout_width="match_parent" | |
+ android:layout_height="wrap_content"> | |
+ | |
+ </name.vampidroid.ui.widget.ExpandableLayoutCheckBoxes> | |
<View | |
style="@style/NavigationViewDivider" | |
diff --git a/app/src/main/res/layout/widget_expandable_layout_check_boxes.xml b/app/src/main/res/layout/widget_expandable_layout_check_boxes.xml | |
new file mode 100644 | |
index 0000000..5a3268f | |
--- /dev/null | |
+++ b/app/src/main/res/layout/widget_expandable_layout_check_boxes.xml | |
@@ -0,0 +1,57 @@ | |
+<?xml version="1.0" encoding="utf-8"?> | |
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
+ xmlns:tools="http://schemas.android.com/tools" | |
+ xmlns:app="http://schemas.android.com/apk/res-auto" | |
+ android:orientation="vertical" | |
+ android:layout_width="match_parent" | |
+ android:layout_height="match_parent"> | |
+ | |
+ <android.support.constraint.ConstraintLayout | |
+ android:id="@+id/headerLayout" | |
+ android:layout_width="match_parent" | |
+ android:layout_height="wrap_content" | |
+ android:background="?attr/selectableItemBackground" | |
+ android:clickable="true" | |
+ android:paddingBottom="16dp" | |
+ android:paddingLeft="8dp" | |
+ android:paddingTop="16dp"> | |
+ | |
+ <TextView | |
+ android:id="@+id/textHeader" | |
+ android:layout_width="wrap_content" | |
+ android:layout_height="wrap_content" | |
+ android:layout_marginBottom="3dp" | |
+ android:layout_marginLeft="16dp" | |
+ android:layout_marginStart="16dp" | |
+ android:text="Row Text:" | |
+ android:textAppearance="@style/TextAppearance.AppCompat.Subhead" | |
+ app:layout_constraintBottom_toBottomOf="parent" | |
+ app:layout_constraintLeft_toLeftOf="parent" | |
+ tools:layout_constraintBottom_creator="1" | |
+ tools:layout_constraintLeft_creator="1" /> | |
+ | |
+ <ImageView | |
+ android:id="@+id/imgArrow" | |
+ android:layout_width="wrap_content" | |
+ android:layout_height="wrap_content" | |
+ android:layout_marginEnd="8dp" | |
+ android:layout_marginRight="8dp" | |
+ android:src="@drawable/ic_expand_more_black_24dp" | |
+ app:layout_constraintBottom_toBottomOf="parent" | |
+ app:layout_constraintRight_toRightOf="parent" | |
+ tools:layout_constraintBottom_creator="1" | |
+ tools:layout_constraintRight_creator="1" /> | |
+ | |
+ </android.support.constraint.ConstraintLayout> | |
+ | |
+ <LinearLayout | |
+ android:id="@+id/rowsLayout" | |
+ android:layout_width="match_parent" | |
+ android:layout_height="wrap_content" | |
+ android:orientation="vertical" | |
+ android:visibility="gone" | |
+ tools:visibility="visible" | |
+ | |
+ > | |
+ </LinearLayout> | |
+</LinearLayout> | |
\ No newline at end of file | |
diff --git a/app/src/main/res/layout/widget_expandable_layout_check_boxes_row.xml b/app/src/main/res/layout/widget_expandable_layout_check_boxes_row.xml | |
new file mode 100644 | |
index 0000000..b19967b | |
--- /dev/null | |
+++ b/app/src/main/res/layout/widget_expandable_layout_check_boxes_row.xml | |
@@ -0,0 +1,39 @@ | |
+<?xml version="1.0" encoding="utf-8"?> | |
+<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
+ xmlns:app="http://schemas.android.com/apk/res-auto" | |
+ xmlns:tools="http://schemas.android.com/tools" | |
+ | |
+ android:id="@+id/layoutRow" | |
+ android:layout_width="match_parent" | |
+ android:layout_height="wrap_content" | |
+ android:background="?attr/selectableItemBackground" | |
+ android:clickable="true" | |
+ android:paddingLeft="8dp"> | |
+ | |
+ | |
+ <TextView | |
+ android:id="@+id/textViewRow" | |
+ android:text="Celerity" | |
+ android:layout_width="wrap_content" | |
+ android:layout_height="wrap_content" | |
+ android:layout_marginStart="16dp" | |
+ app:layout_constraintLeft_toLeftOf="parent" | |
+ android:layout_marginLeft="16dp" | |
+ android:textAppearance="?android:attr/textAppearanceMedium" | |
+ app:layout_constraintBaseline_toBaselineOf="@+id/checkBoxRow" /> | |
+ | |
+ <CheckBox | |
+ android:id="@+id/checkBoxRow" | |
+ android:layout_width="wrap_content" | |
+ android:layout_height="wrap_content" | |
+ android:layout_marginEnd="16dp" | |
+ app:layout_constraintRight_toRightOf="parent" | |
+ android:layout_marginRight="16dp" | |
+ app:layout_constraintTop_toTopOf="parent" | |
+ app:layout_constraintBottom_toBottomOf="parent" | |
+ android:background="@android:color/transparent" | |
+ android:checked="false" | |
+ android:clickable="false" | |
+ android:layout_marginTop="4dp" | |
+ android:layout_marginBottom="4dp" /> | |
+</android.support.constraint.ConstraintLayout> | |
\ No newline at end of file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment