我有一个列表视图,其中包含联系人号码作为项目。
我希望当用户通过联系人执行leftswipe(向左交换)时,应用程序将开始调用该联系人。
我想知道如何处理/检测左侧滑动和向右滑动触点,以及如何检测列表视图滑动事件的执行项目。
感谢
答案 0 :(得分:1)
有一个关于处理 Gestures in Android的教程。检查!!
答案 1 :(得分: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);