With the help of two classes it is simple to implement what you want.
A class to encapsulate an ImageView with the selected / unselected states.
It is responsible for saving the state and the resources ids of the images of each of the states:
SelectableImage.java
public class SelectableImage {
private final int selectedImageResource;
private final int unSelectedImageResource;
private boolean isSelected;
public SelectableImage(int selectedImageResource, int unSelectedImageResource){
this.selectedImageResource = selectedImageResource;
this.unSelectedImageResource = unSelectedImageResource;
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
public int getImageResource(){
return isSelected ? selectedImageResource : unSelectedImageResource;
}
}
Because, in a set (list) of images, only one image can be selected, we have created a class to manage this situation.
It is responsible for saving the SelectableImage list and ensuring that when one is selected, the previous one is deselected:
SelectableImageGroup.java
public class SelectableImageGroup {
private ArrayList<SelectableImage> list;
private SelectableImage selectedImage;
public SelectableImageGroup(){
list = new ArrayList<>();
}
public void add(SelectableImage selectableImage){
list.add(selectableImage);
}
public void setSelectedImage(SelectableImage selectedImage){
if(this.selectedImage == selectedImage)return;
if(!list.contains(selectedImage))throw new IndexOutOfBoundsException();
if (this.selectedImage != null) {
this.selectedImage.setSelected(false);
}
selectedImage.setSelected(true);
this.selectedImage = selectedImage;
}
public void setSelectedPosition(int position){
SelectableImage selectedImage = list.get(position);
setSelectedImage(selectedImage);
}
public ArrayList<SelectableImage> getArray(){
return list;
}
}
Now just implement the ListView as usual.
SelectableImageGroupAdapter.java Adapter
public class SelectableImageGroupAdapter extends ArrayAdapter<SelectableImage> {
private final LayoutInflater layoutInflater;
private final Context context;
private final int listItemResource;
private final SelectableImageGroup selectableImageGroup;
public SelectableImageGroupAdapter(Context context, int listItemResource,
SelectableImageGroup selectableImageGroup) {
super(context, listItemResource, selectableImageGroup.getArray());
this.context = context;
this.listItemResource = listItemResource;
this.selectableImageGroup = selectableImageGroup;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(view == null){
view = layoutInflater.inflate(listItemResource, parent, false);
}
final SelectableImage selectableImage = getItem(position);
ImageView imageView = (ImageView)view.findViewById(R.id.image);
imageView.setImageResource(selectableImage.getImageResource());
//Ver nota
/*imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectableImageGroup.setSelectedImage(selectableImage);
notifyDataSetChanged();
}
});*/
return view;
}
}
Activity Layout: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Each item in the ListView must have an ImageView
List_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
To use:
public class MainActivity extends AppCompatActivity {
private SelectableImageGroup selectableImageGroup;
private SelectableImageGroupAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectableImageGroup = new SelectableImageGroup();
selectableImageGroup.add(new SelectableImage(R.drawable.imagem1,
R.drawable.imagemselecionada1));
selectableImageGroup.add(new SelectableImage(R.drawable.imagem2,
R.drawable.imagemselecionada2));
selectableImageGroup.add(new SelectableImage(R.drawable.imagem3,
R.drawable.imagemselecionada3));
selectableImageGroup.add(new SelectableImage(R.drawable.imagem4,
R.drawable.imagemselecionada4));
selectableImageGroup.add(new SelectableImage(R.drawable.imagem5,
R.drawable.imagemselecionada5));
ListView listView = (ListView)findViewById(R.id.listView);
adapter = new SelectableImageGroupAdapter(this,
R.layout.list_item, selectableImageGroup);
listView.setAdapter(adapter);
//**** Ver nota ******
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectableImageGroup.setSelectedPosition(position);
adapter.notifyDataSetChanged();
}
});
//*********************
}
}
Note:
As the code is, what makes switching between the selected and deselected state is a click on the ListView row.
If you want this switch to be performed only by clicking ImageView , comment the code in Activity and uncomment the adapter >