Skip to content

Commit 7d071bb

Browse files
committed
1.3.0版本发布
1 parent 4740375 commit 7d071bb

11 files changed

Lines changed: 106 additions & 56 deletions

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# CardSlideView
22
一行代码实现ViewPager卡片效果,支持无限循环,支持正常与卡片之间的切换
33

4+
想实现轮播效果的同学,可以参考[CrazyDaily](https://github.com/crazysunj/CrazyDaily "https://github.com/crazysunj/CrazyDaily")开源项目首页实现。
5+
46
## 效果
57

68
![](https://github.com/crazysunj/crazysunj.github.io/blob/master/img/vp_card5.gif)
@@ -66,7 +68,8 @@ public class A {
6668
## gradle依赖
6769

6870
```
69-
compile 'com.crazysunj:cardslideview:1.2.1'
71+
implementation 'com.crazysunj:cardslideview:1.3.0'
72+
同时还需要依赖自己的v4包和cardview包
7073
```
7174

7275
## 感谢

app/build.gradle

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 26
5-
buildToolsVersion "26.0.2"
4+
compileSdkVersion 27
5+
buildToolsVersion "27.0.3"
66
defaultConfig {
77
applicationId "com.crazysunj.cardslide"
88
minSdkVersion 15
9-
targetSdkVersion 26
9+
targetSdkVersion 27
1010
versionCode 1
1111
versionName "1.0"
1212
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
@@ -24,19 +24,20 @@ android {
2424
}
2525

2626
dependencies {
27-
compile fileTree(include: ['*.jar'], dir: 'libs')
28-
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
27+
implementation fileTree(include: ['*.jar'], dir: 'libs')
28+
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
2929
exclude group: 'com.android.support', module: 'support-annotations'
3030
})
31-
compile project(':cardslideview')
31+
implementation project(':cardslideview')
3232
// compile 'com.github.bumptech.glide:glide:3.8.0'
3333
// compile 'com.crazysunj:cardslideview:1.2.1'
34-
compile 'com.android.support:appcompat-v7:26.1.0'
35-
compile 'com.android.support.constraint:constraint-layout:1.0.2'
36-
compile 'com.github.bumptech.glide:glide:4.0.0'
37-
testCompile 'junit:junit:4.12'
38-
testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
34+
implementation 'com.android.support:appcompat-v7:27.1.1'
35+
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
36+
implementation 'com.github.bumptech.glide:glide:4.0.0'
37+
testImplementation 'junit:junit:4.12'
38+
testImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
3939
annotationProcessor 'com.github.bumptech.glide:compiler:4.0.0'
40-
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.4'
41-
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
40+
debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.4'
41+
releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
42+
implementation 'com.android.support:cardview-v7:27.1.1'
4243
}

app/src/main/java/com/crazysunj/cardslide/MainActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ protected void onCreate(Bundle savedInstanceState) {
9696
list.add(new MyBean("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3093044513,96496376&fm=26&gp=0.jpg"));
9797
list.add(new MyBean("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3210647450,3365972530&fm=26&gp=0.jpg"));
9898
list.add(new MyBean("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1152354189,4075004834&fm=26&gp=0.jpg"));
99-
// list.add(new MyBean("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1241938828,3177192306&fm=26&gp=0.jpg"));
100-
// list.add(new MyBean("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=934278520,495630521&fm=26&gp=0.jpg"));
99+
list.add(new MyBean("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1241938828,3177192306&fm=26&gp=0.jpg"));
100+
list.add(new MyBean("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=934278520,495630521&fm=26&gp=0.jpg"));
101101
// list.add(new MyBean("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=800274749,3560269987&fm=26&gp=0.jpg"));
102102
viewPager.bind(getSupportFragmentManager(), new MyCardHandler(), list);
103103
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ ext {
2525
userOrg = 'twsunj'
2626
groupId = 'com.crazysunj'
2727
uploadName = 'CardSlideView'
28-
publishVersion = '1.2.1'
28+
publishVersion = '1.3.0'
2929
desc = 'CardSlideView For Android'
3030
website = 'https://github.com/crazysunj/CardSlideView'
3131
licences = ['Apache-2.0']

cardslideview/build.gradle

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
apply plugin: 'com.android.library'
22
apply plugin: 'bintray-release'
33
android {
4-
compileSdkVersion 26
5-
buildToolsVersion "26.0.2"
4+
compileSdkVersion 27
5+
buildToolsVersion "27.0.3"
66

77
defaultConfig {
88
minSdkVersion 15
9-
targetSdkVersion 26
9+
targetSdkVersion 27
1010
versionCode 1
1111
versionName rootProject.ext.publishVersion
1212
}
@@ -23,9 +23,8 @@ android {
2323
}
2424

2525
dependencies {
26-
compile fileTree(dir: 'libs', include: ['*.jar'])
27-
compile 'com.android.support:support-v4:26.1.0'
28-
compile 'com.android.support:cardview-v7:26.1.0'
26+
compileOnly 'com.android.support:support-v4:27.1.1'
27+
compileOnly 'com.android.support:cardview-v7:27.1.1'
2928
}
3029

3130
publish {

cardslideview/src/main/java/com/crazysunj/cardslideview/BaseCardItem.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package com.crazysunj.cardslideview;
1717

18-
import android.content.Context;
1918
import android.os.Bundle;
2019
import android.support.v4.app.Fragment;
2120

@@ -30,17 +29,10 @@ public class BaseCardItem<T> extends Fragment {
3029
protected static final String ARGUMENTS_DATA = "cards_data";
3130
protected static final String ARGUMENTS_POSITION = "cards_position";
3231

33-
protected Context mContext;
3432

3533
@CardViewPager.TransformerMode
3634
int currentMode;
3735

38-
@Override
39-
public void onAttach(Context context) {
40-
super.onAttach(context);
41-
mContext = context;
42-
}
43-
4436
public void bindHandler(CardHandler<T> handler) {
4537
Bundle bundle = getArguments();
4638
if (bundle == null) {

cardslideview/src/main/java/com/crazysunj/cardslideview/CardItem.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.crazysunj.cardslideview;
1717

18+
import android.content.Context;
1819
import android.os.Bundle;
1920
import android.support.annotation.Nullable;
2021
import android.view.LayoutInflater;
@@ -41,10 +42,11 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
4142
if (handler == null) {
4243
throw new RuntimeException("please bind the handler !");
4344
}
44-
return handler.onBind(mContext, data, position, currentMode);
45+
final Context context = getContext();
46+
return handler.onBind(context, data, position, currentMode);
4547
}
4648

47-
public void bindData(T data, int position) {
49+
void bindData(T data, int position) {
4850
Bundle bundle = getArguments();
4951
if (bundle == null) {
5052
bundle = new Bundle();

cardslideview/src/main/java/com/crazysunj/cardslideview/CardPagerAdapter.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import java.util.List;
2424

25+
import static com.crazysunj.cardslideview.CardViewPager.CACHE_COUNT;
26+
2527
/**
2628
* description
2729
* <p>viewPager适配器
@@ -31,6 +33,8 @@ class CardPagerAdapter extends FragmentStatePagerAdapter {
3133

3234
private static final int MAX_VALUE = Integer.MAX_VALUE;
3335

36+
static final int DIFF_COUNT = CACHE_COUNT / 2;
37+
3438
private List<CardItem> mCardItems;
3539
private boolean mIsLoop;
3640

@@ -41,7 +45,9 @@ class CardPagerAdapter extends FragmentStatePagerAdapter {
4145
}
4246

4347
void setCardMode(@CardViewPager.TransformerMode int mode) {
44-
if (mCardItems == null || mCardItems.isEmpty()) return;
48+
if (mCardItems == null || mCardItems.isEmpty()) {
49+
return;
50+
}
4551
for (CardItem cardItem : mCardItems) {
4652
cardItem.currentMode = mode;
4753
}
@@ -54,12 +60,17 @@ public Fragment getItem(int position) {
5460

5561
@Override
5662
public int getCount() {
57-
return mIsLoop ? MAX_VALUE : getRealCount();
63+
final int realCount = getRealCount();
64+
if (realCount == 0) {
65+
return 0;
66+
}
67+
return mIsLoop ? MAX_VALUE : realCount;
5868
}
5969

6070
@Override
6171
public Object instantiateItem(ViewGroup container, int position) {
62-
return super.instantiateItem(container, mIsLoop ? position % getRealCount() : position);
72+
final int j = position % getRealCount();
73+
return super.instantiateItem(container, mIsLoop ? j : position);
6374
}
6475

6576
@Override
@@ -69,11 +80,7 @@ public void destroyItem(ViewGroup container, int position, Object object) {
6980
int pos = viewPager.getCurrentItem();
7081
int i = pos % getRealCount();
7182
int j = position % getRealCount();
72-
if (viewPager.isNotify) {
73-
super.destroyItem(container, j, object);
74-
return;
75-
}
76-
if (viewPager.isCardMode() && j >= i - 2 && j <= i + 2) {
83+
if (Math.abs(i - j) != DIFF_COUNT && !viewPager.isNotify) {
7784
return;
7885
}
7986
super.destroyItem(container, j, object);
@@ -85,14 +92,17 @@ public void destroyItem(ViewGroup container, int position, Object object) {
8592
@Override
8693
public void startUpdate(ViewGroup container) {
8794
super.startUpdate(container);
95+
final int realCount = getRealCount();
96+
if (realCount == 0) {
97+
return;
98+
}
8899
if (mIsLoop) {
89100
CardViewPager viewPager = (CardViewPager) container;
90101
int position = viewPager.getCurrentItem();
91102
if (position == 0) {
92-
position = getFristItem();
103+
position = getFirstItem(realCount);
93104
} else if (position == getCount() - 1) {
94-
final int realCount = getRealCount();
95-
position = getLastItem(position % realCount);
105+
position = getLastItem(realCount, position % realCount);
96106
}
97107
viewPager.setCurrentItem(position, false);
98108
}
@@ -102,13 +112,11 @@ private int getRealCount() {
102112
return mCardItems == null ? 0 : mCardItems.size();
103113
}
104114

105-
private int getFristItem() {
106-
int realCount = getRealCount();
115+
private int getFirstItem(int realCount) {
107116
return MAX_VALUE / realCount / 2 * realCount;
108117
}
109118

110-
private int getLastItem(int index) {
111-
int realCount = getRealCount();
119+
private int getLastItem(int realCount, int index) {
112120
return MAX_VALUE / realCount / 2 * realCount + index;
113121
}
114122
}

cardslideview/src/main/java/com/crazysunj/cardslideview/CardTransformer.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,9 @@ class CardTransformer implements ViewPager.PageTransformer {
3737

3838
@Override
3939
public void transformPage(View page, float position) {
40-
4140
if (mViewPager == null) {
4241
mViewPager = (ViewPager) page.getParent();
4342
}
44-
4543
int leftInScreen = page.getLeft() - mViewPager.getScrollX();
4644
int centerXInViewPager = leftInScreen + page.getMeasuredWidth() / 2;
4745
int offsetX = centerXInViewPager - mViewPager.getMeasuredWidth() / 2;

0 commit comments

Comments
 (0)