2012-11-16 39 views
1

我有一個列表視圖,其中包含聯繫號碼,因爲它的項目。如何處理左右刷卡

我希望當用戶通過聯繫人執行左向交換(向左交換)時,應用程序將開始呼叫該聯繫人。

我想知道如何處理/檢測左側滑動和右側滑過聯繫人以及如何檢測列表視圖滑動事件執行的項目。

感謝

+1

轉寄此ANS http://stackoverflow.com/a/3472181/1211980從http://stackoverflow.com/ questions/3471066/android-swipe-to-left-or-right?rq = 1個問題。 – SachinGutte

回答

0

使用onFling()方法來檢測刷卡,並且只要檢測到特定列表的刷卡被考慮,您將不得不提供自己的adapter並覆蓋它的getView()方法。
對於如

public class My_simple_adapter extends ArrayAdapter<String> //THIS IS THE CUSTOM ADAPTER 
{ 
private final Context context; 
private final String[] values; 

public My_simple_adapter(Context context,String[] values,int[] pos) 
{ 
    super(context,R.layout.list_item,values); 
    this.context=context; 
    this.values=values; 

} 

@Override 
public View getView(int position,View convert_view,ViewGroup parent) 
{ 
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View rowView = inflater.inflate(R.layout.MY_LAYTOUT, parent, false); 
    // THE ABOVE MY_LAYOUT IS THE LAYOUT YOU WANT TO LOAD FOR EACH ROW OF THE LISTVIEW 
    TextView textView = (TextView) rowView.findViewById(R.id.tv1); 
    textView.setText(values[position]); 

    //SUPPOSE rlt IS THE ID OF THE ABOVE MY_LAYOUT 
    //then 
    rlt.setOnTouchListener(new View.OnTouchListener() 
     { 
       public boolean onTouch(View view, MotionEvent event) { 
        Log.d("test", "clicked!"); 
        if(gestureDetector.onTouchEvent(event)) { 
         Log.d("test", "gesture detected"); 
         return true; 
        } 

        return false; 
       } 
      }); 

    return rowView; 
} 
} 


這是gestureDetector

SimpleOnGestureListener simpleOnGestureListener 
     = new SimpleOnGestureListener(){ 
    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
     float velocityY) { 
       boolean result; 
       //DECIDE UNDER WHAT SPEED OF SWIPING U WANT TO MAKE THE CALL 
       ... 
       ... 
      return result; 
    } 

    @Override 
    public boolean onDown(MotionEvent e) 
    { 

    return true; 
    } 

     }; 
//AND FINALLY 

    final GestureDetector gestureDetector 
      = new GestureDetector(simpleOnGestureListener);