Skip to content

Commit

Permalink
1.3.0版本发布
Browse files Browse the repository at this point in the history
  • Loading branch information
crazysunj committed May 8, 2018
1 parent 4740375 commit 7d071bb
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 56 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# CardSlideView
一行代码实现ViewPager卡片效果,支持无限循环,支持正常与卡片之间的切换

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

## 效果

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

```
compile 'com.crazysunj:cardslideview:1.2.1'
implementation 'com.crazysunj:cardslideview:1.3.0'
同时还需要依赖自己的v4包和cardview包
```

## 感谢
Expand Down
27 changes: 14 additions & 13 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
compileSdkVersion 27
buildToolsVersion "27.0.3"
defaultConfig {
applicationId "com.crazysunj.cardslide"
minSdkVersion 15
targetSdkVersion 26
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand All @@ -24,19 +24,20 @@ android {
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
implementation fileTree(include: ['*.jar'], dir: 'libs')
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile project(':cardslideview')
implementation project(':cardslideview')
// compile 'com.github.bumptech.glide:glide:3.8.0'
// compile 'com.crazysunj:cardslideview:1.2.1'
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.github.bumptech.glide:glide:4.0.0'
testCompile 'junit:junit:4.12'
testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.github.bumptech.glide:glide:4.0.0'
testImplementation 'junit:junit:4.12'
testImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
annotationProcessor 'com.github.bumptech.glide:compiler:4.0.0'
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.4'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.4'
releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
implementation 'com.android.support:cardview-v7:27.1.1'
}
4 changes: 2 additions & 2 deletions app/src/main/java/com/crazysunj/cardslide/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ protected void onCreate(Bundle savedInstanceState) {
list.add(new MyBean("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3093044513,96496376&fm=26&gp=0.jpg"));
list.add(new MyBean("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3210647450,3365972530&fm=26&gp=0.jpg"));
list.add(new MyBean("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1152354189,4075004834&fm=26&gp=0.jpg"));
// list.add(new MyBean("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1241938828,3177192306&fm=26&gp=0.jpg"));
// list.add(new MyBean("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=934278520,495630521&fm=26&gp=0.jpg"));
list.add(new MyBean("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1241938828,3177192306&fm=26&gp=0.jpg"));
list.add(new MyBean("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=934278520,495630521&fm=26&gp=0.jpg"));
// list.add(new MyBean("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=800274749,3560269987&fm=26&gp=0.jpg"));
viewPager.bind(getSupportFragmentManager(), new MyCardHandler(), list);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ext {
userOrg = 'twsunj'
groupId = 'com.crazysunj'
uploadName = 'CardSlideView'
publishVersion = '1.2.1'
publishVersion = '1.3.0'
desc = 'CardSlideView For Android'
website = 'https://github.com/crazysunj/CardSlideView'
licences = ['Apache-2.0']
Expand Down
11 changes: 5 additions & 6 deletions cardslideview/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.library'
apply plugin: 'bintray-release'
android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
compileSdkVersion 27
buildToolsVersion "27.0.3"

defaultConfig {
minSdkVersion 15
targetSdkVersion 26
targetSdkVersion 27
versionCode 1
versionName rootProject.ext.publishVersion
}
Expand All @@ -23,9 +23,8 @@ android {
}

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

publish {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.crazysunj.cardslideview;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;

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

protected Context mContext;

@CardViewPager.TransformerMode
int currentMode;

@Override
public void onAttach(Context context) {
super.onAttach(context);
mContext = context;
}

public void bindHandler(CardHandler<T> handler) {
Bundle bundle = getArguments();
if (bundle == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.crazysunj.cardslideview;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
Expand All @@ -41,10 +42,11 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
if (handler == null) {
throw new RuntimeException("please bind the handler !");
}
return handler.onBind(mContext, data, position, currentMode);
final Context context = getContext();
return handler.onBind(context, data, position, currentMode);
}

public void bindData(T data, int position) {
void bindData(T data, int position) {
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

import java.util.List;

import static com.crazysunj.cardslideview.CardViewPager.CACHE_COUNT;

/**
* description
* <p>viewPager适配器
Expand All @@ -31,6 +33,8 @@ class CardPagerAdapter extends FragmentStatePagerAdapter {

private static final int MAX_VALUE = Integer.MAX_VALUE;

static final int DIFF_COUNT = CACHE_COUNT / 2;

private List<CardItem> mCardItems;
private boolean mIsLoop;

Expand All @@ -41,7 +45,9 @@ class CardPagerAdapter extends FragmentStatePagerAdapter {
}

void setCardMode(@CardViewPager.TransformerMode int mode) {
if (mCardItems == null || mCardItems.isEmpty()) return;
if (mCardItems == null || mCardItems.isEmpty()) {
return;
}
for (CardItem cardItem : mCardItems) {
cardItem.currentMode = mode;
}
Expand All @@ -54,12 +60,17 @@ public Fragment getItem(int position) {

@Override
public int getCount() {
return mIsLoop ? MAX_VALUE : getRealCount();
final int realCount = getRealCount();
if (realCount == 0) {
return 0;
}
return mIsLoop ? MAX_VALUE : realCount;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
return super.instantiateItem(container, mIsLoop ? position % getRealCount() : position);
final int j = position % getRealCount();
return super.instantiateItem(container, mIsLoop ? j : position);
}

@Override
Expand All @@ -69,11 +80,7 @@ public void destroyItem(ViewGroup container, int position, Object object) {
int pos = viewPager.getCurrentItem();
int i = pos % getRealCount();
int j = position % getRealCount();
if (viewPager.isNotify) {
super.destroyItem(container, j, object);
return;
}
if (viewPager.isCardMode() && j >= i - 2 && j <= i + 2) {
if (Math.abs(i - j) != DIFF_COUNT && !viewPager.isNotify) {
return;
}
super.destroyItem(container, j, object);
Expand All @@ -85,14 +92,17 @@ public void destroyItem(ViewGroup container, int position, Object object) {
@Override
public void startUpdate(ViewGroup container) {
super.startUpdate(container);
final int realCount = getRealCount();
if (realCount == 0) {
return;
}
if (mIsLoop) {
CardViewPager viewPager = (CardViewPager) container;
int position = viewPager.getCurrentItem();
if (position == 0) {
position = getFristItem();
position = getFirstItem(realCount);
} else if (position == getCount() - 1) {
final int realCount = getRealCount();
position = getLastItem(position % realCount);
position = getLastItem(realCount, position % realCount);
}
viewPager.setCurrentItem(position, false);
}
Expand All @@ -102,13 +112,11 @@ private int getRealCount() {
return mCardItems == null ? 0 : mCardItems.size();
}

private int getFristItem() {
int realCount = getRealCount();
private int getFirstItem(int realCount) {
return MAX_VALUE / realCount / 2 * realCount;
}

private int getLastItem(int index) {
int realCount = getRealCount();
private int getLastItem(int realCount, int index) {
return MAX_VALUE / realCount / 2 * realCount + index;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@ class CardTransformer implements ViewPager.PageTransformer {

@Override
public void transformPage(View page, float position) {

if (mViewPager == null) {
mViewPager = (ViewPager) page.getParent();
}

int leftInScreen = page.getLeft() - mViewPager.getScrollX();
int centerXInViewPager = leftInScreen + page.getMeasuredWidth() / 2;
int offsetX = centerXInViewPager - mViewPager.getMeasuredWidth() / 2;
Expand Down
Loading

0 comments on commit 7d071bb

Please sign in to comment.