Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple columns in select widgets #2621

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,25 @@ 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.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 @@ -34,6 +34,7 @@
import java.util.List;
import java.util.Locale;

import io.reactivex.annotations.NonNull;
import timber.log.Timber;

/**
Expand All @@ -47,6 +48,25 @@ private WidgetFactory() {

}

// Get appearance hint and clean it up so it is lower case, without the search function and never null.
@NonNull
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 +77,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>