在片段活动上实现Edge Scroll / Swipe

时间:2014-04-16 13:12:44

标签: android android-fragments scroll swipe motionevent

我正在尝试实施类似于我们在Google地图上看到的滚动/滑动机制。即屏幕上的相对中心运动会影响地图,而从屏幕外部开始从左侧滑动会打开侧面菜单。

我的主要活动扩展了FragmentActivity,并且有一个简单的侧边菜单(菜单),目前可通过按钮( btnSlide )访问。我希望能够通过边缘滚动(如谷歌地图)打开侧面菜单,但任何滑动或动作都会影响显示哪个片段并更改片段。

我已尝试使用OnTouchListener并实施GestureDetector.OnGestureListener,但我无法让任何听众接受动议。所有动作都会影响碎片。

下面的代码示例。 CustomHorizo​​ntalScreenView 让我有一个简单的侧边菜单,它只是一个自定义 Horizo​​ntalScrollView

任何帮助将不胜感激!

public class MainActivity extends FragmentActivity implements GestureDetector.OnGestureListener{
    private CustomHorizontalScreenView scrollView;
    private View app;
    private View menu;
    private Button btnSlide;
    private boolean POST_LOGS = false;
    private CameraCollectionPageAdapter mCameraCollectionPageAdapter;
    private ViewPager mViewPager;
    private GestureDetectorCompat mDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = LayoutInflater.from(this);
        scrollView = (CustomHorizontalScreenView) inflater.inflate(R.layout.custom_horizontal_screen_view, null);
        setContentView(scrollView);

        menu = inflater.inflate(R.layout.side_menu, null);
        app = inflater.inflate(R.layout.activity_main, null);

        mDetector = new GestureDetectorCompat(this.getApplicationContext(), this);

        btnSlide = (Button) app.findViewById(R.id.buttonSettings);
        btnSlide.setOnClickListener(new ClickListenerForScrolling(scrollView, menu));

        final View[] children = new View[] { menu, app };

        // Scroll to app (view[1]) when layout finished.
        int scrollToViewIdx = 1;
        scrollView.initViews(children, scrollToViewIdx, new SizeCallbackForMenu(btnSlide));

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        startUp(prefs);
    }

    private void startUp(SharedPreferences prefs){
        //Setting Dimensions
        Display display = getWindowManager().getDefaultDisplay(); 
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt(MeanwhileConstants.SCREEN_WIDTH, display.getWidth());
        editor.putInt(MeanwhileConstants.SCREEN_HEIGHT, display.getHeight());
        editor.commit();
    }

    /********** Control ****************/
    @Override
    protected void onResume() {
        super.onResume();
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        setupUI(prefs);
    }

    @Override
    protected void onPause(){
        super.onPause();
    }

    /*********** UI Stuff ***********/
    @SuppressLint("NewApi")
    private void setupUI(SharedPreferences prefs){
        String currentCamera = prefs.getString(MeanwhileConstants.CURRENT_CAMERA_ID, null);
        LinkedList<Camera> cameras = getPrioritizedCameras(prefs, currentCamera);
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setOffscreenPageLimit(2);
        mCameraCollectionPageAdapter = new CameraCollectionPageAdapter(getSupportFragmentManager(), mViewPager, this.getApplicationContext(), cameras);
        mCameraCollectionPageAdapter.notifyDataSetChanged();
        mViewPager.setAdapter(mCameraCollectionPageAdapter);

    }
}

0 个答案:

没有答案