0

Scenario:

I've created an app drawer and placed a seekbar within it. This app drawer is made from app_drawer.xml whilst my fragment below is made up from main_activity.xml. The issue I'm having is, although the seekbar shows up perfectly and the app drawer pulls in and out as it should, when I drag it nothing happens (By which I mean the textview is NOT updated). Even though it should.

I've been told the below is a fix, but I keep getting the error:

Cannot make a static reference to the non-static method setOnTouchListener(View.OnTouchListener) from the type View

How do I get rid of the error?

Fix using proposed code:

     public class MainActivity extends Fragment {

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {

                View rootView = inflater.inflate(R.layout.main_activity, container, false);
                View rootView2 = inflater.inflate(R.layout.app_drawer, container, false);



         final SeekBar sk=(SeekBar) rootView2.findViewById(R.id.seekBar1);  
         SeekBar.setOnTouchListener(new WorkarroundFixMovementOnDrawer());

         TextView textProgress = (TextView)rootView.findViewById(R.id.TextView01);

              sk.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {   

              public void onProgressChanged(SeekBar bar, int progress,
                    boolean fromUser) {

 textProgress.setText("Progress: "+ String.valueOf(progress));
    }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }
              });

                return rootView;
            }
        }

    /**
     * This is just a workarround for make seekbar work on drawer
     */
    static class WorkarroundFixMovementOnDrawer implements View.OnTouchListener {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            int action = event.getAction();
            DrawerLayout parentDrawer = getParentDrawer(v);

            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    // Disallow Drawer to intercept touch events.
                    if (parentDrawer != null) {
                        parentDrawer.requestDisallowInterceptTouchEvent(true);
                    }
                    break;

                case MotionEvent.ACTION_UP:
                    // Allow Drawer to intercept touch events.
                    if (parentDrawer != null) {
                        parentDrawer.requestDisallowInterceptTouchEvent(false);
                    }
                    break;
            }

            // Handle seekbar touch events.
            v.onTouchEvent(event);
            return true;

        }

        /**
         * Try to get DrawerLayout from parent
         * @param view view to search
         * @return the drawerLayout parent
         */
        public static DrawerLayout getParentDrawer(View view) {

            if (view != null) {

                ViewParent recursiveView = view.getParent();
                while (!(recursiveView instanceof DrawerLayout)) {

                    if (recursiveView == null) {
                        return null;
                    }

                    recursiveView = recursiveView.getParent();
                }

                return (DrawerLayout) recursiveView;

            }

            return null;

        }

    }

Orignal code:

       public class MainActivity extends Fragment {

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {

                View rootView = inflater.inflate(R.layout.main_activity, container, false);
                View rootView2 = inflater.inflate(R.layout.app_drawer, container, false);



         final SeekBar sk=(SeekBar) rootView2.findViewById(R.id.seekBar1);  
         TextView textProgress = (TextView)rootView.findViewById(R.id.TextView01);

              sk.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {   

              public void onProgressChanged(SeekBar bar, int progress,
                    boolean fromUser) {

 textProgress.setText("Progress: "+ String.valueOf(progress));
    }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }
              });

                return rootView;
            }
        }

Proposed Fix:

seekBar.setOnTouchListener(new WorkarroundFixMovementOnDrawer());


/**
 * This is just a workarround for make seekbar work on drawer
 */
static class WorkarroundFixMovementOnDrawer implements View.OnTouchListener {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        int action = event.getAction();
        DrawerLayout parentDrawer = getParentDrawer(v);

        switch (action) {
            case MotionEvent.ACTION_DOWN:
                // Disallow Drawer to intercept touch events.
                if (parentDrawer != null) {
                    parentDrawer.requestDisallowInterceptTouchEvent(true);
                }
                break;

            case MotionEvent.ACTION_UP:
                // Allow Drawer to intercept touch events.
                if (parentDrawer != null) {
                    parentDrawer.requestDisallowInterceptTouchEvent(false);
                }
                break;
        }

        // Handle seekbar touch events.
        v.onTouchEvent(event);
        return true;

    }

    /**
     * Try to get DrawerLayout from parent
     * @param view view to search
     * @return the drawerLayout parent
     */
    public static DrawerLayout getParentDrawer(View view) {

        if (view != null) {

            ViewParent recursiveView = view.getParent();
            while (!(recursiveView instanceof DrawerLayout)) {

                if (recursiveView == null) {
                    return null;
                }

                recursiveView = recursiveView.getParent();
            }

            return (DrawerLayout) recursiveView;

        }

        return null;

    }

}
Raghunandan
  • 132,755
  • 26
  • 225
  • 256

2 Answers2

0

setOntouchListener is a interface.

You have

final SeekBar sk=(SeekBar) rootView2.findViewById(R.id.seekBar1);  
SeekBar.setOnTouchListener(new WorkarroundFixMovementOnDrawer());

Should be

sk.setOnTouchListener(new WorkarroundFixMovementOnDrawer());

Coz You have

class WorkarroundFixMovementOnDrawer implements View.OnTouchListener 

Your WorkarroundFixMovementOnDrawer implements the interface View.OnTouchListener.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

Change

TextView textProgress = (TextView)rootView.findViewById(R.id.TextView01);

to

textProgress = (TextView)rootView.findViewById(R.id.TextView01);

and add an Object to your class

public class MainActivity extends Fragment {
TextView textProgress;
Barışcan Kayaoğlu
  • 1,294
  • 3
  • 14
  • 35
  • Add this to your original code. The problem is that you create the reference textProgress in runtime which you may not create it, but you use it in your interface as well. You either need to have the object that means at least you get a null pointer when you access the reference, or declare it static so that it will be created before runtime. – Barışcan Kayaoğlu Mar 12 '14 at 15:40