Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot drag seekbar inside scrollview

I apologize if this question has been answered elsewhere - I couldn't find anyone on SO with the same problem.

I added a SeekBar to a RelativeLayout that sits inside a nested ScrollView. However, I am unable to drag the thumb of the seekbar. I can click along its path to change it's value, but I can't drag it. Any help would be appreciated.

My XML looks like:

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

And my Java is:

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

I suspect this is because of the way my XML file is structured, but am not sure what the correct solution is. I can occasionally get it to drag, but only after trying over and over and over. Also, the OnTouchListener correctly identifies it as the SeekBar that's being touched when I try to drag.

like image 908
user3654985 Avatar asked May 20 '14 04:05

user3654985


1 Answers

try this

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;
        }
    });
like image 59
Xar-e-ahmer Khan Avatar answered Nov 15 '22 06:11

Xar-e-ahmer Khan