0

请参阅下面的更新 我有一个ViewPager,它的第一页(fragment)有一个DrawerLayout实现一个SimpleOnGestureListener监听onSwipe事件的。onSwipe(fragment)内的事件的目的DrawerLayout是监听用户是否滑动到leftright触发drawer.在此处输入图像描述

问题是到那时,我滑动到left or rightViewPager的页面更改,而不是触发Drawer.

有没有办法在当前视图打开时暂时禁用的滑动,以便它可以监听左右事件?ViewPagerDrawerLayout

如果抽屉被打开并且如果我向右滑动屏幕,那么这就是页面更改的时间。我已经为此苦苦挣扎了几天。

我这可能在里面DrawerLayoutFragment吗?

if(MainClass_ViewPager.getView == getActivity().getView)
MainClass_ViewPager.isEnabled(false)

onSwipe of `DrawerLayoutFragment`  is triggered

抽屉

public class DrawerLayoutFragment extends Fragment implements SimpleGestureListener{

   private SimpleGestureFilter detector; 

    private DrawerLayout mDrawerLayout;

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void onCreate (Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);


    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return rootView;
    }

     public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getActivity().getMenuInflater();
           inflater.inflate(R.menu.main_menu, menu);
            return getActivity().onCreateOptionsMenu(menu);
        }

     @Override
        public boolean onOptionsItemSelected(MenuItem item) {

             // The action bar home/u p action should open or close the drawer.
             // ActionBarDrawerToggle will take care of this.
            if (mDrawerToggle.onOptionsItemSelected(item)) {

                return true;
            }
            // Handle action buttons
            switch(item.getItemId()) {
            default:
                Log.d("onOptionsItemSelected", "inside");
                return super.onOptionsItemSelected(item);
            }
        }


     protected void onPostCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Sync the toggle state after onRestoreInstanceState has occurred.
            mDrawerToggle.syncState();
        }

     @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            // Pass any configuration change to the drawer toggls
            mDrawerToggle.onConfigurationChanged(newConfig);
        }



    /* The click listner for ListView in the navigation drawer */
    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);

            Log.d("DrawerItemClickListener", "inside");
        }
    }

    private void selectItem(int position) {

   }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActivity().getActionBar().setTitle(mTitle);
    }


    //Simple Gesture
    public boolean dispatchTouchEvent(MotionEvent me){ 
      this.detector.onTouchEvent(me);
     return getActivity().dispatchTouchEvent(me); 
    }

    @Override
    public void onSwipe(int direction) {
        Log.d("onSwipe", "onSwipe");

    }

    @Override
    public void onDoubleTap() {
        // TODO Auto-generated method stub
        Log.d("onDoubleTap", "onDoubleTap");

    }

}

更新- 这是我到目前为止得到的。我有一个父 ViewPager,它有一个子 ViewPager(fragment),我无法处理子 ViewPager的刷卡,因为父 ViewPager 总是被刷,没有给孩子任何机会。抽屉正在工作,同时我按住屏幕左侧一秒钟然后将其拖动到右侧。如上图所示。

private void selectItem(int position) {
     Log.d("selectItem", "inside");
       // update the main content by replacing fragments
       ViewPagerConnect fragment = new ViewPagerConnect();
       Bundle args = new Bundle();
       args.putInt("position", position);
       fragment.setArguments(args);

       FragmentTransaction ft = getChildFragmentManager().beginTransaction();
                 ft.add(R.id.content_frame, fragment);
         ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
         ft.commit();

       // update selected item and title, then close the drawer
       mDrawerList.setItemChecked(position, true);
       setTitle(mPlanetTitles[position]);
       mDrawerLayout.closeDrawer(mDrawerList);
   }

---------------------------------------


public class ViewPagerConnect extends SherlockFragment{

    AppSectionsPagerAdapter mAppSectionsPagerAdapter;
     ViewPager mViewPager;
     static int position;

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

        mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getChildFragmentManager());

        position = getArguments().getInt("position");

         View rootView = inflater.inflate(R.layout.viewpager_connect, container, false);
        mViewPager = (ViewPager) rootView.findViewById(R.id.pager);
        mViewPager.setAdapter(mAppSectionsPagerAdapter);


                return rootView;

    }

    public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {

        public AppSectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }


        @Override
        public int getCount() {
            return 1;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return "Section: " + (position + 1);
        }


        @Override
        public Fragment getItem(int arg0) {
             switch(arg0){
             case 0:
                 PlanetFragment fragment = new PlanetFragment();
                 Bundle args = new Bundle();
                 args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
                 fragment.setArguments(args);
                 return fragment;

             }
            return null;

        }
    }

}
----------------------------------------------
public class PlanetFragment extends SherlockFragment {

     ViewPager mViewPager;
    public static final String ARG_PLANET_NUMBER = "planet_number";

    public PlanetFragment() {
        // Empty constructor required for fragment subclasses
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
        int i = getArguments().getInt(ARG_PLANET_NUMBER);
        String planet = getResources().getStringArray(R.array.planets_array)[i];

        int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
                        "drawable", getActivity().getPackageName());
        ((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
        getActivity().setTitle(planet);
        return rootView;
    }
}
---------------------------
viewpager_connect.xml

<?xml version="1.0" encoding="utf-8"?>
  <android.support.v4.view.ViewPager
      xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

-----------------------------------------------
fragment_planet.xml

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:gravity="center"
    android:padding="32dp" />
4

1 回答 1

1

在此处获取演示 NavDrawer

如果您将 ViewPager 实现换成示例代码中给出的 Planet Selection 框架,它将正常工作。

细节:

在“selectItem”中,只需在“fragment”对象中获取一个 ViewPager,它是 FragmentManager 事务的目标。这样就可以了,您将拥有 NavDrawer 和 ViewPager。不要禁用任何东西,因为它会正常工作。

请参阅下面的“planetfragment” - 只需交换您的 ViewPager 类,它就会全部工作。移除行星碎片。在其位置添加您的 ViewPager。这就是你所要做的。您的“viewpager”应该是一个片段,它获取一个 ViewPager 并将其连接到一个适配器。它实现了与当前示例中的行星相同的接口。把它换进去。

private void selectItem(int position) {
    // update the main content by replacing fragments
    Fragment fragment = new PlanetFragment();
    Bundle args = new Bundle();
    args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
    fragment.setArguments(args);

    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mPlanetTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}
于 2013-08-10T04:43:58.663 回答