setOnClickListener loses the default animation

Asked

Viewed 33 times

1

When setting the setOnClickListener to set the action of an item in my listview, the default animation of the click in the list is lost. I’d like to keep up the excitement and run my routine.

@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View item = LayoutInflater.from(context).inflate(resource, parent, false);
    item.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //MyActions
        }
    });
    return item;
}

I tried something like super.onClick but it didn’t work. Someone would know how to solve?

Valeeu!

inserir a descrição da imagem aqui

  • Good night friend.... probably your listview is losing focus. this happens if the items in your listview contain Buttons or imageButtons. Note if this is your case. If yes, add the xml property of Button or imageButton: focusableInTouch="false"

  • Thanks for the return friend. The item I display is exactly this one that is in the gif. In any case I put android:focusableInTouchMode="false" in Imageview, but did not fix. To make the gif I commented on the part that adds the Listener and then the click effect works again normal. Anyway thank you very much for the strength.

1 answer

0

Try to use the AdapterView.OnItemClickListener in listview to handle clicks. Discard onClickListener of the item in getView of your Adapter for now.

@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View item = LayoutInflater.from(context).inflate(resource, parent, false);
    // ...
    return item;
}

Set Onitemclicklistener in your Listview

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO: substituir "object" pela classe usada para popular a lista
        Object itemClicado = (Object) parent.getItemAtPosition(position);
    }
});

Browser other questions tagged

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