onSwipe Event is not Triggered inside a ViewPager
I have a ViewPager, the first page(fragment) of it has a DrawerLayout that
implements a SimpleOnGestureListener that listens to onSwipe event. The
purpose of the onSwipe event inside the DrawerLayout(fragment) is to
listen if the user swipes to left and right which triggers the opening and
closing of the drawer.
The problem is by the time, I swipe to left or right the ViewPager's page
changes instead of triggering the opening and closing of the Drawer.
Is there a way to disable temporarily the swiping of the ViewPager, while
its current view is on DrawerLayout so that it can listen to the left and
right event?
If the Drawer is opened and If I swipe the screen to the right then that's
the time the page changes. I've been struggling with this for a couple of
days now.
Drawer
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);
detector = new SimpleGestureFilter(getActivity(),this);
mTitle = mDrawerTitle = getActivity().getTitle();
ActionBar actionBar = getActivity().getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
@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);
}
public static class PlanetFragment extends SherlockFragment {
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;
}
}
//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");
}
}
No comments:
Post a Comment