android卡片viewpage,ViewPagerCards 一分钟实现ViewPager卡片

ViewPagerCards

欢迎关注微信公众号、长期为您推荐优秀博文、开源项目、视频

微信公众号名称:Android干货程序员

687474703a2f2f75706c6f61642d696d616765732e6a69616e7368752e696f2f75706c6f61645f696d616765732f343033373130352d386637333762353130346464306235642e706e673f696d6167654d6f6772322f6175746f2d6f7269656e742f7374726970253743696d61676556696577322f322f772f31323430

4c6f764c6b64180215640430c7e86488.gif68747470733a2f2f727562656e736f7573612e6769746875622e696f2f696d672f7669657770616765725f726573756c742e676966

ViewPagerCardView.gif

使用步骤:

第一步:初始化ViewPager:

android:id="@+id/viewPager"

android:layout_width="match_parent"

android:layout_height="330dp"

android:layout_gravity="bottom"

android:clipToPadding="false"

android:overScrollMode="never"

android:paddingBottom="30dp"

android:paddingEnd="@dimen/card_padding"

android:paddingLeft="@dimen/card_padding"

android:paddingRight="@dimen/card_padding"

android:paddingStart="@dimen/card_padding" />

第二步:设置setPageTransformer:

public class ShadowTransformer implements ViewPager.OnPageChangeListener, ViewPager.PageTransformer {

private ViewPager mViewPager;

private CardAdapter mAdapter;

private float mLastOffset;

private boolean mScalingEnabled;

public ShadowTransformer(ViewPager viewPager, CardAdapter adapter) {

mViewPager = viewPager;

viewPager.addOnPageChangeListener(this);

mAdapter = adapter;

}

public void enableScaling(boolean enable) {

if (mScalingEnabled && !enable) {

// shrink main card

CardView currentCard = mAdapter.getCardViewAt(mViewPager.getCurrentItem());

if (currentCard != null) {

currentCard.animate().scaleY(1);

currentCard.animate().scaleX(1);

}

}else if(!mScalingEnabled && enable){

// grow main card

CardView currentCard = mAdapter.getCardViewAt(mViewPager.getCurrentItem());

if (currentCard != null) {

currentCard.animate().scaleY(1.1f);

currentCard.animate().scaleX(1.1f);

}

}

mScalingEnabled = enable;

}

@Override

public void transformPage(View page, float position) {

}

@Override

public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

int realCurrentPosition;

int nextPosition;

float baseElevation = mAdapter.getBaseElevation();

float realOffset;

boolean goingLeft = mLastOffset > positionOffset;

// If we're going backwards, onPageScrolled receives the last position

// instead of the current one

if (goingLeft) {

realCurrentPosition = position + 1;

nextPosition = position;

realOffset = 1 - positionOffset;

} else {

nextPosition = position + 1;

realCurrentPosition = position;

realOffset = positionOffset;

}

// Avoid crash on overscroll

if (nextPosition > mAdapter.getCount() - 1

|| realCurrentPosition > mAdapter.getCount() - 1) {

return;

}

CardView currentCard = mAdapter.getCardViewAt(realCurrentPosition);

// This might be null if a fragment is being used

// and the views weren't created yet

if (currentCard != null) {

if (mScalingEnabled) {

currentCard.setScaleX((float) (1 + 0.1 * (1 - realOffset)));

currentCard.setScaleY((float) (1 + 0.1 * (1 - realOffset)));

}

currentCard.setCardElevation((baseElevation + baseElevation

* (CardAdapter.MAX_ELEVATION_FACTOR - 1) * (1 - realOffset)));

}

CardView nextCard = mAdapter.getCardViewAt(nextPosition);

// We might be scrolling fast enough so that the next (or previous) card

// was already destroyed or a fragment might not have been created yet

if (nextCard != null) {

if (mScalingEnabled) {

nextCard.setScaleX((float) (1 + 0.1 * (realOffset)));

nextCard.setScaleY((float) (1 + 0.1 * (realOffset)));

}

nextCard.setCardElevation((baseElevation + baseElevation

* (CardAdapter.MAX_ELEVATION_FACTOR - 1) * (realOffset)));

}

mLastOffset = positionOffset;

}

@Override

public void onPageSelected(int position) {

}

@Override

public void onPageScrollStateChanged(int state) {

}

}

第三步:设置setAdapter:

public class CardFragmentPagerAdapter extends FragmentStatePagerAdapter implements CardAdapter {

private List mFragments;

private float mBaseElevation;

public CardFragmentPagerAdapter(FragmentManager fm, float baseElevation) {

super(fm);

mFragments = new ArrayList<>();

mBaseElevation = baseElevation;

for(int i = 0; i< 5; i++){

addCardFragment(new CardFragment());

}

}

@Override

public float getBaseElevation() {

return mBaseElevation;

}

@Override

public CardView getCardViewAt(int position) {

return mFragments.get(position).getCardView();

}

@Override

public int getCount() {

return mFragments.size();

}

@Override

public Fragment getItem(int position) {

return mFragments.get(position);

}

@Override

public Object instantiateItem(ViewGroup container, int position) {

Object fragment = super.instantiateItem(container, position);

mFragments.set(position, (CardFragment) fragment);

return fragment;

}

public void addCardFragment(CardFragment fragment) {

mFragments.add(fragment);

}

}

第四步:设置CardAdapter:

public interface CardAdapter {

int MAX_ELEVATION_FACTOR = 8;

float getBaseElevation();

CardView getCardViewAt(int position);

int getCount();

}

第五步:实现:

mViewPager.setAdapter(mFragmentCardAdapter);

mViewPager.setPageTransformer(false, mFragmentCardShadowTransformer);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值