2015-08-22 88 views
2

我有一個包含ListView的滑動窗格佈局。我有一個按鈕來打開滑動窗格。所以我想禁用這個滑動窗格的滑動手勢,當我嘗試訪問同一個佈局中的任何其他視圖時,這會產生問題。爲SlidingPaneLayout禁用滑動手勢

是否有反正只能使滑動手勢被禁用,並有按鈕功能應該工作?它應該像平常一樣按下按鈕來打開和關閉滑動窗格。

下面是我的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(); 
      } 

     } 
    }); 
+0

感謝編輯@rummler –

回答

8

在您的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

感謝@jone,它的工作 –

+1

如果** onInterceptTouchEvent()**返回false,**的onTouchEvent()**將不會被調用。因此,重寫** onTouchEvent()**不起任何作用,也不需要。 ** https://developer.android.com/training/gestures/viewgroup.html** – Arash

0

我不使用從SDK slidingpanel爲按鈕功能的代碼,但這個com.sothree.slidinguppanel其中工程完美。這個庫不存在這個問題。