19

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

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

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

我该怎么做?

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

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>

提前致谢 :)

4

2 回答 2

35

只需添加一个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;
    }
});
于 2013-08-23T11:23:41.327 回答
0

采用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;
    }
});
于 2019-07-12T14:27:01.723 回答