以下の更新を参照してください。
私は を持っていViewPager
ます。その最初のページ ( fragment
) には、イベントをリッスンするDrawerLayout
を実装するがあります。(フラグメント)内のイベントの目的は、ユーザーがスワイプしてとの開閉をトリガーするかどうかをリッスンすることです。SimpleOnGestureListener
onSwipe
onSwipe
DrawerLayout
left
right
drawer
問題は、その時までにleft or right
、.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(fragment) を持つ親 ViewPager を持っています。画面の左側を 1 秒間保持してから右にドラッグすると、引き出しが機能します。上の画像に示すように。
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" />