禁止在 FragmentActivity 中的选项卡之间滑动 [英] disable swiping between tabs in FragmentActivity

查看:26
本文介绍了禁止在 FragmentActivity 中的选项卡之间滑动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 (Sherlock)FragmentActivity 带有 2 个标签片段.左边的片段是一个 GridView,用于显示相册的图片,右边的片段由一个 ViewPager 组成,用于查看单个图片.从左边的片段中,您可以滚动图片并选择一张.用 Tab 键(或滑动)到右边的片段将显示图片,因为它是一个 ViewPager,你可以滑动到上一张或下一张图片.

I have a (Sherlock)FragmentActivity with 2 tabbed fragments. The left fragment is a GridView that displays pictures of an album and the right fragment consists of a ViewPager that is used to view individual pictures. From the left fragment you can scroll through the pictures and select one. Tabbing (or swiping) over to the right fragment will show the picture and because it is a ViewPager you can swipe to the preceding or the next picture.

这很好用,只是 FragmentActivity 想要拦截向右滑动并移回左侧选项卡.当我在正确的选项卡上时,我想防止 FragmentActivity 拦截滑动.如果我必须完全禁用选项卡之间的滑动,那将是令人满意的.我只希望滑动专用于当前选项卡,而不是用于在选项卡之间移动.

This works great except that the FragmentActivity wants to intercept the right swipe and move back to the left tab. I would like to prevent the FragmentActivity from intercepting the swipes when I am on the right tab. If I had to disable swiping between tabs altogether it would be satisfactory. I just want the swiping to be dedicated to the current tab and not be used to move between tabs.

以下图像指示当前行为.右图显示了当我向右滑动时会发生什么.如您所见,左侧选项卡开始出现.我希望滑动仅应用于右侧标签,以便我可以在图像之间滑动而不会出现左侧标签.

The following images indicate the current behavior. The right image shows what happens when I do a swipe to the right. As you can see the left tab starts to appear. I want the swipe to instead apply to the right tab only so that I can swipe between the images without the left tab appearing.

我看到了在 ViewPager 中控制滑动的解决方案,但尚未找到控制选项卡片段之间滑动的解决方案.

I see solutions to control swiping within a ViewPager but have yet to find a solution to control swiping between tabbed fragments.

这是 GridView 片段和 ViewPager 片段的 xml:

Here is the xml for the GridView fragment and the ViewPager fragment:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical">
  <FrameLayout android:id="@android:id/tabcontent"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent">
    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/gridview"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:columnWidth="100dip"
              android:gravity="center"
              android:horizontalSpacing="4dip"
              android:numColumns="auto_fit"
              android:stretchMode="columnWidth"
              android:verticalSpacing="4dip" />
  </FrameLayout>
</LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical">

  <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
                                     android:id="@+id/pager"
                                     android:layout_width="fill_parent"
                                     android:layout_height="0px"
                                     android:layout_weight="1"/>
</LinearLayout>

这里是ViewPager片段的代码摘要:

Here a code summary of the ViewPager fragment:

public class FragmentFlash extends SherlockFragment {

   private GestureDetector gestureDetector;
   View.OnTouchListener gestureListener;
   private ViewPager pager = null;
   private int pagerPosition;

   @Override
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      pagerPosition = 0;
      // Gesture detection
      gestureDetector = new GestureDetector(new MyGestureDetector());
      gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
               return gestureDetector.onTouchEvent(event);
            }
         };
      }

   @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
      View v = inflater.inflate(R.layout.flash, container, false);
      pager = (ViewPager) v.findViewById(R.id.pager);
      pager.setOnTouchListener(gestureListener);
      return v;
   }

   class MyGestureDetector extends SimpleOnGestureListener {
      private static final int SWIPE_MIN_DISTANCE = 10;
      private static final int SWIPE_MAX_OFF_PATH = 250;
      private static final int SWIPE_THRESHOLD_VELOCITY = 50;

      @Override
         public boolean onDown(MotionEvent e) {
         return true;//false; make onFling work with fragments
      }

      @Override
         public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
         try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
               return false;
            else
               // right to left swipe
               if(distanceX > SWIPE_MIN_DISTANCE) {
                  if (pagerPosition < imageUrls.length-1)
                     pager.setCurrentItem(++pagerPosition);
               // left to right swipe
               } else if (distanceX < -SWIPE_MIN_DISTANCE) {
                  if (pagerPosition > 0)
                     pager.setCurrentItem(--pagerPosition);
               }
            return true;
         } catch (Exception e) {
            // nothing
         }
         return false;
      }
   }

   private class ImagePagerAdapter extends PagerAdapter {

      private String[] images;
      private LayoutInflater inflater;

      ImagePagerAdapter(String[] images) {
         this.images = images;
         inflater = mContext.getLayoutInflater();
      }

      @Override
         public void destroyItem(View container, int position, Object object) {
         ((ViewPager) container).removeView((View) object);
      }

      @Override
         public void finishUpdate(View container) {
      }

      @Override
         public int getCount() {
         return images.length;
      }

      @Override
         public Object instantiateItem(View view, int position) {
         final View imageLayout = inflater.inflate(R.layout.item_pager_image, null);
         final ImageView imageView = (ImageView)   imageLayout.findViewById(R.id.image);
         final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);

         byte[] image = ;//get byte array from file at images[position];
         if (null != image) {
            Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
            imageView.setImageBitmap(bitmap);
         }
         ((ViewPager) view).addView(imageLayout, 0);

         return imageLayout;
      }

      @Override
         public boolean isViewFromObject(View view, Object object) {
         return view.equals(object);
      }

      @Override
         public void restoreState(Parcelable state, ClassLoader loader) {
      }

      @Override
         public Parcelable saveState() {
         return null;
      }

      @Override
         public void startUpdate(View container) {
      }
   }

   public void pagerPositionSet(int pagerPosition, String[] imageUrls) {
      Log.i(Flashum.LOG_TAG, "FragmentFlash pagerPositionSet: " + pagerPosition);
      if (pagerPosition >= 0)
         this.pagerPosition = pagerPosition;
      if (pager != null) {
         pager.setAdapter(new ImagePagerAdapter(imageUrls));
         pager.setCurrentItem(this.pagerPosition);
      }
   }

}

这是 item_pager_image.xml:

This is the item_pager_image.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="1dip" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:contentDescription="@string/descr_image" />

    <ProgressBar
        android:id="@+id/loading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="gone" />

</FrameLayout>

推荐答案

好的,我终于想通了.Laurence Dawson 走在正确的轨道上,但不是将 CustomViewPager 应用于片段,而是需要应用于 FragmentActivity.你看,由活动管理的选项卡之间的切换也是一个 ViewPager 适配器.因此,活动的 xml 是

Ok, I finally figured this out. Laurence Dawson was on the right track but instead of applying the CustomViewPager to the fragment it needs to be applied to the FragmentActivity. You see, switching between tabs, which is managed by the activity, is also a ViewPager adaptor. Thus, xml for the activity is

<TabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TabWidget
            android:id="@android:id/tabs"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"/>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"/>

        <com.Flashum.CustomViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

    </LinearLayout>
</TabHost>

并且自定义 ViewPager 与建议相同,只是构造函数使其能够导致 onInterceptTouchEvent 返回 false.这可以防止 FragmentActivity 作用于滑动,以便它可以专用于片段!

And the custom ViewPager is as suggested except that that the constructor enables it to cause onInterceptTouchEvent to return false. This prevents the FragmentActivity from acting on the swipe so that it can be dedicated to the fragment!

public class CustomViewPager extends ViewPager {

    private boolean enabled;

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.enabled = **false**;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.enabled) {
            return super.onTouchEvent(event);
        }

        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (this.enabled) {
            return super.onInterceptTouchEvent(event);
        }

        return false;
    }

    public void setPagingEnabled(boolean enabled) {
        this.enabled = enabled;
    }
}

这篇关于禁止在 FragmentActivity 中的选项卡之间滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆