Provide filter when there are many options

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2047233003

Review-Url: https://codereview.chromium.org/2047233003
This commit is contained in:
liyuqian 2016-06-08 11:17:09 -07:00 committed by Commit bot
parent ecbc12b1c1
commit 2a437e6459
3 changed files with 95 additions and 7 deletions

View File

@ -0,0 +1,64 @@
package org.skia.viewer;
import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Spinner;
import java.util.ArrayList;
import java.util.List;
public class OptionAdapter extends ArrayAdapter<String> {
private String mCurrentOption;
private List<String> mOptions;
private Filter mFilter = null;
private Spinner mSpinner;
public OptionAdapter(Context context, int resource, List<String> options, Spinner spinner) {
super(context, resource);
addAll(options);
mOptions = options;
mSpinner = spinner;
}
public void setCurrentOption(String currentOption) {
this.mCurrentOption = currentOption;
}
private class OptionFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final String pattern = constraint.toString().toLowerCase();
ArrayList<String> filteredOptions = new ArrayList<>();
for(String option : mOptions) {
if (option.equals(mCurrentOption)
|| option.toLowerCase().indexOf(pattern) > -1) {
filteredOptions.add(option);
}
}
FilterResults results = new FilterResults();
results.values = filteredOptions;
results.count = filteredOptions.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
clear();
List<String> filteredOptions = (List<String>) results.values;
addAll(filteredOptions);
// We set the selection to the current option to avoid unwanted option selection change
mSpinner.setSelection(filteredOptions.indexOf(mCurrentOption));
notifyDataSetChanged();
}
}
@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new OptionFilter();
}
return mFilter;
}
}

View File

@ -1,12 +1,14 @@
package org.skia.viewer;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
@ -32,6 +34,7 @@ public class StateAdapter extends BaseAdapter implements AdapterView.OnItemSelec
static final String VALUE = "value";
static final String OPTIONS = "options";
private static final String BACKEND_STATE_NAME = "Backend";
private static final int FILTER_LENGTH = 20;
ViewerActivity mViewerActivity;
LinearLayout mLayout;
@ -86,7 +89,7 @@ public class StateAdapter extends BaseAdapter implements AdapterView.OnItemSelec
}
private View inflateItemView(JSONObject item) throws JSONException {
View itemView = LayoutInflater.from(mViewerActivity).inflate(R.layout.state_item, null);
LinearLayout itemView = (LinearLayout) LayoutInflater.from(mViewerActivity).inflate(R.layout.state_item, null);
TextView nameText = (TextView) itemView.findViewById(R.id.nameText);
TextView valueText = (TextView) itemView.findViewById(R.id.valueText);
Spinner optionSpinner = (Spinner) itemView.findViewById(R.id.optionSpinner);
@ -97,15 +100,31 @@ public class StateAdapter extends BaseAdapter implements AdapterView.OnItemSelec
valueText.setText(value);
valueText.setVisibility(View.VISIBLE);
optionSpinner.setVisibility(View.GONE);
} else {
ArrayList<String> optionList = new ArrayList<>();
String[] optionStrings = new String[options.length()];
for (int j = 0; j < options.length(); j++) {
optionList.add(options.getString(j));
}
optionSpinner.setAdapter(new ArrayAdapter<String>(mViewerActivity,
android.R.layout.simple_spinner_dropdown_item, optionList));
final OptionAdapter adapter = new OptionAdapter(mViewerActivity,
android.R.layout.simple_spinner_dropdown_item, optionList, optionSpinner);
adapter.setCurrentOption(value);
optionSpinner.setAdapter(adapter);
if (optionStrings.length >= FILTER_LENGTH) {
EditText filterText = new EditText(mViewerActivity);
filterText.setHint("Filter");
itemView.addView(filterText, 1);
filterText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int cnt, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int cnt) {}
@Override
public void afterTextChanged(Editable s) {
adapter.getFilter().filter(s.toString());
}
});
}
optionSpinner.setSelection(optionList.indexOf(value));
optionSpinner.setOnItemSelectedListener(this);
optionSpinner.setVisibility(View.VISIBLE);

View File

@ -114,11 +114,16 @@ int SkiaAndroidApp::message_callback(int fd, int events, void* data) {
break;
}
case kSurfaceChanged: {
SkASSERT(message.fNativeWindow == skiaAndroidApp->fNativeWindow &&
message.fNativeWindow);
SkASSERT(message.fNativeWindow);
int width = ANativeWindow_getWidth(skiaAndroidApp->fNativeWindow);
int height = ANativeWindow_getHeight(skiaAndroidApp->fNativeWindow);
auto window_android = (Window_android*)skiaAndroidApp->fWindow;
if (message.fNativeWindow != skiaAndroidApp->fNativeWindow) {
window_android->onDisplayDestroyed();
ANativeWindow_release(skiaAndroidApp->fNativeWindow);
skiaAndroidApp->fNativeWindow = message.fNativeWindow;
window_android->initDisplay(skiaAndroidApp->fNativeWindow);
}
window_android->setContentRect(0, 0, width, height);
window_android->paintIfNeeded();
break;