Skip to content

Commit

Permalink
Support mulitple columns in select widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 committed Oct 9, 2018
1 parent e0a156f commit 124aa9a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,6 +40,8 @@
import java.util.ArrayList;
import java.util.List;

import timber.log.Timber;

public abstract class SelectWidget extends QuestionWidget {

/**
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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()) {
Expand Down
4 changes: 1 addition & 3 deletions collect_app/src/main/res/layout/recycler_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ limitations under the License.
-->
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="android.support.v7.widget.LinearLayoutManager">
android:layout_height="wrap_content">

</android.support.v7.widget.RecyclerView>

0 comments on commit 124aa9a

Please sign in to comment.