请参阅下面的更新
我有一个ViewPager
,它的第一页(fragment
)有一个DrawerLayout
实现一个SimpleOnGestureListener
监听onSwipe
事件的。onSwipe
(fragment)内的事件的目的DrawerLayout
是监听用户是否滑动到left
并right
触发drawer
.
问题是到那时,我滑动到left or right
ViewPager的页面更改,而不是触发Drawer
.
有没有办法在当前视图打开时暂时禁用的滑动,以便它可以监听左右事件?ViewPager
DrawerLayout
如果抽屉被打开并且如果我向右滑动屏幕,那么这就是页面更改的时间。我已经为此苦苦挣扎了几天。
我这可能在里面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" />