一分钟实现ViewPager卡片ViewPagerCards
2017-06-07 本文已影响2550人
小怪兽打葫芦娃
自定义控件
- 一分钟实现贴纸功能
- 一分钟实现TextView高亮
- 一分钟实现新手引导页
- 一分钟实现ViewPager卡片
- 一分钟实现轮播图
- 一分钟实现GridView拖拽
- 一分钟实现底部导航栏
- 一分钟实现底部FragmentTabhost
- 一分钟实现多张图片选择
- 一分钟实现仿美拍直播的点赞动画
- 一分钟实现高仿今日头条视频列表
- 一分钟实现购物车加减控件
- 一分钟实现省市县三级联动
- 一分钟实现二维码生成和扫描
- 一分钟实现沉浸式状态栏
- 一分钟实现图片裁剪
- 一分钟实现视频弹幕
- 一分钟实现图片缩放
- 一分钟实现旋转选择器
- 一分钟实现ofo小黄车的引导界面
- 一分钟实现自定义ImageView外貌
- 一分钟实现向左拖拽跳转详情页
- 一分钟实现QQ首页动画特效
- 一分钟实现ViewPager上下滑动
联网
工具
数据库
源码分析相关面试题
Activity相关面试题
- 保存Activity的状态
- 深刻剖析activity启动模式(一)
- 深刻剖析activity启动模式(二)
- 深刻剖析activity启动模式(三)
- Activity Task和Process之间的关系
- 源码分析service开启Activity抛异常?activity不会抛异常
- Activity优雅退出
- onCreate源码分析
Service相关面试题
与XMPP相关面试题
与性能优化相关面试题
与登录相关面试题
与开发相关面试题
- 迭代开发的时候如何向前兼容新旧接口
- 手把手教你如何解决as jar包冲突
- context的原理分析
- 解决ViewPager.setCurrentItem中间很多页面切换方案
- 字体适配
- 软键盘适配
- 机型适配,例如三星、小米、华为、魅族等
- CardView 设置水波纹效果
与人事相关面试题
配套视频 :
http://www.toutiao.com/i6430935144185463297/
</img></img>
使用步骤:
第一步:初始化ViewPager:
<android.support.v4.view.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 (goingLeft) {
realCurrentPosition = position + 1;
nextPosition = position;
realOffset = 1 - positionOffset;
} else {
nextPosition = position + 1;
realCurrentPosition = position;
realOffset = positionOffset;
}
if (nextPosition > mAdapter.getCount() - 1
|| realCurrentPosition > mAdapter.getCount() - 1) {
return;
}
CardView currentCard = mAdapter.getCardViewAt(realCurrentPosition);
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);
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<CardFragment> 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);
-
欢迎关注微信公众号、长期为您推荐优秀博文、开源项目、视频
-
微信公众号名称:Android干货程序员