【发布时间】:2026-01-20 13:20:08
【问题描述】:
扩展另一个 * 问题,我已经实现了一些手势检测代码,以便我可以检测我的列表视图(在 FrameLayout 中)中的一行何时被刷过。我在这里关注了 Damian 的问题/答案,关于如何从适配器获取单个行/视图。 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;
}
}
我的列表适配器代码与引用的问题相同。
编辑:我尝试在 listview 上使用 getChildAt() 来获取行的视图,当有一个或更少的项目时,这有效,但是当返回的视图超过错误的视图时,错误的删除按钮变为可见。
编辑 2:我使用问题的答案 here 让它工作:
【问题讨论】: