我创建了一个带有禁用滑动选项的SlidingPaneLayout
public class SlidingPaneLayoutNoSwipe extends SlidingPaneLayout {
public SlidingPaneLayoutNoSwipe(Context context) {
super(context);
}
public SlidingPaneLayoutNoSwipe(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SlidingPaneLayoutNoSwipe(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return false;
}
}
布局:其中includeSide
是侧边菜单,includeMain
是主要布局
<com.mypackage.SlidingPaneLayoutNoSwipe
android:id="@+id/slidingPaneMain"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/includeSide"
layout="@layout/layout_side"/>
<include
android:id="@+id/includeMain"
layout="@layout/layout_main"/>
</com.mypackage.SlidingPaneLayoutNoSwipe>
菜单由按钮打开,剩余的includeMain
上有一个叠加层
我想通过单击覆盖区域来关闭菜单
我尝试将onClickListener
添加到includeMain
的根布局:
<LinearLayout
android:id="@+id/layoutMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:background="@drawable/header_background"
android:orientation="vertical">
代码:
mLayoutMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mSlidingPane.isOpen()){
mSlidingPane.closePane();
}
}
});
但它不起作用。 我怎么能这样做?
答案 0 :(得分:0)
实际上答案比我想象的更简单:
public class SlidingPaneLayoutNoSwipe extends SlidingPaneLayout {
public SlidingPaneLayoutNoSwipe(Context context) {
super(context);
}
public SlidingPaneLayoutNoSwipe(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SlidingPaneLayoutNoSwipe(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if(isOpen()){
return super.onInterceptTouchEvent(ev);
}
return false;
}
}
现在可以点击&amp;滑动。