ListView不会在AndroidSlidingUpPanel中滚动

时间:2014-01-07 23:24:30

标签: android listview slidingmenu

我有一个带有列表视图的AndroidSlidingUpPanel作为孩子。 我的问题是当窗格展开时我尝试滚动ListView,而是滚动窗格。 我发现我必须覆盖onInterceptTouchEvent并将特定区域设置为MotionEvent.ACTION_DOWN(或类似的东西),但我真的迷失了。

你能帮助我,告诉我如何解决这个问题,我的代码是:

<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

2 个答案:

答案 0 :(得分: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;
        }
    });

答案 1 :(得分:2)

我一直在研究同样的问题。只需使用以下内容:

mSlidingPanel.setEnableDragViewTouchEvents(true);