无法在scrollview中拖动搜索栏

时间:2014-05-20 04:51:29

标签: android scrollview seekbar

如果在其他地方已经回答了这个问题我很抱歉 - 我找不到任何关于同样问题的人。

我将SeekBar添加到位于嵌套ScrollView内的RelativeLayout。但是,我无法拖动搜索栏的拇指。我可以沿着它的路径点击来改变它的值,但我无法拖动它。任何帮助将不胜感激。

我的XML看起来像:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/containerView"
android:background="@android:color/transparent"
android:padding="0dip"
android:layout_margin="0dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent">


<ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scrollbarStyle="insideOverlay"
        android:id="@+id/scrollView" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"
        android:fillViewport="true">
    <HorizontalScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scrollbarStyle="insideInset"
            android:id="@+id/horizontalScrollView" android:fillViewport="true">
        <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:id="@+id/scrollContainer">
        </RelativeLayout>
    </HorizontalScrollView>
</ScrollView>
</RelativeLayout>

我的Java是:

SeekBar thisElement = new SeekBar(getActivity());
thisElement.setEnabled(true);
thisElement.setLayoutParams(lp);
thisElement.setAlpha(alphaVal);
thisElement.setMinimumHeight(elementHeight);
final int min = Integer.parseInt(getJsonPropertyValue(tmpJson, "elementSliderMinimum", "0"));
int max = Integer.parseInt(getJsonPropertyValue(tmpJson, "elementSliderMaximum", "100"));
int def = Integer.parseInt(getJsonPropertyValue(tmpJson, "elementSliderDefault", "50"));
thisElement.setMax(max - min);
thisElement.setProgress(def - min);
thisElement.setTag(elementCounter);
thisElement.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        String msg = "onTouch:" + v.getClass().toString();
        Log.w("ZZ",msg);
        return false;
    }
});
thisElement.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        String elementSliderAction = getJsonPropertyValue(tmpJson, "elementSliderAction", "");
        if (elementSliderAction.equalsIgnoreCase("changeLabel")) {
            int labelNumber = -1;
            String elementSliderLabel = getJsonPropertyValue(tmpJson, "elementSliderLabel", "");
            for (int j=0; j<childItems.size(); j++) {
                BT_item tmpElement = childItems.get(j);
                if (tmpElement.getItemId().equalsIgnoreCase(elementSliderLabel)) {
                    TextView labelToChange = (TextView)scrollContainer.findViewWithTag(j+1000);
                    String newVal = "" + (progress - min);
                    labelToChange.setText(newVal);
                }
            }
        }
        else if (elementSliderAction.equalsIgnoreCase("saveToPref")) {
            String elementSliderPrefName = getJsonPropertyValue(tmpJson, "elementSliderPrefName", "");
            String newVal = "" + (progress - min);
            setPrefString(elementSliderPrefName, newVal);
        }

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
});
scrollContainer.addView(thisElement);

我怀疑这是因为我的XML文件的结构方式,但我不确定正确的解决方案是什么。我可以偶尔让它拖动,但只有在经过一遍又一遍的尝试之后。此外,OnTouchListener正确地将其识别为我尝试拖动时触摸的SeekBar。

2 个答案:

答案 0 :(得分:16)

  

试试这个

thisElement.setOnTouchListener(new SeekBar.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 Seekbar touch events.
            v.onTouchEvent(event);
            return true;
        }
    });

答案 1 :(得分:0)

您可以使用以下代码

thisElement.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
        scrolview1.setOverScrollMode(ScrollView.OVER_SCROLL_NEVER);
        return false;
    }
});

它适用于你