WrappingViewPager

jitpack.io Apache License 2.0

ViewPager replacement with dynamic height support and smooth animations for the few edge cases where a standard ViewPager doesn't fulfill your needs.

Sample Sample 2

Install

The library is available on jitpack.io.

Gradle dependency:

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}
dependencies {
    compile 'com.github.iffa:wrapping-viewpager:1.0.1'
}

Quick start

Integrating the library takes less than five minutes: below are the basic steps to get you started.

  1. Use xyz.santeri.wvp.WrappingViewPager instead of android.support.v4.view.ViewPager and set the height to wrap_content in your layout file:

    <xyz.santeri.wvp.WrappingViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
  2. Replace your PagerAdapter with any one of the following adapters with wrapping-functionality built-in:

    • WrappingFragmentPagerAdapter
    • WrappingFragmentStatePagerAdapter

    OR

    • Implement the wrapping in your own adapter implementation by overriding PagerAdapter#setPrimaryItem(ViewGroup, int, Object) like so:
      private int mCurrentPosition = -1; // Keep track of the current position
      
      @Override
      public void setPrimaryItem(ViewGroup container, int position, Object object) {
          super.setPrimaryItem(container, position, object);
      
          if (!(container instanceof WrappingViewPager)) {
              return; // Do nothing if it's not a compatible ViewPager
          }
      
          if (position != mCurrentPosition) { // If the position has changed, tell WrappingViewPager
              Fragment fragment = (Fragment) object;
              WrappingViewPager pager = (WrappingViewPager) container;
              if (fragment != null && fragment.getView() != null) {
                  mCurrentPosition = position;
                  pager.onPageChanged(fragment.getView());
              }
          }
      }
  3. (optional) Set the animation duration and interpolator to your liking with WrappingViewPager#setAnimationDuration(long) (default 100ms) and WrappingViewPager#setAnimationInterpolator(Interpolator).

  4. Enjoy!

For a working demo see the sample module.

License

Copyright (C) 2016 Santeri Elo

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.


WrappingViewPager

Apache许可2.0data-canonical-src

ViewPager替换为动态高度支持和平滑动画,适用于标准ViewPager不符合您需求的少数边缘案例。

示例data-canonical-src 样品2

安装

该库可从 jitpack.io 获取。

Gradle依赖关系

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}
dependencies {
    compile 'com.github.iffa:wrapping-viewpager:1.0.1'
}

快速入门

集成库需要不到五分钟时间:以下是让您启动的基本步骤。

  1. Use xyz.santeri.wvp.WrappingViewPager instead of android.support.v4.view.ViewPager and set the height to wrap_content in your layout file:

    <xyz.santeri.wvp.WrappingViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
  2. Replace your PagerAdapter with any one of the following adapters with wrapping-functionality built-in:

    • WrappingFragmentPagerAdapter
    • WrappingFragmentStatePagerAdapter

    OR

    • Implement the wrapping in your own adapter implementation by overriding PagerAdapter#setPrimaryItem(ViewGroup, int, Object) like so:
      private int mCurrentPosition = -1; // Keep track of the current position

      @Override public void setPrimaryItem(ViewGroup container, int position, Object object) { super.setPrimaryItem(container, position, object);

      <span class="pl-k">if</span> (<span class="pl-k">!</span>(container <span class="pl-k">instanceof</span> <span class="pl-smi">WrappingViewPager</span>)) {
          <span class="pl-k">return</span>; <span class="pl-c"><span class="pl-c">//</span> Do nothing if it&#39;s not a compatible ViewPager</span>
      }
      
      <span class="pl-k">if</span> (position <span class="pl-k">!=</span> mCurrentPosition) { <span class="pl-c"><span class="pl-c">//</span> If the position has changed, tell WrappingViewPager</span>
          <span class="pl-smi">Fragment</span> fragment <span class="pl-k">=</span> (<span class="pl-smi">Fragment</span>) object;
          <span class="pl-smi">WrappingViewPager</span> pager <span class="pl-k">=</span> (<span class="pl-smi">WrappingViewPager</span>) container;
          <span class="pl-k">if</span> (fragment <span class="pl-k">!=</span> <span class="pl-c1">null</span> <span class="pl-k">&amp;&amp;</span> fragment<span class="pl-k">.</span>getView() <span class="pl-k">!=</span> <span class="pl-c1">null</span>) {
              mCurrentPosition <span class="pl-k">=</span> position;
              pager<span class="pl-k">.</span>onPageChanged(fragment<span class="pl-k">.</span>getView());
          }
      }
      

      }

  3. (optional) Set the animation duration and interpolator to your liking with WrappingViewPager#setAnimationDuration(long) (default 100ms) and WrappingViewPager#setAnimationInterpolator(Interpolator).

  4. Enjoy!

有关演示,请参阅示例模块。

许可证

Copyright © 2016 Santeri Elo

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.




相关问题推荐