【问题标题】:SeekBar in a NavigationDrawerNavigationDrawer 中的 SeekBar
【发布时间】:2019-09-08 15:21:39
【问题描述】:

我想在导航抽屉中使用搜索栏

基本上我需要左右滑动来设置搜索栏,你猜对了,它会滑动导航抽屉......

我想要做的是在焦点集中时滑动搜索栏,在没有焦点时滑动导航抽屉。

我该怎么做?

这是我设置项目时的代码(我还没有设置搜索栏)

private View onCritereView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View mView = inflater.inflate(R.layout.fragment_critere, container, false);
    LinearLayout mLinearLayout = (LinearLayout) mView.findViewById(R.id.critere_linearlayout);

    View mViewElement = inflater.inflate(R.layout.item_critere, null);
    ((TextView)mViewElement.findViewById(R.id.critere_title)).setText("Prix Maximum");

    mLinearLayout.addView(mViewElement);

    return mView;
}

这是带有搜索栏的item_critere.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/critere_item"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="*">

    <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Large Text"
                android:id="@+id/critere_title"
                android:layout_gravity="left|center_vertical"
                android:layout_column="0"/>

        <SeekBar
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/critere_qualifier"
                android:layout_column="1"
                android:layout_span="4"/>
    </TableRow>

    <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Medium Text"
                android:id="@+id/critere_value"
                android:layout_span="4"/>
    </TableRow>

</TableLayout>

提前致谢:)

【问题讨论】:

    标签: android seekbar navigation-drawer


    【解决方案1】:

    只需添加一个onTouchListener。当您在搜索栏(action_down)上触摸屏幕时,不允许父级拦截事件。

    seekbar.setOnTouchListener(new ListView.OnTouchListener() 
    {
        @Override
        public boolean onTouch(View v, MotionEvent event) 
        {
            int action = event.getAction();
            switch (action) 
            {
            case MotionEvent.ACTION_DOWN:
                // Disallow Drawer to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(true);
                break;
    
            case MotionEvent.ACTION_UP:
                // Allow Drawer to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(false);
                break;
            }
    
            // Handle seekbar touch events.
            v.onTouchEvent(event);
            return true;
        }
    });
    

    【讨论】:

    • 这实际上是为了在 ScrollView 中使用 ListViews 而复制的。所以它是新的 SeekBar.OnTouchListener 而不是 ListView.onTouchListener。这是一种非常简单的方法,可以解决很多此类问题
    • 现在更难了.. x) 我在导航抽屉和搜索栏之间添加了一个滚动视图。我只想禁用导航抽屉的interceptTouch事件而不是滚动视图x)这可能吗?
    • 所以你的抽屉里有一个带有水平搜索栏的垂直滚动条,对吧?
    • 我认为它应该可以正常工作,而无需进一步实施。如果您触摸搜索栏,则只会为搜索栏捕获事件。如果您触摸滚动视图中的其他位置,一切都会按原样进行
    • Arf nop :/ 滚动条与导航抽屉一起使用。它区分水平/垂直运动。但是在 seekbar 上,水平运动不会被 seekbar 或 navigationdrawer 捕捉到。
    【解决方案2】:

    使用DrawerLayout.setDrawerLockMode

    而不是ViewParent.requestDisallowInterceptTouchEvent

    在@dumazy 中回答requestDisallowInterceptTouchEvent 解决了这个问题,但它也产生了另一个问题。

    当您在导航抽屉上滚动时,如果您触摸 SeekBar,OnSeekBarChangeListener 将触发。因此,滚动事件将中断,并且搜索栏进度将通过移动手指开始改变。

    我对原来的答案稍作修改,使用DrawerLockMode解决了这个问题:

    mSeekBar.setOnTouchListener(new SeekBar.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            switch (action) {
    
                case MotionEvent.ACTION_DOWN:
                    // Disallow Drawer to intercept touch events.
                    // v.getParent().requestDisallowInterceptTouchEvent(true);
                    mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
                    break;
    
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    // Allow Drawer to intercept touch events.
                    // v.getParent().requestDisallowInterceptTouchEvent(false);
                    mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNDEFINED);
                    break;
            }
    
            // Handle SeekBar touch events.
            v.onTouchEvent(event);
            return true;
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-26
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-07
      • 2013-08-13
      • 2018-12-11
      相关资源
      最近更新 更多