Android在列表上滑动
有没有人有一个ListActivity在列中显示Textviews的简单的例子,当你从左到右滑动,你看到一个新的视图中的行? 这将是说编辑该行的数据或显示该行更详细的信息。 请不要引用代码将军或其他网站,因为我GOOGLE了,并没有看到这个答案。
我有同样的问题,我没有在这里find我的答案。
我想检测ListView项中的滑动操作,并将其标记为已被刷新,同时继续支持OnItemClick和OnItemLongClick。
这是我的解决scheme:
第一个SwipeDetector类:
import android.util.Log; import android.view.MotionEvent; import android.view.View; public class SwipeDetector implements View.OnTouchListener { public static enum Action { LR, // Left to Right RL, // Right to Left TB, // Top to bottom BT, // Bottom to Top None // when no action was detected } private static final String logTag = "SwipeDetector"; private static final int MIN_DISTANCE = 100; private float downX, downY, upX, upY; private Action mSwipeDetected = Action.None; public boolean swipeDetected() { return mSwipeDetected != Action.None; } public Action getAction() { return mSwipeDetected; } @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = event.getX(); downY = event.getY(); mSwipeDetected = Action.None; return false; // allow other events like Click to be processed case MotionEvent.ACTION_UP: upX = event.getX(); upY = event.getY(); float deltaX = downX - upX; float deltaY = downY - upY; // horizontal swipe detection if (Math.abs(deltaX) > MIN_DISTANCE) { // left or right if (deltaX < 0) { Log.i(logTag, "Swipe Left to Right"); mSwipeDetected = Action.LR; return false; } if (deltaX > 0) { Log.i(logTag, "Swipe Right to Left"); mSwipeDetected = Action.RL; return false; } } else if (Math.abs(deltaY) > MIN_DISTANCE) { // vertical swipe // detection // top or down if (deltaY < 0) { Log.i(logTag, "Swipe Top to Bottom"); mSwipeDetected = Action.TB; return false; } if (deltaY > 0) { Log.i(logTag, "Swipe Bottom to Top"); mSwipeDetected = Action.BT; return false; } } return false; } return false; } }
第二我在列表视图中使用滑动检测器类:
final ListView lv = getListView(); final SwipeDetector swipeDetector = new SwipeDetector(); lv.setOnTouchListener(swipeDetector); lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (swipeDetector.swipeDetected()){ // do the onSwipe action } else { // do the onItemClick action } } }); lv.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) { if (swipeDetector.swipeDetected()){ // do the onSwipe action } else { // do the onItemLongClick action } } });
这样我可以支持3个操作 – 刷卡,点击,长按,我可以使用ListView项目信息。
join后期:
由于ListView捕获一个滚动操作,有时很难轻扫。 为了解决这个问题,我对SwipeDetector.onTouch进行了如下修改:
public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { downX = event.getX(); downY = event.getY(); mSwipeDetected = Action.None; return false; // allow other events like Click to be processed } case MotionEvent.ACTION_MOVE: { upX = event.getX(); upY = event.getY(); float deltaX = downX - upX; float deltaY = downY - upY; // horizontal swipe detection if (Math.abs(deltaX) > HORIZONTAL_MIN_DISTANCE) { // left or right if (deltaX < 0) { Log.i(logTag, "Swipe Left to Right"); mSwipeDetected = Action.LR; return true; } if (deltaX > 0) { Log.i(logTag, "Swipe Right to Left"); mSwipeDetected = Action.RL; return true; } } else // vertical swipe detection if (Math.abs(deltaY) > VERTICAL_MIN_DISTANCE) { // top or down if (deltaY < 0) { Log.i(logTag, "Swipe Top to Bottom"); mSwipeDetected = Action.TB; return false; } if (deltaY > 0) { Log.i(logTag, "Swipe Bottom to Top"); mSwipeDetected = Action.BT; return false; } } return true; } } return false; }
这是我用来检测滑动的一个片段。 然后你可以使用一个viewflipper来改变视图。
@Override public boolean onTouchEvent(MotionEvent event) { if (gestureDetector.onTouchEvent(event)) { return true; } else { return false; } } private static final int SWIPE_MIN_DISTANCE = 30; private static final int SWIPE_MAX_OFF_PATH = 250; private static final int SWIPE_THRESHOLD_VELOCITY = 200; 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) { leftFling(); } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { rightFling(); } } catch (Exception e) { // nothing } return false; } }
这是一个非常简化的版本,使用两个监听器(onTouch进行轻扫检测,onClickIem进行项目单击检测),使用isSwipe标志停止onClickItemListener,直到确认它不是一个Swipe
检测点击
考虑到它不是首先滑动
listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { if(!isSwipe) { adapter.increase(arg2); adapter.notifyDataSetChanged(); } } });
检测刷卡
listView.setOnTouchListener(new OnTouchListener() { private int action_down_x = 0; private int action_up_x = 0; private int difference = 0; @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: action_down_x = (int) event.getX(); isSwipe=false; //until now break; case MotionEvent.ACTION_MOVE: if(!isSwipe) { action_up_x = (int) event.getX(); difference = action_down_x - action_up_x; if(Math.abs(difference)>50) { Log.d("action","action down x: "+action_down_x); Log.d("action","action up x: "+action_up_x); Log.d("action","difference: "+difference); //swipe left or right if(difference>0){ //swipe left Log.d("action","swipe left"); adapter.decrease(selectedItem); adapter.notifyDataSetChanged(); } else{ //swipe right Log.d("action","swipe right"); } isSwipe=true; } } break; case MotionEvent.ACTION_UP: Log.d("action", "ACTION_UP - "); action_down_x = 0; action_up_x = 0; difference = 0; break; } return false; //to allow the clicklistener to work after } })
如果你想显示一些button的动作,当一个列表项是轻扫的互联网上有很多图书馆有这种行为。 我实施了我在互联网上find的图书馆,我非常满意。 使用起来非常简单,而且非常快捷。 我改进了原来的库,我添加了一个新的点击监听器的项目点击。 此外,我添加了字体真棒图书馆( http://fortawesome.github.io/Font-Awesome/ ),现在你可以简单地添加一个新的项目标题,并从字体真棒指定图标名称。
这里是github链接