Change the color of the List View item if it is disabled - Android

Asked

Viewed 321 times

0

I have a normal listview that lists the names of my users.

And I would like disabled users to have the grey background of the list item. How to do this?

public class Usuario {

private String nome;
private String endereco;
private Boolean ativo;


//construtor
// getters e setters

}

That is, if active is true, leave normal. If false, leave gray.

How do I change the color of the List View item if it is disabled?

  • Are you using Adapter when completing Listview?

  • Arrayadpater himself. I didn’t perosnalize mine.

  • So, from what I’ve been reading, override in the "isEnabled" function of the BaseAdapter, i never tried!

1 answer

1

To do this properly, you need to create a Adapter which will receive your user, perform the active and inactive check and create an item from ListView for him.

You will need to:

  1. A class ( e. g. UsuarioListAdapter) extending BaseAdapter;
  2. A layout ( e. g. adapter_item_usuario),that the UsuarioListAdapter use to put the data of your class Usuario.

Get to work!

  • Create layout for list items:

    <?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" >
    
    <TextView android:id="@+id/textview_nome"/>
    
    <TextView android:id="@+id/textview_endereco"/>
    
    </RelativeLayout>
    
  • Now create the class that will extend the BaseAdapter:

    public class UsuarioListAdapter extends BaseAdapter {
    
        Context mContext;
        List<Usuario> mUsuarios = new ArrayList<Usuario>();
    
        public UsuarioListAdapter(Context context, List<Usuario> usuarios){
    
            mContext = context;
            mUsuarios = usuarios;
        }
    
        @Override
        public int getCount() {
            return mUsuarios.size();
        }
    
        @Override
        public Object getItem(int position) {
            return mUsuarios.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        // Neste método que é feita a verificação e a mudança de cor do item
        @Override
        public View getView(int position, View view, ViewGroup parent){
    
        LayoutInflater layoutInflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
        view = layoutInflater.inflate(R.layout.adapter_item_usuario,null);
    
        // Verificação e mudança de cor
        if(mUsuario.get(position).estaAtivo == false){
    
            view.setBackgroundResource(R.color.Grey);
    
        }
    
        TextView mTextviewNome = (TextView) view.findViewById(R.id.textview_noticia);
        TextView mTextViewEnderco = (TextView) view.findViewById(R.id.textview_noticia_data);
    
        mTextviewNome.setText(mUsuarios.get(position).getNome());
        mTextViewEnderco.setText(mUsuarios.get(position).getEndereco());
    
        return view;
    }
    

Now, instead of you creating a SimpleArrayAdapter, create a UsuarioListAdapter passing the correct parameters and setting it as your Adapter ListView.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.