简体   繁体   English

从片段上向右滑动即可打开“抽屉布局”

[英]Open Drawer Layout by right swipe from fragment

I have drawer layout and it's working fine. 我有抽屉式布局,工作正常。 But i want to open navigation drawer after right-swipe on the fragment. 但是我想在片段上向右滑动后打开导航抽屉。 Can you help me with this. 你能帮我吗

Here is my listener. 这是我的听众。

public class OnSwipeTouchListener implements View.OnTouchListener {

    private final GestureDetector gestureDetector;

    public OnSwipeTouchListener(Context context) {
        gestureDetector = new GestureDetector(context, new GestureListener());
    }

    public void onSwipeLeft() {
    }

    public void onSwipeRight() {
    }

    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    private final class GestureListener extends GestureDetector.SimpleOnGestureListener {

        private static final int SWIPE_DISTANCE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            float distanceX = e2.getX() - e1.getX();
            float distanceY = e2.getY() - e1.getY();
            if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                if (distanceX > 0)
                    onSwipeRight();
                else
                    onSwipeLeft();
                return true;
            }
            return false;
        }
    }
}

Usage in fragment 片段中的用法

public class ChartsFragment extends Fragment {

    public ChartsFragment(){}

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

        View rootView = inflater.inflate(R.layout.fragment_charts, container, false);
        rootView.setOnTouchListener(new OnSwipeTouchListener(getActivity().getApplicationContext()){
            @Override
            public void onSwipeRight() {
                Log.v("SWIPE", "SWIPE");
            }
        });
        return rootView;
    }
}

But i dont know how to open Drawer, because it's in another class (MainActivity) 但是我不知道如何打开Drawer,因为它在另一个类(MainActivity)中

 mDrawerLayout.openDrawer(linearLayout);

thanks to Pankaj Arora Solution: 感谢Pankaj Arora 解决方案:

final DrawerLayout mDrawerLayout = (DrawerLayout)getActivity().findViewById(R.id.drawer_layout);
        rootView.setOnTouchListener(new OnSwipeTouchListener(getActivity().getApplicationContext()){
            @Override
            public void onSwipeRight() {
                mDrawerLayout.openDrawer(Gravity.START);
                Log.v("SWIPE", "SWIPE");
            }
        });

Try to get the id of drawer layout in your fragment like. 尝试在您的片段中获取抽屉布局的ID。

//global //全球

DrawerLayout mDrawerLayout

in oncreateview 在oncreateview中

mDrawerLayout = getactivity().findviewbyid(R.id.your drawer layout id);

and then in onswipe right call 然后在onswipe正确呼叫

mDrawerLayout.openDrawer(Gravity.Start); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM