Android OnFling和其他方法不';无法在ListView中处理TextView
我需要帮助…:-) 我有一个自定义的ListView适配器。这些项目是文本视图。 我希望能够在每个TextView上处理onSingleTap、onFling和所有其他事件。 但只有onDown活动有效!其他人都不要!我不明白为什么。在我的另一个活动中,TextView不是ListView的一部分,一切都正常 这就是我所拥有的:Android OnFling和其他方法不';无法在ListView中处理TextView,android,android-listview,textview,gesturedetector,Android,Android Listview,Textview,Gesturedetector,我需要帮助…:-) 我有一个自定义的ListView适配器。这些项目是文本视图。 我希望能够在每个TextView上处理onSingleTap、onFling和所有其他事件。 但只有onDown活动有效!其他人都不要!我不明白为什么。在我的另一个活动中,TextView不是ListView的一部分,一切都正常 这就是我所拥有的: public class ListViewAdapter extends BaseAdapter{ ... private GestureDetector txtP
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!..
}
}
公共类ListViewAdapter扩展了BaseAdapter{
...
私人手势检测器txtProductGD;
private View.OnTouchListener txtProductGL;
...
公共ListViewAdapter(上下文、ArrayList产品){
...
txtProductGL=new View.OnTouchListener(){
公共布尔onTouch(视图v,运动事件){
txtProductGD=新的GestureDetector(新的txtProductGestureDetector((TextView)v));
返回txtProductGD.onTouchEvent(事件);
}
};
...
}
公共视图getView(int位置、视图转换视图、视图组父视图){
...
视图=充气机。充气(R.layout.listview_项,空);
TextView TextView=(TextView)view.findViewById(R.id.txtProduct);
setOnTouchListener(txtProductGL);
...
}
私有类txtProductGestureDetector扩展了SimpleOnGestureListener{
私有文本视图文本视图;
公共txtProductGestureDetector(文本视图文本视图){
this.textView=textView;
}
公共布尔onDown(运动事件e){
textView.setText(“onDown…”);//它可以工作!
返回false;
}
公共布尔值OnSingleTapConfiged(运动事件e){
textView.setText(“OnSingleTapConfiged…”);//它不工作!
返回false;
}
//所有其他方法都是无效的!。。
}
}
我不知道您是否出于其他原因使用自定义类txtProductGestureDetector
,但如果您只是想覆盖特定视图的方法,请尝试以下操作:
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;
}
}
});
它可以放在getView
方法的右边,并将touchListener分配给textView