I had a similar scenario, and I decided that the problem was creating a custom mesh element with a boolean field to keep track of whether the element was selected or not, and then select the element accordingly through the user adapter. The following is an example of what I did:
(1) I created a custom mesh element with a boolean field, which we will call selectedStatus for simplicity. I also added the appropriate methods to the grid element class to get the selected status:
public boolean getSelectedStatus () { return selectedStatus; } public void setSelectedStatus (boolean paramSelectedStatus) { this.selectedStatus = paramSelectedStatus; }
(2) Then I created a custom Adapter that extends BaseAdapter process the created custom mesh object. In this Adapter I check whether the selected state of the mesh object is selected true or false and selects the element shown below accordingly:
@Override public View getView (final int position, View convertView, ViewGroup parent) { // rest of getView() code... if (!yourGridObject.getSelectedStatus()) { convertView.setBackgroundColor(Color.TRANSPARENT); } else { convertView.setBackgroundColor(Color.LTGRAY); } // rest of getView() code... return convertView; }
(3) Finally, you add onItemClickListener to set the selected state and background color of the grid elements when they are selected (click):
yourGridView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { YourGridObject yourGridObject = (YourGridObject) parent.getItemAtPosition(position); if (!yourGridObject.getSelected()) { view.setBackgroundColor(Color.LTGRAY); yourGridObject.setSelected(true); } else { view.setBackgroundColor(Color.TRANSPARENT); yourGridObject.setSelected(false); } } });
Implementing the selection in this way ensures that the selection (selection) of the grid elements does not change when the number of columns and rows changes, since the selection status is contained within the grid objects themselves.
source share