<bdo id='lz3tO'></bdo><ul id='lz3tO'></ul>
<i id='lz3tO'><tr id='lz3tO'><dt id='lz3tO'><q id='lz3tO'><span id='lz3tO'><b id='lz3tO'><form id='lz3tO'><ins id='lz3tO'></ins><ul id='lz3tO'></ul><sub id='lz3tO'></sub></form><legend id='lz3tO'></legend><bdo id='lz3tO'><pre id='lz3tO'><center id='lz3tO'></center></pre></bdo></b><th id='lz3tO'></th></span></q></dt></tr></i><div id='lz3tO'><tfoot id='lz3tO'></tfoot><dl id='lz3tO'><fieldset id='lz3tO'></fieldset></dl></div>
    <tfoot id='lz3tO'></tfoot>

    <small id='lz3tO'></small><noframes id='lz3tO'>

  • <legend id='lz3tO'><style id='lz3tO'><dir id='lz3tO'><q id='lz3tO'></q></dir></style></legend>

      1. 如何通过在 ViewPager 中用手指滑动来禁用分页但仍然能够以编程方式滑动?

        时间:2023-10-04
        • <legend id='uWO8y'><style id='uWO8y'><dir id='uWO8y'><q id='uWO8y'></q></dir></style></legend>
            <i id='uWO8y'><tr id='uWO8y'><dt id='uWO8y'><q id='uWO8y'><span id='uWO8y'><b id='uWO8y'><form id='uWO8y'><ins id='uWO8y'></ins><ul id='uWO8y'></ul><sub id='uWO8y'></sub></form><legend id='uWO8y'></legend><bdo id='uWO8y'><pre id='uWO8y'><center id='uWO8y'></center></pre></bdo></b><th id='uWO8y'></th></span></q></dt></tr></i><div id='uWO8y'><tfoot id='uWO8y'></tfoot><dl id='uWO8y'><fieldset id='uWO8y'></fieldset></dl></div>
          1. <tfoot id='uWO8y'></tfoot>
              <tbody id='uWO8y'></tbody>

            <small id='uWO8y'></small><noframes id='uWO8y'>

              • <bdo id='uWO8y'></bdo><ul id='uWO8y'></ul>

                  本文介绍了如何通过在 ViewPager 中用手指滑动来禁用分页但仍然能够以编程方式滑动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有 ViewPager,在它下面有 10 个按钮.通过单击按钮,例如#4,寻呼机立即通过mPager.setCurrentItem(3); 转到页面#4.但是,我想通过水平滑动手指来禁用分页.因此,分页是通过点击按钮完成的.那么,如何禁用手指滑动呢?

                  I have ViewPager and below it I have 10 buttons. By clicking on button, for example #4, the pager goes immediately to page #4 by mPager.setCurrentItem(3);. But, I want to disable the paging by swiping with finger horizontally. Thus, the paging is done ONLY by clicking on the buttons. So, how I can disable the swiping with finger?

                  推荐答案

                  你需要继承 ViewPager.onTouchEvent 中有很多你不想改变的好东西,比如允许子视图被触摸.onInterceptTouchEvent 是您要更改的内容.如果您查看 ViewPager 的代码,您会看到以下注释:

                  You need to subclass ViewPager. onTouchEvent has a lot of good things in it that you don't want to change like allowing child views to get touches. onInterceptTouchEvent is what you want to change. If you look at the code for ViewPager, you'll see the comment:

                      /*
                       * This method JUST determines whether we want to intercept the motion.
                       * If we return true, onMotionEvent will be called and we do the actual
                       * scrolling there.
                       */
                  

                  这是一个完整的解决方案:

                  Here's a complete solution:

                  首先,将这个类添加到您的 src 文件夹中:

                  First, add this class to your src folder:

                  import android.content.Context;
                  import android.support.v4.view.ViewPager;
                  import android.util.AttributeSet;
                  import android.view.MotionEvent;
                  import android.view.animation.DecelerateInterpolator;
                  import android.widget.Scroller;
                  import java.lang.reflect.Field;
                  
                  public class NonSwipeableViewPager extends ViewPager {
                  
                      public NonSwipeableViewPager(Context context) {
                          super(context);
                          setMyScroller();
                      }
                  
                      public NonSwipeableViewPager(Context context, AttributeSet attrs) {
                          super(context, attrs);
                          setMyScroller();
                      }
                  
                      @Override
                      public boolean onInterceptTouchEvent(MotionEvent event) {
                          // Never allow swiping to switch between pages
                          return false;
                      }
                  
                      @Override
                      public boolean onTouchEvent(MotionEvent event) {
                          // Never allow swiping to switch between pages
                          return false;
                      }
                  
                      //down one is added for smooth scrolling
                  
                      private void setMyScroller() {
                          try {
                              Class<?> viewpager = ViewPager.class;
                              Field scroller = viewpager.getDeclaredField("mScroller");
                              scroller.setAccessible(true);
                              scroller.set(this, new MyScroller(getContext()));
                          } catch (Exception e) {
                              e.printStackTrace();
                          }
                      }
                  
                      public class MyScroller extends Scroller {
                          public MyScroller(Context context) {
                              super(context, new DecelerateInterpolator());
                          }
                  
                          @Override
                          public void startScroll(int startX, int startY, int dx, int dy, int duration) {
                              super.startScroll(startX, startY, dx, dy, 350 /*1 secs*/);
                          }
                      }
                  }
                  

                  接下来,确保使用此类而不是常规的 ViewPager,您可能将其指定为 android.support.v4.view.ViewPager.在您的布局文件中,您需要使用以下内容指定它:

                  Next, make sure to use this class instead of the regular ViewPager, which you probably specified as android.support.v4.view.ViewPager. In your layout file, you'll want to specify it with something like:

                  <com.yourcompany.NonSwipeableViewPager
                      android:id="@+id/view_pager"
                      android:layout_width="match_parent"
                      android:layout_height="0dp"
                      android:layout_weight="1" />
                  

                  这个特殊的例子是在一个 LinearLayout 并且是为了占据整个屏幕,这就是为什么 layout_weight 是 1 而 layout_height 是0dp.

                  This particular example is in a LinearLayout and is meant to take up the entire screen, which is why layout_weight is 1 and layout_height is 0dp.

                  setMyScroller();方法是为了平滑过渡

                  And setMyScroller(); method is for smooth transition

                  这篇关于如何通过在 ViewPager 中用手指滑动来禁用分页但仍然能够以编程方式滑动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:从 Bool 数组返回索引值数组,其中 true 下一篇:如何确定 Fragment 何时在 ViewPager 中可见

                  相关文章

                  1. <i id='I89Zu'><tr id='I89Zu'><dt id='I89Zu'><q id='I89Zu'><span id='I89Zu'><b id='I89Zu'><form id='I89Zu'><ins id='I89Zu'></ins><ul id='I89Zu'></ul><sub id='I89Zu'></sub></form><legend id='I89Zu'></legend><bdo id='I89Zu'><pre id='I89Zu'><center id='I89Zu'></center></pre></bdo></b><th id='I89Zu'></th></span></q></dt></tr></i><div id='I89Zu'><tfoot id='I89Zu'></tfoot><dl id='I89Zu'><fieldset id='I89Zu'></fieldset></dl></div>

                    <small id='I89Zu'></small><noframes id='I89Zu'>

                    <legend id='I89Zu'><style id='I89Zu'><dir id='I89Zu'><q id='I89Zu'></q></dir></style></legend>
                    • <bdo id='I89Zu'></bdo><ul id='I89Zu'></ul>

                    1. <tfoot id='I89Zu'></tfoot>