为 SlidingPaneLayout 禁用滑动手势

问题描述 投票:0回答:4

我有一个滑动窗格布局,其中包含一个

ListView
。我有一个按钮可以打开滑动窗格。因此,我想禁用此滑动窗格的滑动手势,这在我尝试访问同一布局中的任何其他视图时会产生问题。

有没有办法只禁用滑动手势并让按钮功能正常工作?它应该像往常一样在单击按钮时打开和关闭滑动窗格。

下面是我的 XML 布局的一部分:

<android.support.v4.widget.SlidingPaneLayout
    android:id="@+id/sliding_pane_layout_accountHome"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff" >

    <LinearLayout
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:background="#FFFFFF">

        <ListView
            android:id="@+id/lv_menuList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </LinearLayout>
</android.support.v4.widget.SlidingPaneLayout>

这里是按钮功能的代码

slidingLayout1 = (SlidingPaneLayout) findViewById(R.id.sliding_pane_layout_accountHome);
slidingLayout1.openPane();

iv_menu=(ImageView)findViewById(R.id.iv_menu);
    iv_menu.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) 
        {
            if(slidingLayout1.isOpen()==true)
            {
                slidingLayout1.closePane();
            }
            else
            {
                slidingLayout1.openPane();
            }

        }
    });
android swipe-gesture slidingpanelayout
4个回答
11
投票

在您的 CustomSlidingPaneLayout 视图类中编写以下代码,并在您的 xml 文件中使用此自定义布局。它将完美地满足您的要求。

@Override
  public boolean onInterceptTouchEvent(MotionEvent ev) {
   return false;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    return false;
}

完全自定义视图类:

public class MySlidingPanelLayout extends SlidingPaneLayout {
// ===========================================================
// Constructors
// ===========================================================
public MySlidingPanelLayout(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

public MySlidingPanelLayout(Context context, AttributeSet attrs,
        int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}

public MySlidingPanelLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    return false;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    MyLog.i("MySlidingPanelLayout", "onTouch:");
    if (this.isOpen()) {
        this.closePane();
    }
    return false; // here it returns false so that another event's listener
                    // should be called, in your case the MapFragment
                    // listener
}
}

0
投票

我没有使用 SDK 中的滑动面板,但是这个 com.sothree.slidinguppanel 可以完美地工作。这个库没有这个问题。


0
投票

我知道这个线程很老但是如果有人想在不覆盖任何内容的情况下解决这个问题,您只想将布局菜单的

layoutParams
的宽度设置为零。

binding.clMenu.layoutParams.width = 0
其中
clMenu
是在
SlidingPaneLayout
内声明的第一个布局。


0
投票

根据https://developer.android.com/develop/ui/views/layout/twopane,您可以使用:

slidingPaneLayout.lockMode = SlidingPaneLayout.LOCK_MODE_LOCKED

© www.soinside.com 2019 - 2024. All rights reserved.