codepath / android_guides

Extensive Open-Source Guides for Android Developers
guides.codepath.com
MIT License
28.31k stars 6.35k forks source link

Problem with ArrayAdapter and event onTouch #414

Open michferrero opened 2 years ago

michferrero commented 2 years ago

Hi, I have a problem with using the ArrayAdapter and the onTouch event in my Android application. I would need that when pressing a button contained in the displayed row, the list is updated without losing the onTouch event.

This is my code:

public class SlotsItemAdapter extends ArrayAdapter<SlotItem>  {
private Button slotPTT = null;
...
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    ...
       slotPTT.setOnTouchListener(new View.OnTouchListener() {
            @SuppressLint("ResourceAsColor")
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                  if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {

                       // During the pression of button I need to change the button following an external event

                  } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                      // This action is not performed, but I get the MotionEvent.ACTION_CANCEL

                  }
            }
       });
    }
}

During the pression of button slotPTT I need to change the background color following an external event. I set the modify in the model and I call "notifyDataSetChanged" that refresh all the view called getView. The getView metod refresh all the component and also the onTouch event causing the lost the focus and the event ActionUp don't fire.

I haven't been able to solve the problem for a few days. Do you have any ideas about it? Thank you.

Michele