I have a GridView implemented and activated the
mGridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
mode. Now I have the possibility to select multiple items from my grid when I perform a long click on one item. I want to achieve this behavior on a normal, short click. Is this possible?
First, I'd suggest to think if this user scenario is what you have been looking for. By default, in Android UX to select something you do long press and it's a pattern users used to. So, maybe you should rethink the whole flow.
Saying that, do you actually need GridView.CHOICE_MODE_MULTIPLE_MODAL
?
You can handle it on the Adapter level, by just storing selected positions and update this list in onClick
handler:
static final String[] numbers = new String[] {
"A", "B", "C", "D", "E",....
"U", "V", "W", "X", "Y", "Z"};
.....
gridView = (GridView) findViewById(R.id.gridView1);
final CustomAdapter adapter = new CustomAdapter(numbers);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
int selectedIndex = adapter.selectedPositions.indexOf(position);
if (selectedIndex > -1) {
adapter.selectedPositions.remove(selectedIndex);
((CustomView)v).display(false);
} else {
adapter.selectedPositions.add(position);
((CustomView)v).display(true);
}
}
});
Custom BaseAdapter
to display Custom views:
public class CustomAdapter extends BaseAdapter {
private String[] strings;
List<Integer> selectedPositions = new ArrayList<>();
CustomAdapter(String [] strings) {
this.strings = strings;
}
@Override
public int getCount() {
return strings.length;
}
@Override
public Object getItem(int position) {
return strings[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
CustomView customView = (convertView == null) ?
new CustomView(MainActivity.this) : (CustomView) convertView;
customView.display(strings[position], selectedPositions.contains(position));
return customView;
}
}
Custom View (in my case - cell with TextView). Xml:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/textView"
android:textColor="#FFF"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="60dp" />
</merge>
Code:
class CustomView extends FrameLayout {
TextView textView;
public CustomView(Context context) {
super(context);
LayoutInflater.from(context).inflate(R.layout.custom_view, this);
textView = (TextView)getRootView().findViewById(R.id.textView);
}
public void display(String text, boolean isSelected) {
textView.setText(text);
display(isSelected);
}
public void display(boolean isSelected) {
textView.setBackgroundColor(isSelected? Color.RED : Color.LTGRAY);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With