我已经研究了一段时间。这个想法开始很简单,我想要一个 SlidingDrawer handle 上的按钮,以允许用户查看特定于抽屉内容的设置。所以我做了一个布局,旁边有一个按钮,并将其设置为 handle 。抽屉画得很好,但不允许按下按钮(在 Handlebars 上)。每当我尝试点击这个东西时,点击都会被解释为 handle 点击,并切换抽屉的状态。
有人知道这是怎么回事吗?
谢谢~Aedon
最佳答案
我会发布我的实现,以免其他人遇到麻烦。
基本上,您必须扩展 SlidingDrawer 类并处理 onInterceptTouch 事件,以便当它们位于句柄布局内的项目顶部时通过。
这假设您正在为 handle 使用 ViewGroup(例如任何布局),并且其中的所有 View 都是可点击的。
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SlidingDrawer;
public class ClickableSlidingDrawer extends SlidingDrawer {
private ViewGroup mHandleLayout;
private final Rect mHitRect = new Rect();
public ClickableSlidingDrawer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ClickableSlidingDrawer(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
View handle = getHandle();
if (handle instanceof ViewGroup) {
mHandleLayout = (ViewGroup) handle;
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (mHandleLayout != null) {
int childCount = mHandleLayout.getChildCount();
int handleClickX = (int)(event.getX() - mHandleLayout.getX());
int handleClickY = (int)(event.getY() - mHandleLayout.getY());
Rect hitRect = mHitRect;
for (int i=0;i<childCount;i++) {
View childView = mHandleLayout.getChildAt(i);
childView.getHitRect(hitRect);
if (hitRect.contains(handleClickX, handleClickY)) {
return false;
}
}
}
return super.onInterceptTouchEvent(event);
}
}
然后,在您的布局 .xml 中使用 <my.package.name.ClickableSlidingDrawer>
而不是 <SlidingDrawer>
关于android - 将按钮添加到 SlidingDrawer 句柄?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5666693/