即使触摸了搜索栏,也会滚动

时间:2013-08-23 12:39:42

标签: android scrollview seekbar motionevent

我在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;
}

现在我需要的是:

  

如果你触摸搜索栏,如果你滑动,请使用搜索栏。

     

否则,如果您触摸搜索栏,如果您滑动verticaly,请使用scrollview

我该怎么做?

提前致谢:)

1 个答案:

答案 0 :(得分: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"
  />