1

我有一个列表视图,其中包含联系电话作为项目。

我希望当用户在联系人上执行左滑(向左交换)时,应用程序将开始呼叫该联系人。

我想知道如何处理/检测在联系人上向左滑动和向右滑动,以及如何检测在列表视图滑动事件的哪个项目上执行。

谢谢

4

2 回答 2

1

有一个关于在 Android 中处理手势的教程。核实!!

于 2012-11-16T11:25:48.760 回答
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;
 }
} 


这是手势检测器

 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);
于 2012-11-16T11:36:52.473 回答