3

J'ai besoin d'aide ... :-) J'ai un adaptateur personnalisé pour mon ListView. Les éléments sont TextViews. Je veux être capable de gérer onSingleTap, onFling et tous les autres événements sur chaque TextView. Mais seul l'événement onDown fonctionne! Tous les autres ne le font pas! Je ne comprends pas pourquoi. Dans mon autre activité où TextViews ne font pas partie d'un tout ListView fonctionne très bien ...OnFling et d'autres méthodes ne fonctionnent pas sur TextView dans ListView

C'est ce que j'ai:

public class ListViewAdapter extends BaseAdapter{ 
... 
private GestureDetector txtProductGD; 
private View.OnTouchListener txtProductGL; 
... 

public ListViewAdapter(Context context, ArrayList<Product> products) { 
    ... 
    txtProductGL = new View.OnTouchListener() { 
    public boolean onTouch(View v, MotionEvent event) { 
    txtProductGD = new GestureDetector(new txtProductGestureDetector((TextView) v)); 

    return txtProductGD.onTouchEvent(event); 
    } 
    }; 
    ... 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
... 
view = inflater.inflate(R.layout.listview_item, null); 

TextView textView = (TextView) view.findViewById(R.id.txtProduct); 

textView.setOnTouchListener(txtProductGL); 
... 
} 

private class txtProductGestureDetector extends SimpleOnGestureListener { 

    private TextView textView; 

    public txtProductGestureDetector(TextView textView) { 
    this.textView = textView; 
    } 

    public boolean onDown (MotionEvent e) { 
    textView.setText("onDown..."); // IT WORKS! 
    return false; 
    } 

    public boolean onSingleTapConfirmed (MotionEvent e) { 
    textView.setText("onSingleTapConfirmed..."); // IT DOESN'T WORK! 
    return false; 
    } 

    // ALL OTHER METHODS ARE ALSO DON'T WORK!..  
}     
} 

Répondre

0

Je ne sais pas si vous utilisez votre classe personnalisée pour txtProductGestureDetector une autre raison, mais si vous êtes à la recherche de passer outre les méthodes pour une vue spécifique, essayez ceci:

TextView textView = (TextView) view.findViewById(R.id.txtProduct); 
textView.setOnTouchListener(new OnTouchListener() { 
    public boolean onTouch(View v, MotionEvent event) { 
     switch(event.getAction()) { 
      case (MotionEvent.ACTION_DOWN) : 
       Log.d(DEBUG_TAG,"Action was DOWN"); 
       return true; 
      case (MotionEvent.ACTION_MOVE) : 
       Log.d(DEBUG_TAG,"Action was MOVE"); 
       return true; 
      case (MotionEvent.ACTION_UP) : 
       Log.d(DEBUG_TAG,"Action was UP"); 
       return true; 
      case (MotionEvent.ACTION_CANCEL) : 
       Log.d(DEBUG_TAG,"Action was CANCEL"); 
       return true;  
      default : 
       return true; 
      }    
     } 
}); 

qui peut être placé directement dans votre méthode getView et attribuera un touchListener à votre textView.

Questions connexes