How to give a list of images the behavior of a RadioGroup?

1

I have 2 image vectors, one for the normal image and one for when it is selected. When selecting an image in ListView I need the other images not to be selected. I have tried in many ways and this was my last attempt:

Activity.java:

Integer vetor1[] = {R.drawable.imagem1, R.drawable.imagem2,R.drawable.imagem.3, R.drawable.imagem4, R.drawable.imagem5};
Integer vetor2[] = {R.drawable.imagemselecionada1, R.drawable.imagemselecionada2,R.drawable.imagemselecionada3, R.drawable.imagemselecionada4, R.drawable.imagemselecionada5};

protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
        Adapter adaptader =  new Adapter(EscolhaAvatar.this,vetor1,vetor2);
        ListView listaImagens = (ListView) findViewById(R.id.list);
        listaImagens.setAdapter(adapter);
        listaImagens.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

listaImagens.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                desabilitaImagem(view, position);
            }
        });
    }

public void desabilitaImagem(View v, int posicao) {
        ImageView img = (ImageView) v.findViewById(R.id.img);
        listaImagens.setAdapter(adapter);
        img.setImageResource(vetor2[posicao]);
    }

My idea was to recreate ListView and leave only the image in position as selected

    
asked by anonymous 26.06.2016 / 22:45

1 answer

0

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 >

    
28.06.2016 / 00:08