禁用SlidingPaneLayout的滑动手势

5

我有一个包含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();
            }

        }
    });

感谢@rummler的编辑。 - Sai Chakradhar Sana
嘿,你能告诉我如何在那个滑动面板中添加滑动手势吗? - Aviroxi
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
}
}

1
如果 onInterceptTouchEvent() 返回 false,则不会调用 **onTouchEvent()**。因此,覆盖 onTouchEvent() 没有任何效果,也不需要这样做。 https://developer.android.com/training/gestures/viewgroup.html - Arash

0

我没有使用SDK中的slidingpanel,而是使用了com.sothree.slidinguppanel,这个库完美地工作。你在使用这个库时不会遇到这个问题。


0

0

我知道这个帖子已经很老了,但是如果有人想知道如何在不覆盖任何内容的情况下解决这个问题,你只需要将布局菜单的layoutParams宽度设置为零。

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


如何为SlidingUpPanel添加下滑手势? - Aviroxi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接