<tfoot id='0fFdz'></tfoot>
    • <bdo id='0fFdz'></bdo><ul id='0fFdz'></ul>

    <legend id='0fFdz'><style id='0fFdz'><dir id='0fFdz'><q id='0fFdz'></q></dir></style></legend>
  • <small id='0fFdz'></small><noframes id='0fFdz'>

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

      1. Android:垂直 ViewPager

        时间:2023-10-04

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

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

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

                <legend id='PEiuV'><style id='PEiuV'><dir id='PEiuV'><q id='PEiuV'></q></dir></style></legend>

                1. 本文介绍了Android:垂直 ViewPager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有没有办法制作一个不水平滚动但垂直滚动的ViewPager?!

                  Is there a way to make a ViewPager that does not scroll horizontally, but vertically?!

                  推荐答案

                  你可以使用 ViewPager.PageTransformer 以提供垂直 ViewPager 的错觉.要实现垂直滚动而不是水平拖动,您必须覆盖 ViewPager 的默认触摸事件并在处理它们之前交换 MotionEvent 的坐标,例如:

                  You can use a ViewPager.PageTransformer to give the illusion of a vertical ViewPager. To achieve scrolling with a vertical instead of a horizontal drag you will have to override ViewPager's default touch events and swap the coordinates of MotionEvents prior to handling them, e.g.:

                  /**
                   * Uses a combination of a PageTransformer and swapping X & Y coordinates
                   * of touch events to create the illusion of a vertically scrolling ViewPager. 
                   * 
                   * Requires API 11+
                   * 
                   */
                  public class VerticalViewPager extends ViewPager {
                  
                      public VerticalViewPager(Context context) {
                          super(context);
                          init();
                      }
                  
                      public VerticalViewPager(Context context, AttributeSet attrs) {
                          super(context, attrs);
                          init();
                      }
                  
                      private void init() {
                          // The majority of the magic happens here
                          setPageTransformer(true, new VerticalPageTransformer());
                          // The easiest way to get rid of the overscroll drawing that happens on the left and right
                          setOverScrollMode(OVER_SCROLL_NEVER);
                      }
                  
                      private class VerticalPageTransformer implements ViewPager.PageTransformer {
                  
                          @Override
                          public void transformPage(View view, float position) {
                  
                              if (position < -1) { // [-Infinity,-1)
                                  // This page is way off-screen to the left.
                                  view.setAlpha(0);
                  
                              } else if (position <= 1) { // [-1,1]
                                  view.setAlpha(1);
                  
                                  // Counteract the default slide transition
                                  view.setTranslationX(view.getWidth() * -position);
                  
                                  //set Y position to swipe in from top
                                  float yPosition = position * view.getHeight();
                                  view.setTranslationY(yPosition);
                  
                              } else { // (1,+Infinity]
                                  // This page is way off-screen to the right.
                                  view.setAlpha(0);
                              }
                          }
                      }
                  
                      /**
                       * Swaps the X and Y coordinates of your touch event.
                       */
                      private MotionEvent swapXY(MotionEvent ev) {
                          float width = getWidth();
                          float height = getHeight();
                  
                          float newX = (ev.getY() / height) * width;
                          float newY = (ev.getX() / width) * height;
                  
                          ev.setLocation(newX, newY);
                  
                          return ev;
                      }
                  
                      @Override
                      public boolean onInterceptTouchEvent(MotionEvent ev){
                          boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
                          swapXY(ev); // return touch coordinates to original reference frame for any child views
                          return intercepted;
                      }
                  
                      @Override
                      public boolean onTouchEvent(MotionEvent ev) {
                          return super.onTouchEvent(swapXY(ev));
                      }
                  
                  }
                  

                  当然,您可以根据需要调整这些设置.最终看起来像这样:

                  Of course you can tweak these settings as you see fit. Ends up looking like this:

                  这篇关于Android:垂直 ViewPager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:ViewPager 和 Fragment — 存储 Fragment 状态的正确方法是什么? 下一篇:收到错误“Java.lang.IllegalStateException Activity 已被破坏";在 V

                  相关文章

                    <bdo id='r5jpF'></bdo><ul id='r5jpF'></ul>

                    1. <legend id='r5jpF'><style id='r5jpF'><dir id='r5jpF'><q id='r5jpF'></q></dir></style></legend>

                      <tfoot id='r5jpF'></tfoot>

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

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