2014-01-07 120 views
2

我有一個AndroidSlidingUpPanel列表視圖作爲一個孩子。 我的問題是當窗格展開,我嘗試滾動ListView時,窗格被滾動。 我發現我必須重寫onInterceptTouchEvent並將特定區域設置爲MotionEvent.ACTION_DOWN(或類似的東西),但我真的迷失在此。ListView不滾動AndroidSlidingUpPanel

能否請你幫我,告訴我怎樣才能解決這個問題,我的代碼是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity" > 

<com.sothree.slidinguppanel.SlidingUpPanelLayout 
    xmlns:sothree="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/sliding_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    sothree:collapsedHeight="40dp" 
    sothree:dragView="@+id/name" 
    sothree:shadowHeight="1dp" > 

    <FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 



    </FrameLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#eee" 
     android:clickable="true" 
     android:focusable="false" > 

     <TextView 
      android:id="@+id/name" 
      android:layout_width="match_parent" 
      android:layout_height="40dp" 
      android:layout_centerHorizontal="true" 
      android:text="" 
      android:textSize="14sp" /> 

     <ListView 
      android:id="@+id/lv_menu" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:paddingTop="40dp" /> 
    </RelativeLayout> 
</com.sothree.slidinguppanel.SlidingUpPanelLayout> 

而且在MainActivity:

public class MainActivity extends Activity implements PanelSlideListener, OnItemClickListener { 
public static final String SAVED_STATE_ACTION_BAR_HIDDEN = "saved_state_action_bar_hidden"; 

private ListView _menuListView; 
private SlidingUpPanelLayout _slideUpPane; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY); 
    setContentView(R.layout.activity_main); 

    _slideUpPane = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout); 

    _slideUpPane.setShadowDrawable(getResources().getDrawable(R.drawable.above_shadow)); 
    _slideUpPane.setAnchorPoint(0.2f); 
    _slideUpPane.setPanelSlideListener(this); 

    ArrayList<MenuItem> menuItems = new ArrayList<MenuItem>(); 
    menuItems.add(new MenuItem("Menu Item")); 
    menuItems.add(new MenuItem("Menu Item")); 
    menuItems.add(new MenuItem("Menu Item")); 
    menuItems.add(new MenuItem("Menu Item")); 
    menuItems.add(new MenuItem("Menu Item")); 
    menuItems.add(new MenuItem("Menu Item")); 
    menuItems.add(new MenuItem("Menu Item")); 
    menuItems.add(new MenuItem("Menu Item")); 
    menuItems.add(new MenuItem("Menu Item")); 
    menuItems.add(new MenuItem("Menu Item")); 
    menuItems.add(new MenuItem("Menu Item")); 
    menuItems.add(new MenuItem("Menu Item")); 
    menuItems.add(new MenuItem("Menu Item")); 

    _menuListView = (ListView) findViewById(R.id.lv_menu); 
    _menuListView.setAdapter(new MenuAdapter(this, menuItems)); 
    _menuListView.setOnItemClickListener(this); 


    boolean actionBarHidden = savedInstanceState != null ? 
      savedInstanceState.getBoolean(SAVED_STATE_ACTION_BAR_HIDDEN, false): false; 
    if (actionBarHidden) { 
     getActionBar().hide(); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public void onPanelSlide(View panel, float slideOffset) { 
    Log.i(TAG, "onPanelSlide, offset " + slideOffset); 
    if (slideOffset < 0.2) { 
     if (getActionBar().isShowing()) { 
      getActionBar().hide(); 
     } 
    } else { 
     if (!getActionBar().isShowing()) { 
      getActionBar().show(); 
     } 
    } 

} 

@Override 
public void onPanelCollapsed(View panel) { 
    Log.i(TAG, "onPanelCollaped"); 
} 

@Override 
public void onPanelExpanded(View panel) { 
    Log.i(TAG, "onPanelExpanded"); 
} 

@Override 
public void onPanelAnchored(View panel) { 
    Log.i(TAG, "onPanelAnchored"); 
} 

@Override 
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
    _slideUpPane.collapsePane(); 
} 

}

我嘗試使用這些但我不知道如何去做: Android, SlidingPaneLayout and listView ListView in SlidingUpPanel is not scrollable

回答

9

我發現這個問題很容易解決: 所需要唯一的變化是定義爲ListView新onTouchListener。

的變化應該是這樣的:

_menuListView.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      int action = event.getAction(); 
      switch (action) { 
      case MotionEvent.ACTION_DOWN: 
       // Disallow ScrollView to intercept touch events. 
       v.getParent().requestDisallowInterceptTouchEvent(true); 
       break; 

      case MotionEvent.ACTION_UP: 
       // Allow ScrollView to intercept touch events. 
       v.getParent().requestDisallowInterceptTouchEvent(false); 
       break; 
      } 

      // Handle ListView touch events. 
      v.onTouchEvent(event); 
      return true; 
     } 
    }); 
+2

首先我使用了這個解決方案,但是因爲我需要讓父母攔截「MotionEvent」,所以它沒有解決問題。我在AndroidSlidingUpPanel中評論了整個'onInterceptTouchEvent',現在它就像一個魅力一樣。我只是好奇他們爲什麼要添加80行代碼,這是不需要的 - 是的,我很認真。 – seb

+0

@galvan我做了同樣的事情,但我無法滾動列表視圖。還有什麼更多要做或者問題在哪裏? –

+0

@iDroidExplorer請確認您具有相同的佈局結構,以便您的列表父項將相同。 – galvan

2

我一直在尋找進入了一段同樣的問題。只需使用以下內容:

mSlidingPanel.setEnableDragViewTouchEvents(true);