Search code examples
androidseekbar

Android with SeekBar - Prevent 'thumb' from moving when touching SeekBar to drag/scroll screen


I've got an android app with a TableLayout with each row having a SeekBar and a ToggleButton. The rows go beyond the visible range of the screen so it's scrollable. When I touch and drag to scroll the page up and in the process touch the SeekBar, it immediately changes the position of the "thumb" instead of scrolling the page. However, the ToggleButton does not behave this way; instead, I can start the drag on the button and scroll then release without the ToggleButton changing state.

Is there anyway to get the SeekBar to behave this way, such that touching it to start a drag will not cause the bar to change positions but instead scroll the page?


Solution

  • I've had similar problems. The only solution I could find (when the seekbar is in a listview) was to disable the seekbar until the item is clicked.

    Solution

    In the ArrayAdapter I set both enabled and focusable to false and added the SeekBar listener, setting the attributes to false allowed me to use the list item onItemClicked listener. Inside the onItemCLickListener I retreived the seekbar, set the attributes to true, this meant it could be slid up or down. I then disabled it after the adjustment had been made. code below

    ArrayAdapter Snippet

    this code is inside the creation of the list item, in which the seekbar is housed

        seekBar.setClickable(false);
        seekBar.setFocusable(false);
        seekBar.setEnabled(false);
    
        /* attach listener */
        attachProgressUpdatedListener(seekBar, positionOfItemInList);
    

    AttachProgressUpdatedListener

    this method attaches the listener to the seekbar, inside the arrayAdapter class

    private void attachProgressUpdatedListener(SeekBar seekBar,
        final int position) {
    
    seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
    
        public void onStopTrackingTouch(SeekBar seekBar) {
        int progress = seekBar.getProgress();
        int task_id = (Integer) seekBar.getTag();
    
        TaskHandler taskHandler = new TaskHandler(DBAdapter
            .getDBAdapterInstance(getContext()));
    
        taskHandler.updateTaskProgress(task_id, progress);
    
        mList.get(position).setProgress(progress);
    
        //need to fire an update to the activity
        notifyDataSetChanged();
        seekBar.setEnabled(false);
    
        }
    
        public void onStartTrackingTouch(SeekBar seekBar) {
        // empty as onStartTrackingTouch listener not being used in
        // current implementation
    
        }
    
        public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        // empty as onProgressChanged listener not being used in
        // current implementation
    
        }
    });
    
    }
    

    OnItemCLickListener

    this snipped is from the activity which houses the list view.

    taskListView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        SeekBar sb = (SeekBar) view.findViewById(R.id.seek);
        sb.setFocusable(true);
        sb.setEnabled(true);
    
        }
    });