我有一个 ListFragment ,我使用ContentProvider填充。
我需要为每个人添加手势监听器,以便当用户向右滑动时,该项目将从列表中删除。
以下是我目前在ListFragment中的所有内容。
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
String selection = l.getItemAtPosition(position).toString();
Toast.makeText(v.getContext(), selection + "", Toast.LENGTH_LONG).show();
Log.d("TodoListFragment", selection+ "");
}
我如何附加手势检测和聆听?
我做了一些研究,发现了这段代码:
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.d("Swipe", "Left");
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.d("Swipe", "Right");
}
} catch (Exception e) {
// nothing
}
return false;
}
}
谁能告诉我如何实施它? (不是删除部分,而只是实现)
答案 0 :(得分:3)
我使用以下答案解决了整个问题。
它完美无缺。
class SideIndexGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.d("Scrolled- fragment ", "Scrolling");
return super.onScroll(e1, e2, distanceX, distanceY);
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final GestureDetector gestureDetector = new GestureDetector(getActivity(),
new SideIndexGestureListener());
View.OnTouchListener gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
};
getListView().setOnTouchListener(gestureListener);
try {
adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_single_choice, elementos);
setListAdapter(adapter);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}catch (Exception e){
}