【问题标题】:ListView not scrolls in AndroidSlidingUpPanelListView 在 AndroidSlidingUpPanel 中不滚动
【发布时间】:2014-01-07 23:24:30
【问题描述】:

我小时候有一个带有列表视图的 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

【问题讨论】:

    标签: android listview slidingmenu


    【解决方案1】:

    我为这个问题找到了一个非常简单的解决方案: 唯一需要的更改是为 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;
            }
        });
    

    【讨论】:

    • 首先我使用了这个解决方案,但是因为我需要让父级拦截MotionEvent,所以没有成功。我在 AndroidSlidingUpPanel 中评论了整个onInterceptTouchEvent,现在它就像一个魅力。我只是好奇他们为什么要添加大约 80 行代码,而这些代码是不需要的——是的,我是认真的。
    • @galvan 我做了同样的事情,但我无法滚动列表视图。还有什么事情要做或问题出在哪里?
    • @iDroidExplorer 请验证您是否具有相同的布局结构,因此您的列表父级将是相同的。
    【解决方案2】:

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

    mSlidingPanel.setEnableDragViewTouchEvents(true);
    

    【讨论】:

      猜你喜欢
      • 2013-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多