• <tfoot id='p5XN5'></tfoot>

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

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

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

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

        Android viewpager检测滑动超出范围

        时间:2023-10-03
          • <bdo id='amhpz'></bdo><ul id='amhpz'></ul>

          • <tfoot id='amhpz'></tfoot>

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

            <legend id='amhpz'><style id='amhpz'><dir id='amhpz'><q id='amhpz'></q></dir></style></legend>
                <tbody id='amhpz'></tbody>

              • <i id='amhpz'><tr id='amhpz'><dt id='amhpz'><q id='amhpz'><span id='amhpz'><b id='amhpz'><form id='amhpz'><ins id='amhpz'></ins><ul id='amhpz'></ul><sub id='amhpz'></sub></form><legend id='amhpz'></legend><bdo id='amhpz'><pre id='amhpz'><center id='amhpz'></center></pre></bdo></b><th id='amhpz'></th></span></q></dt></tr></i><div id='amhpz'><tfoot id='amhpz'></tfoot><dl id='amhpz'><fieldset id='amhpz'></fieldset></dl></div>
                1. 本文介绍了Android viewpager检测滑动超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在我的 Android 应用程序中,我使用 viewpager 进行图像滑动.我的要求是,如果用户滑出第一页和最后一页,则活动应该完成.

                  In my Android application, I am using a viewpager for image swiping. My requirement is, if a user swipes out of the first and last page, the activity should finish.

                  我采用了这个示例.但是方法 setOnSwipeOutListener 在我的活动中没有被调用.

                  I have taken this example. But the method setOnSwipeOutListener is not called in my activity.

                  这是我的自定义视图寻呼机类:

                  This is my custom view pager class:

                  public class CustomViewPager extends ViewPager {
                  
                      public CustomViewPager(Context context) {
                          super(context);
                      }
                  
                      public CustomViewPager(Context context, AttributeSet attrs) {
                          super(context, attrs);
                      }
                  
                      float mStartDragX;
                      OnSwipeOutListener mListener;
                  
                      public void setOnSwipeOutListener(OnSwipeOutListener listener) {
                          mListener = listener;
                      }
                  
                      @Override
                      public boolean onInterceptTouchEvent(MotionEvent ev) {
                          float x = ev.getX();
                          switch (ev.getAction()) {
                          case MotionEvent.ACTION_DOWN:
                              mStartDragX = x;
                              break;
                          case MotionEvent.ACTION_MOVE:
                              if (mStartDragX < x && getCurrentItem() == 0) {
                                  mListener.onSwipeOutAtStart();
                              } else if (mStartDragX > x
                                      && getCurrentItem() == getAdapter().getCount() - 1) {
                                  mListener.onSwipeOutAtEnd();
                              }
                              break;
                          }
                          return super.onInterceptTouchEvent(ev);
                      }
                  
                      public interface OnSwipeOutListener {
                          public void onSwipeOutAtStart();
                  
                          public void onSwipeOutAtEnd();
                      }
                  
                  } 
                  

                  在我的活动类下面,我在自定义 viewpager 类上调用 setOnSwipeOutListener 方法,但它没有被调用.

                  And below in my activity class I call setOnSwipeOutListener method on my custom viewpager class, but it does not get called.

                  myPager = (CustomViewPager) findViewById(R.id.home_pannels_pager);
                  
                  .......
                  
                  myPager.setOnSwipeOutListener(new OnSwipeOutListener() { // the method never called
                      @Override
                      public void onSwipeOutAtStart() {
                          Log.e("swipe Out At Start ", "swipe out");
                      }
                  
                      @Override
                      public void onSwipeOutAtEnd() {
                          Log.e("swipe Out At End ", "swipe end");
                      }
                  });
                  
                  myPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                      @Override
                      public void onPageSelected(int po) {
                          Log.e("positon", ""+po);
                      }
                  });
                  

                  请帮助我检测用户是否滑动到第一页和最后一页以关闭此活动以及如何从代码中调用该方法.

                  Please help me to detect if the user has swiped beyond the first and last page to close this activity and how to call the method from code.

                  推荐答案

                  就我而言,onInterceptTouchEvent 不起作用.我用 onTouchEvent 改变了方法,它正在工作.原始解决方案来自 tjlian616

                  In my case, onInterceptTouchEvent does not work. I change the method with onTouchEvent and it is working. The original solution is from tjlian616

                      @Override
                  public boolean onTouchEvent(MotionEvent ev){
                      if(getCurrentItem()==getAdapter().getCount()-1){
                          final int action = ev.getAction();
                          float x = ev.getX();
                          switch(action & MotionEventCompat.ACTION_MASK){
                          case MotionEvent.ACTION_DOWN:
                              mStartDragX = x;
                              break;
                          case MotionEvent.ACTION_MOVE:
                              break;
                          case MotionEvent.ACTION_UP:
                              if (x<mStartDragX){
                                  mListener.onSwipeOutAtEnd();
                              }else{
                                  mStartDragX = 0;
                              }
                              break;
                          }
                      }else{
                          mStartDragX=0;
                      }
                      return super.onTouchEvent(ev);
                  }    
                  

                  这篇关于Android viewpager检测滑动超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将视图渲染到 View Pager - 优化方式 下一篇:想要在 viewpager 上播放声音 滑动

                  相关文章

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

                    <legend id='6T9iK'><style id='6T9iK'><dir id='6T9iK'><q id='6T9iK'></q></dir></style></legend>

                    <small id='6T9iK'></small><noframes id='6T9iK'>

                    • <bdo id='6T9iK'></bdo><ul id='6T9iK'></ul>

                    1. <tfoot id='6T9iK'></tfoot>