Skip to content

Commit

Permalink
2.2.0版本发布
Browse files Browse the repository at this point in the history
  • Loading branch information
crazysunj committed Nov 7, 2020
1 parent 313aef1 commit cd875f6
Show file tree
Hide file tree
Showing 30 changed files with 1,874 additions and 729 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@

[![](https://img.shields.io/github/release/crazysunj/CardSlideView.svg) ](https://github.com/crazysunj/CardSlideView/releases)

一行代码实现ViewPager卡片效果,支持无限循环,支持正常与卡片之间的切换,随意变换,2.x版本采用RecyclerView实现

想实现轮播效果的同学,可以参考[CrazyDaily](https://github.com/crazysunj/CrazyDaily "https://github.com/crazysunj/CrazyDaily")开源项目首页实现。
一行代码实现ViewPager卡片效果,比ViewPager2更强大,底层同样是RecyclerView

## 效果

![](https://github.com/crazysunj/crazysunj.github.io/blob/master/img/vp_card5.gif)
![](https://github.com/crazysunj/crazysunj.github.io/blob/master/img/card_slide_2_2_0.gif)

## 功能
* ItemDecoration
* 可设item之间间距,可做到叠加
* 可设各个view自身的padding和margin
* 横竖两个方向
* 无限循环
* page和linear两种滑动方式
* 百分比适配,但只会根据宽高其中一个维度适配
* 非无循环模式边界支持回弹,可动态设置是否开启

想实现轮播效果的同学,1.x版本可以参考[CrazyDaily](https://github.com/crazysunj/CrazyDaily "https://github.com/crazysunj/CrazyDaily")开源项目首页实现,2.x版本可以参考本项目实例,还有炫酷的指示器、3D旋转和倒影效果哦

## 用法

Expand Down
144 changes: 117 additions & 27 deletions README_2.0.md
Original file line number Diff line number Diff line change
@@ -1,61 +1,151 @@
## 2.x

### 实例
```
CardSlideView<MyBean> slideView = (CardSlideView) findViewById(R.id.slide_view);
slideView.bind(list, new MyCardHolder());
slideView.bind(list, new NormalCardHolder());
```


```
static class MyCardHolder implements CardHolder<MyBean> {
static class NormalCardHolder implements CardHolder<MyBean> {
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
return inflater.inflate(R.layout.item, container, false);
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
return inflater.inflate(R.layout.item, container, false);
}
@Override
public void onBindView(@NonNull CardViewHolder holder, MyBean data, int position) {
Log.e("MainActivity", "onBindView---data:" + data + "position:" + position);
ImageView imageView = holder.getView(R.id.image);
final String img = data.getImg();
Glide.with(imageView.getContext()).load(img).apply(new RequestOptions().dontAnimate()).into(imageView);
holder.itemView.setOnClickListener(v -> {
Log.e("MainActivity", "setOnClickListener---data:" + data + "position:" + position);
TestActivity.start(v.getContext(), img);
});
}
@Override
public void onBindView(@NonNull CardViewHolder holder, MyBean data, int position) {
ImageView imageView = holder.getView(R.id.image);
final String img = data.getImg();
Glide.with(imageView.getContext()).load(img).apply(new RequestOptions().dontAnimate().placeholder(new ColorDrawable(Color.WHITE))).into(imageView);
}
}
```

### 方法简介
```
// 获取当前选中下标
// 获取当前选中下标,滑动过程中并不会改动
int currentItem = slideView.getCurrentItem();
// 获取当前距离中心最近的view,这个是实时的
int centerView = slideView.getCenterView();
// 获取当前的方向
int orientation = slideView.getOrientation();
// 设置变换效果,默认是缩放的,最低0.8,实现PageTransformer接口即可
slideView.setItemTransformer(new MyScale());
// 设置变换效果,默认是缩放+透明度,最低0.8,实现PageTransformer接口即可
slideView.setItemTransformer(new DefaultTransformer());
static class MyScale implements PageTransformer {
public class DefaultTransformer implements PageTransformer {
public DefaultTransformer() {
}
@Override
public void transformPage(@NonNull View view, float offsetPercent, int orientation) {
view.setScaleX(1.0f);
view.setScaleY(1.0f);
if (orientation == LinearLayout.HORIZONTAL) {
if (offsetPercent > 0) {
view.setPivotX(view.getWidth());
view.setPivotY(view.getHeight() / 2.f);
} else {
view.setPivotX(0);
view.setPivotY(view.getHeight() / 2.f);
}
} else {
if (offsetPercent > 0) {
view.setPivotX(view.getWidth() / 2.f);
view.setPivotY(view.getHeight());
} else {
view.setPivotX(view.getWidth() / 2.f);
view.setPivotY(0);
}
}
final float finalPercent = 1 - Math.min(Math.abs(offsetPercent), 2.f) / 2.f;
float scale = 0.8f + 0.2f * finalPercent;
view.setScaleX(scale);
view.setScaleY(scale);
final float alpha = (float) Math.pow(finalPercent, 0.8);
view.setAlpha(alpha);
}
}
// 设置卡片的滑动效果,默认是page,类似于viewPager,库里除了提供这个效果,还提供了快速线性滑动
slideView.setSnapHelper(new CardLinearSnapHelper());
// 设置是否无限循环
slideView.setLooper(true);
// 设置page滑动监听,监听方式跟viewPager类似,在惯性滑动时不会回调
// 设置page滑动监听,监听方式跟viewPager类似,在滑动时不会回调
slideView.setOnPageChangeListener(new MyPageChangeListener());
public interface OnPageChangeListener {
void onPageSelected(int position);
}
// 支持RecyclerView的ItemDecoration
public RecyclerView.ItemDecoration getItemDecorationAt(int index);
// 获取绑定的数据
public List<T> getData();
// 获取总item数
public int getItemCount();
// 获取对应下标ItemDecoration
public RecyclerView.ItemDecoration getItemDecorationAt(int index);
// 获取ItemDecoration已设置总数
public int getItemDecorationCount();
// 获取对应view在列表中的具体下标
public int getPosition(@NonNull View view);
// 移除对应ItemDecoration
public void removeItemDecoration(@NonNull RecyclerView.ItemDecoration decor);
public void removeItemDecorationAt(int index);
// 设置横竖方向是否可滑动
public void setCanScrollHorizontally(boolean canScrollHorizontally);
public void setCanScrollVertically(boolean canScrollVertically);
// 切到对应下标,smoothScroll为true带动画
public void setCurrentItem(int item, boolean smoothScroll);
// 设置每个item的宽高比
public void setItemRate(float itemRate);
// 设置中间item距离两边空隙的距离,为item宽度的百分比
public void setSideOffsetPercent(float sideOffsetPercent);
// 设置item点击事件,如果点击非中间item,那么点击item将会移动到中间
slideView.setOnPageItemClickListener(new MyPageItemClickListener());
public interface OnPageItemClickListener<T> {
void onItemClick(View view, T data, int position);
}
#BitmapHelper
// 支持bitmap倒影转换,建议放在子线程
public static Bitmap convertReflection(@NonNull Bitmap originalImage, int viewWidth, int viewHeight) ;
```

### 属性简介
```
// item间距百分比,以item宽度为基准,范围-1~1
<attr name="card_item_margin_percent" format="float" />
// 是否支持无线循环
<attr name="card_loop" format="boolean" />
// 是否支持回弹效果,只在非无限循环下生效
<attr name="card_rebound" format="boolean" />
// 中间item两边留边距离百分比,以item宽度为基准,范围0-1
<attr name="card_side_offset_percent" format="float" />
// item的宽高比
<attr name="card_item_rate" format="float" />
// 滑动模式,page-类似ViewPager,linear-类似平时RecyclerView(有作滑动优化)
<attr name="card_page_mode" format="enum">
<enum name="linear" value="0" />
<enum name="page" value="1" />
</attr>
```

## gradle依赖

```
implementation 'com.crazysunj:cardslideview:2.0.0'
implementation 'com.crazysunj:cardslideview:2.2.0'
同时还需要依赖自己的v4包和recyclerview包,androidx哦
```

Expand Down
26 changes: 10 additions & 16 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 28
compileSdkVersion 30
defaultConfig {
applicationId "com.crazysunj.cardslide"
minSdkVersion 15
targetSdkVersion 28
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand All @@ -24,21 +24,15 @@ android {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
// implementation project(':cardslideview')
implementation 'com.crazysunj:cardslideview:2.0.0'
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation('com.github.bumptech.glide:glide:4.7.1') {
exclude group: "com.android.support"
}
implementation project(':cardslideview')
implementation 'com.crazysunj:cardslideview:2.2.0'
// implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.github.bumptech.glide:glide:4.11.0'
implementation('jp.wasabeef:glide-transformations:3.3.0') {
exclude group: 'com.github.bumptech.glide', module: 'glide'
}
testImplementation 'junit:junit:4.12'
testImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.4'
releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
testImplementation 'junit:junit:4.13'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
}
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name=".MainActivity"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
6 changes: 0 additions & 6 deletions app/src/main/java/com/crazysunj/cardslide/APP.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import android.app.Application;

import com.squareup.leakcanary.LeakCanary;

/**
* description
* <p>
Expand All @@ -15,9 +13,5 @@ public class APP extends Application {
@Override
public void onCreate() {
super.onCreate();
if (LeakCanary.isInAnalyzerProcess(this)) {
return;
}
LeakCanary.install(this);
}
}
Loading

0 comments on commit cd875f6

Please sign in to comment.