Question

In my application, i used fragments. I want to expand the fragment when i swipe the screen left to right. I tried below code but not expanding. How can i ?

    public class LeftFrag extends Fragment implements OnGestureListener {
        private GestureDetector gDetector;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.leftfrag, container, false);

            TextView tv1 = (TextView)view.findViewById(R.id.text1);

            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
            Date date = new Date();
            tv1.setText(dateFormat.format(date));
            gDetector = new GestureDetector(getActivity(), this);

            view.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    gDetector.onTouchEvent(event);
                    return true;
                }
            });

            return view;
        }

        @Override
        public boolean onDown(MotionEvent e) {
            // TODO Auto-generated method stub
            //return false;
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            int SWIPE_MIN_DISTANCE = 50; //120;
            int SWIPE_THRESHOLD_VELOCITY = 100; //200;

            String str = "Velocity vx " + velocityX + " vy " + velocityY + " sx " + e1.getX() + " sy " + e1.getY() + " ex " + e2.getX() + " ey " + e2.getY();
            Log.d("onFling", str );

    //        TextView tv = (TextView)findViewById(R.id.text);
    //        tv.setText(str);


    //        Log.d("Velocity", "sx " + e1.getX() + " sy " + e1.getY() + " ex " + e2.getX() + " ey " + e2.getY() + " Velocity1 " + velocityX + " Velocity2 " + velocityY);

             if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE &&
                     Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                 //From Right to Left
                 Toast.makeText(getActivity(), "From Right to Left " + str, Toast.LENGTH_LONG).show();
                 str = "From Right to Left " + str;
                 return true;
            }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE &&
                         Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                //From Left to Right
                Toast.makeText(getActivity(), "From Left to Right " + str, Toast.LENGTH_LONG).show();
                str = "From Left to Right " + str;
                getActivity().getWindow().setLayout(LayoutParams.MATCH_PARENT , LayoutParams.MATCH_PARENT );
                getFragmentManager().findFragmentById(R.id.leftfrag);
                return true;
            }

            if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE &&
                        Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                Toast.makeText(getActivity(), "From Bottom to Top " + str, Toast.LENGTH_LONG).show();
                str = "From Bottom to Top " + str;
                //From Bottom to Top
                return true;
            }  else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE &&
                        Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                //From Top to Bottom
                Toast.makeText(getActivity(), "From Top to Bottom " + str, Toast.LENGTH_LONG).show();
                str = "From Top to Bottom " + str;
                str = str + " 1233";
                return true;
            }
            return false;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                float distanceY) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public void onShowPress(MotionEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            // TODO Auto-generated method stub
            return false;
        }
    }

enter image description here

enter image description here

Was it helpful?

Solution

Anyway i got it... my updated code

    public class LeftFrag extends Fragment implements OnGestureListener {
        private GestureDetector gDetector;
        private int nWidth = 0, nHeight = 0;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.leftfrag, container, false);



            TextView tv1 = (TextView)view.findViewById(R.id.text1);

            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
            Date date = new Date();
            tv1.setText(dateFormat.format(date));
            gDetector = new GestureDetector(getActivity(), this);

            view.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    gDetector.onTouchEvent(event);
                    return true;
                }
            });

            return view;
        }

        @Override
        public boolean onDown(MotionEvent e) {
            // TODO Auto-generated method stub
            //return false;
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            int SWIPE_MIN_DISTANCE = 50; //120;
            int SWIPE_THRESHOLD_VELOCITY = 100; //200;

            String str = "Velocity vx " + velocityX + " vy " + velocityY + " sx " + e1.getX() + " sy " + e1.getY() + " ex " + e2.getX() + " ey " + e2.getY();
            Log.d("onFling", str );

    //        TextView tv = (TextView)findViewById(R.id.text);
    //        tv.setText(str);


    //        Log.d("Velocity", "sx " + e1.getX() + " sy " + e1.getY() + " ex " + e2.getX() + " ey " + e2.getY() + " Velocity1 " + velocityX + " Velocity2 " + velocityY);

             if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE &&
                     Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                 //From Right to Left
                 Toast.makeText(getActivity(), "From Right to Left " + str, Toast.LENGTH_LONG).show();
                 str = "From Right to Left " + str;

                 View view = getView();
                 LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(nWidth, nHeight);
                 view.setLayoutParams(p);
                 view.requestLayout();       
                 return true;
            }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE &&
                         Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                //From Left to Right
                Toast.makeText(getActivity(), "From Left to Right " + str, Toast.LENGTH_LONG).show();
                str = "From Left to Right " + str;
                nWidth = getView().getWidth();
                nHeight = getView().getHeight();
                Log.e("Log", "Width = " + nWidth + " Height = " + nHeight );

                View view = getView();
                LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
                view.setLayoutParams(p);
                view.requestLayout();       
                return true;
            }

            if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE &&
                        Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                Toast.makeText(getActivity(), "From Bottom to Top " + str, Toast.LENGTH_LONG).show();
                str = "From Bottom to Top " + str;
                //From Bottom to Top
                return true;
            }  else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE &&
                        Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                //From Top to Bottom
                Toast.makeText(getActivity(), "From Top to Bottom " + str, Toast.LENGTH_LONG).show();
                str = "From Top to Bottom " + str;
                str = str + " 1233";
                return true;
            }
            return false;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                float distanceY) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public void onShowPress(MotionEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            // TODO Auto-generated method stub
            return false;
        }
    }

Now fragment expanded and collapsed well

thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top