android_guides icon indicating copy to clipboard operation
android_guides copied to clipboard

Problem with ArrayAdapter and event onTouch

Open michferrero opened this issue 3 years ago • 0 comments

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

michferrero avatar Sep 06 '22 15:09 michferrero