From 124aa9a49b7e52d7694379cb5a19d73256bcf9a2 Mon Sep 17 00:00:00 2001 From: grzegorz Date: Tue, 9 Oct 2018 03:24:15 +0200 Subject: [PATCH] Support mulitple columns in select widgets --- .../collect/android/widgets/SelectWidget.java | 29 +++++++++++++++++ .../android/widgets/WidgetFactory.java | 31 ++++++++++++------- .../src/main/res/layout/recycler_view.xml | 4 +-- 3 files changed, 49 insertions(+), 15 deletions(-) diff --git a/collect_app/src/main/java/org/odk/collect/android/widgets/SelectWidget.java b/collect_app/src/main/java/org/odk/collect/android/widgets/SelectWidget.java index 12c36576e46..16984084c26 100644 --- a/collect_app/src/main/java/org/odk/collect/android/widgets/SelectWidget.java +++ b/collect_app/src/main/java/org/odk/collect/android/widgets/SelectWidget.java @@ -18,6 +18,7 @@ import android.content.Context; import android.support.v7.widget.DividerItemDecoration; +import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.DisplayMetrics; import android.view.LayoutInflater; @@ -39,6 +40,8 @@ import java.util.ArrayList; import java.util.List; +import timber.log.Timber; + public abstract class SelectWidget extends QuestionWidget { /** @@ -165,6 +168,7 @@ public void addMediaFromChoice(MediaLayout mediaLayout, int index, TextView text protected RecyclerView setUpRecyclerView() { RecyclerView recyclerView = (RecyclerView) LayoutInflater.from(getContext()).inflate(R.layout.recycler_view, null); // keep in an xml file to enable the vertical scrollbar recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL)); + recyclerView.setLayoutManager(new GridLayoutManager(getContext(), getNumberOfColumns())); return recyclerView; } @@ -179,4 +183,29 @@ void adjustRecyclerViewSize(AbstractSelectListAdapter adapter, RecyclerView recy recyclerView.setNestedScrollingEnabled(false); } } + + private int getNumberOfColumns() { + String columnsAppearance = "columns"; + + int numberOfColumns = 1; + String appearance = WidgetFactory.getAppearance(getFormEntryPrompt()); + if (appearance != null) { + appearance = WidgetFactory.getAppearance(getFormEntryPrompt()); + + if (appearance.contains(columnsAppearance)) { + try { + appearance = + appearance.substring(appearance.indexOf(columnsAppearance), appearance.length()); + int idx = appearance.indexOf('-'); + if (idx != -1) { + numberOfColumns = Integer.parseInt(appearance.substring(idx + 1)); + } + } catch (Exception e) { + Timber.e("Exception parsing columns"); + } + } + } + + return numberOfColumns; + } } diff --git a/collect_app/src/main/java/org/odk/collect/android/widgets/WidgetFactory.java b/collect_app/src/main/java/org/odk/collect/android/widgets/WidgetFactory.java index fe0ea796b98..181a5f45a01 100644 --- a/collect_app/src/main/java/org/odk/collect/android/widgets/WidgetFactory.java +++ b/collect_app/src/main/java/org/odk/collect/android/widgets/WidgetFactory.java @@ -47,6 +47,24 @@ private WidgetFactory() { } + // Get appearance hint and clean it up so it is lower case, without the search function and never null. + static String getAppearance(FormEntryPrompt fep) { + String appearance = fep.getAppearanceHint(); + if (appearance == null) { + appearance = ""; + } else { + // For now, all appearance tags are in English. + appearance = appearance.toLowerCase(Locale.ENGLISH); + + // Strip out the search() appearance/function which is handled in ExternalDataUtil so that + // it is not considered when matching other appearances. For example, a file named list.csv + // used as a parameter to search() should not be interpreted as a list appearance. + appearance = ExternalDataUtil.SEARCH_FUNCTION_REGEX.matcher(appearance).replaceAll(""); + } + + return appearance; + } + /** * Returns the appropriate QuestionWidget for the given FormEntryPrompt. * @@ -57,18 +75,7 @@ private WidgetFactory() { public static QuestionWidget createWidgetFromPrompt(FormEntryPrompt fep, Context context, boolean readOnlyOverride) { - // Get appearance hint and clean it up so it is lower case and never null. - String appearance = fep.getAppearanceHint(); - if (appearance == null) { - appearance = ""; - } - // For now, all appearance tags are in English. - appearance = appearance.toLowerCase(Locale.ENGLISH); - - // Strip out the search() appearance/function which is handled in ExternalDataUtil so that - // it is not considered when matching other appearances. For example, a file named list.csv - // used as a parameter to search() should not be interpreted as a list appearance. - appearance = ExternalDataUtil.SEARCH_FUNCTION_REGEX.matcher(appearance).replaceAll(""); + String appearance = getAppearance(fep); final QuestionWidget questionWidget; switch (fep.getControlType()) { diff --git a/collect_app/src/main/res/layout/recycler_view.xml b/collect_app/src/main/res/layout/recycler_view.xml index 889cbd19c87..4f5d1d826b9 100644 --- a/collect_app/src/main/res/layout/recycler_view.xml +++ b/collect_app/src/main/res/layout/recycler_view.xml @@ -15,10 +15,8 @@ limitations under the License. --> + android:layout_height="wrap_content">