Skip to content

Commit

Permalink
1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
crazysunj committed Aug 29, 2017
1 parent b88d0c5 commit 937dcc9
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 22 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# CardSlideView
一行代码实现ViewPager卡片效果,支持无限循环
一行代码实现ViewPager卡片效果,支持无限循环,支持正常与卡片之间的切换

## 效果

Expand Down Expand Up @@ -34,10 +34,12 @@ public class MyCardHandler implements CardHandler<String> {

详细介绍戳[这里](http://crazysunj.com/2017/06/25/%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0ViewPager%E5%8D%A1%E7%89%87%E6%95%88%E6%9E%9C/)

具体参考demo

## gradle依赖

```
compile 'com.crazysunj:cardslideview:1.1.1'
compile 'com.crazysunj:cardslideview:1.2.0'
```

## 感谢
Expand Down
36 changes: 30 additions & 6 deletions app/src/main/java/com/crazysunj/cardslide/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.crazysunj.cardslideview.CardViewPager;

Expand Down Expand Up @@ -80,6 +80,8 @@ public class MainActivity extends AppCompatActivity {
"https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1241938828,3177192306&fm=26&gp=0.jpg"};
private CardViewPager viewPager;

private boolean isCard = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -92,12 +94,34 @@ 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://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=800274749,3560269987&fm=26&gp=0.jpg"));
Log.d("MainActivity", "list:" + list);
// 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.bind(getSupportFragmentManager(), new MyCardHandler(), Arrays.asList(imageArray));
}

public void onClick(View view) {
if (isCard) {
isCard = false;
switchNormal();
} else {
isCard = true;
switchCard();
}
}

private void switchNormal() {
viewPager.setCardTransformer(0, 0);
viewPager.setCardPadding(0);
viewPager.setCardMargin(0);
viewPager.notifyUI(CardViewPager.MODE_NORMAL);
}

private void switchCard() {
viewPager.setCardTransformer(180, 0.38f);
viewPager.setCardPadding(60);
viewPager.setCardMargin(40);
viewPager.notifyUI(CardViewPager.MODE_CARD);
}
}
8 changes: 5 additions & 3 deletions app/src/main/java/com/crazysunj/cardslide/MyCardHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.bumptech.glide.Glide;
import com.crazysunj.cardslideview.CardHandler;
import com.crazysunj.cardslideview.CardViewPager;
import com.crazysunj.cardslideview.ElasticCardView;

/**
Expand All @@ -18,12 +19,13 @@
public class MyCardHandler implements CardHandler<MyBean> {

@Override
public View onBind(final Context context, final MyBean data, final int position) {
public View onBind(final Context context, final MyBean data, final int position, @CardViewPager.TransformerMode int mode) {
View view = View.inflate(context, R.layout.item, null);
ImageView imageView = (ImageView) view.findViewById(R.id.image);
ElasticCardView cardView = (ElasticCardView) view.findViewById(R.id.cardview);
cardView.setPreventCornerOverlap(false);
cardView.setUseCompatPadding(false);
final boolean isCard = mode == CardViewPager.MODE_CARD;
cardView.setPreventCornerOverlap(isCard);
cardView.setUseCompatPadding(isCard);
final String img = data.getImg();
Glide.with(context).load(img).into(imageView);
view.setOnClickListener(new View.OnClickListener() {
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@
android:layout_height="match_parent"
app:card_loop="true" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="变换" />

</FrameLayout>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ext {
userOrg = 'twsunj'
groupId = 'com.crazysunj'
uploadName = 'CardSlideView'
publishVersion = '1.1.1'
publishVersion = '1.2.0'
desc = 'CardSlideView For Android'
website = 'https://github.com/crazysunj/CardSlideView'
licences = ['Apache-2.0']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class BaseCardItem<T> extends Fragment {

protected Context mContext;

@CardViewPager.TransformerMode
int currentMode;

@Override
public void onAttach(Context context) {
super.onAttach(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@
*/
public interface CardHandler<T> extends Serializable {

View onBind(Context context, T data, int position);
View onBind(Context context, T data, int position, @CardViewPager.TransformerMode int mode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@
*/
public class CardItem<T extends Serializable> extends BaseCardItem<T> {

@SuppressWarnings("unchecked")
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

Bundle bundle = getArguments();
CardHandler<T> handler = (CardHandler<T>) bundle.getSerializable(ARGUMENTS_HANDLER);
T data = (T) bundle.getSerializable(ARGUMENTS_DATA);
int position = bundle.getInt(ARGUMENTS_POSITION, 0);
if (handler == null) {
throw new RuntimeException("please bind the handler !");
}
return handler.onBind(mContext, data, position);
return handler.onBind(mContext, data, position, currentMode);
}

public void bindData(T data, int position) {
Expand All @@ -53,4 +53,5 @@ public void bindData(T data, int position) {
bundle.putSerializable(ARGUMENTS_DATA, data);
bundle.putInt(ARGUMENTS_POSITION, position);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ class CardPagerAdapter extends FragmentStatePagerAdapter {
mIsLoop = isLoop;
}

void setCardMode(@CardViewPager.TransformerMode int mode) {
if (mCardItems == null || mCardItems.isEmpty()) return;
for (CardItem cardItem : mCardItems) {
cardItem.currentMode = mode;
}
}

@Override
public Fragment getItem(int position) {
return mCardItems.get(position);
Expand All @@ -62,7 +69,11 @@ public void destroyItem(ViewGroup container, int position, Object object) {
int pos = viewPager.getCurrentItem();
int i = pos % getRealCount();
int j = position % getRealCount();
if (j >= i - 2 && j <= i + 2) {
if (viewPager.isNotify) {
super.destroyItem(container, j, object);
return;
}
if (viewPager.isCardMode() && j >= i - 2 && j <= i + 2) {
return;
}
super.destroyItem(container, j, object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;

import java.io.Serializable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -35,10 +39,28 @@
*/
public class CardViewPager extends ViewPager {

private static final int CACHE_COPUNT = 6;

public static final int MODE_CARD = 0;
public static final int MODE_NORMAL = 1;

@IntDef({MODE_CARD, MODE_NORMAL})
@Retention(RetentionPolicy.SOURCE)
public @interface TransformerMode {
}

private int mMaxOffset;
private float mScaleRate;
private boolean mIsLoop = false;
private int mCardPaddingLeft;
private int mCardPaddingTop;
private int mCardPaddingRight;
private int mCardPaddingBottom;
private CardTransformer mTransformer;
@TransformerMode
private int mCurrentMode = MODE_CARD;

boolean isNotify;

public CardViewPager(Context context) {
this(context, null);
Expand All @@ -54,7 +76,11 @@ public CardViewPager(Context context, AttributeSet attrs) {
int padding = typedArray
.getDimensionPixelOffset(R.styleable.CardViewPager_card_padding,
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 60, displayMetrics));
setPadding(getPaddingLeft() + padding, getPaddingTop(), getPaddingRight() + padding, getPaddingBottom());
mCardPaddingLeft = getPaddingLeft();
mCardPaddingTop = getPaddingTop();
mCardPaddingRight = getPaddingRight();
mCardPaddingBottom = getPaddingBottom();
setPadding(mCardPaddingLeft + padding, mCardPaddingTop, mCardPaddingRight + padding, mCardPaddingBottom);

int margin = typedArray
.getDimensionPixelOffset(R.styleable.CardViewPager_card_margin,
Expand All @@ -78,12 +104,62 @@ public CardViewPager(Context context, AttributeSet attrs) {
* @param maxOffset 移动偏移量
* @param scaleRate 缩放比例
*/
public void setCardTransformer(int maxOffset, float scaleRate) {
mTransformer = new CardTransformer(maxOffset, scaleRate);
public void setCardTransformer(float maxOffset, float scaleRate) {
int cardMaxOffset = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, maxOffset, getResources().getDisplayMetrics());
mTransformer = new CardTransformer(cardMaxOffset, scaleRate);
setPageTransformer(false, mTransformer);
}

/**
* 设置卡片左右padding
*
* @param padding 值,自动转dp
*/
public void setCardPadding(float padding) {
int cardPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, padding, getResources().getDisplayMetrics());
setPadding(mCardPaddingLeft + cardPadding, mCardPaddingTop, mCardPaddingRight + cardPadding, mCardPaddingBottom);
}

/**
* 设置卡片margin
*
* @param margin 值,自动转dp
*/
public void setCardMargin(float margin) {
int cardMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, margin, getResources().getDisplayMetrics());
setPageMargin(cardMargin);
}

/**
* 根据模式刷新通知UI刷新
*
* @param mode 模式
*/
public void notifyUI(@TransformerMode int mode) {
mCurrentMode = mode;
isNotify = true;
CardPagerAdapter adapter = (CardPagerAdapter) getAdapter();
adapter.setCardMode(mCurrentMode);
setAdapter(adapter);
isNotify = false;
}

boolean isCardMode() {
return mCurrentMode == MODE_CARD;
}

public int getCurrentMode() {
return mCurrentMode;
}

/**
* 绑定数据源
*
* @param fm FragmentManager
* @param handler 数据处理类
* @param data 数据源
* @param <T> 泛型,必须实现Serializable
*/
public <T extends Serializable> void bind(FragmentManager fm, CardHandler<T> handler, List<T> data) {
List<CardItem> cardItems = getCardItems(handler, data, mIsLoop);
if (mTransformer == null) {
Expand All @@ -98,9 +174,8 @@ public <T extends Serializable> void bind(FragmentManager fm, CardHandler<T> han
private <T extends Serializable> List<CardItem> getCardItems(CardHandler<T> handler, List<T> data, boolean isLoop) {
List<CardItem> cardItems = new ArrayList<CardItem>();
int dataSize = data.size();
int cacheCount = 6;
boolean isExpand = isLoop && dataSize < cacheCount;
int size = isExpand ? cacheCount : dataSize;
boolean isExpand = isLoop && dataSize < CACHE_COPUNT;
int size = isExpand ? CACHE_COPUNT : dataSize;
for (int i = 0; i < size; i++) {
int position = isExpand ? i % dataSize : i;
T t = data.get(position);
Expand All @@ -111,4 +186,12 @@ private <T extends Serializable> List<CardItem> getCardItems(CardHandler<T> hand
}
return cardItems;
}

@Override
public void setAdapter(PagerAdapter adapter) {
if (!(adapter instanceof CardPagerAdapter)) {
throw new RuntimeException("please set CardPagerAdapter!");
}
super.setAdapter(adapter);
}
}

0 comments on commit 937dcc9

Please sign in to comment.