扩展另一个 Stackoverflow 问题,我实现了一些手势检测代码,以便我可以检测列表视图(位于 FrameLayout 中)中的行何时被滑动。我在这里关注了达米安关于如何从适配器获取单个行/视图的问题/答案。如何获取列表视图中行的位置(在屏幕上)
我的 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, MotionEvent e2, float velocityX, 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;
}
}
这对我有用,希望你能让它发挥作用,或者它至少给你一些提示。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)