1

我在 NavigationDrawer 的 ScroolView 的线性布局中使用搜索栏

NavigationDrawer > ScrollView > LinearLayout > SeekBar + 其他东西

当我触摸搜索栏时,我无法上下滚动,我想这样做。

这是我的功能,它允许我滑动搜索栏而不是滑动导航抽屉

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);

    int i;
    for (i = 0; i < mTitles.length; i++) {
        View mViewElement = inflater.inflate(R.layout.item_critere, null);
        ((TextView) mViewElement.findViewById(R.id.critere_title)).setText(mTitles[i]);
        SeekBar mSeekBar = (SeekBar) mViewElement.findViewById(R.id.critere_qualifier);
        mSeekBar.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;
            }
        });
        mLinearLayout.addView(mViewElement);
    }
    return mView;
}

现在我需要的是:

如果您触摸搜索栏并且水平滑动,请使用搜索栏。

否则,如果您触摸搜索栏并且垂直滑动,请使用滚动视图

我怎样才能做到这一点 ?

提前致谢 :)

4

1 回答 1

0

这是一个有效的 VerticalSeekBar 实现:

package android.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class VerticalSeekBar extends SeekBar {

    public VerticalSeekBar(Context context) {
        super(context);
    }

    public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public VerticalSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldh, oldw);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    protected void onDraw(Canvas c) {
        c.rotate(-90);
        c.translate(-getHeight(), 0);

        super.onDraw(c);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isEnabled()) {
            return false;
        }

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
                setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));
                onSizeChanged(getWidth(), getHeight(), 0, 0);
                break;

            case MotionEvent.ACTION_CANCEL:
                break;
        }
        return true;
    }
}

要实现它,请在您的项目中创建一个新类,选择正确的包:

在那里,粘贴代码并保存。现在在您的 XML 布局中使用它:

<android.widget.VerticalSeekBar
  android:id="@+id/seekBar1"
  android:layout_width="wrap_content"
  android:layout_height="200dp"
  />
于 2013-08-23T12:44:19.037 回答