扩展另一个Stackoverflow问题,我已经实现了一些手势检测代码,以便我可以检测我的listview(在FrameLayout中)中的行何时被刷过.我跟着Damian的问题/答案,关于如何从适配器获取单独的行/视图.
How to get location (on screen) of row in listview
How to get location (on screen) of row in listview
我在我的onFling中有代码获取行的视图,并尝试在我的xml布局中设置为不可见的删除按钮可见.但是,这不会发生.我想知道如何在刷卡的列表视图中显示按钮?
class MyGestureDetector extends SimpleOnGestureListener { @Override public boolean onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY) { try { if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { int itemId = MyClass.this.lv.pointToPosition( (int) e1.getX(),(int) e1.getY()); Log.v("item id",String.valueOf(itemId)); View v = MyClass.this.adapter .getViewOnScreen(itemId); Button delete = (Button) v.findViewById(R.id.button_delete); delete.setVisibility(View.VISIBLE); //MyClass.this.adapter.notifyDataSetChanged(); } } catch (Exception e) { // nothing } return false; } }
我的列表适配器代码与引用的问题相同.
编辑:我尝试在列表视图上使用getChildAt()来获取行的视图,当有一个或更少的项目时,这可以工作,但是当返回错误的视图时,错误的删除按钮变为可见.
编辑2:我使用问题here上的答案让它工作:
解决方法
我曾经在我的应用程序中实现了类似的功能.我这样做的方式:
public class MyGestureDetector extends SimpleOnGestureListener { private ListView list; public MyGestureDetector(ListView list) { this.list = list; } //CONDITIONS ARE TYPICALLY VELOCITY OR DISTANCE @Override public boolean onFling(MotionEvent e1,float velocityY) { if (INSERT_CONDITIONS_HERE) if (showDeleteButton(e1)) return true; return super.onFling(e1,e2,velocityX,velocityY); } private boolean showDeleteButton(MotionEvent e1) { int pos = list.pointToPosition((int)e1.getX(),(int)e1.getY()); return showDeleteButton(pos); } private boolean showDeleteButton(int pos) { View child = list.getChildAt(pos); if (child != null){ Button delete = (Button) child.findViewById(R.id.delete_button_id); if (delete != null) if (delete.getVisibility() == View.INVISIBLE) delete.setVisibility(View.VISIBLE); else delete.setVisibility(View.INVISIBLE); return true; } return false; } }
这对我有用,希望你能让它发挥作用,或者它至少会给你一些暗示.