Skip to content

Commit

Permalink
New Api disableTouchScroll (#28)
Browse files Browse the repository at this point in the history
* Add new API setOnPageSelectedListener.
* New Api disableTouchScroll

* Optimize BannerViewPager

* Fix issue #27

* update sample
  • Loading branch information
zhpanvip authored Nov 15, 2019
1 parent 7b06c20 commit 454ee03
Show file tree
Hide file tree
Showing 38 changed files with 450 additions and 383 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

### 1.setPageStyle

[一屏多页Demo](https://github.com/zhpanvip/BannerViewPager/blob/master/app/src/main/java/com/example/zhpan/circleviewpager/activity/PageStyleActivity.java)
[一屏多页Demo](https://github.com/zhpanvip/BannerViewPager/blob/master/app/src/main/java/com/example/zhpan/circleviewpager/fragment/PageFragment.java)

| MULTI_PAGE |MULTI_PAGE_SCALE | MULTI_PAGE_OVERLAP |
|--|--|--|
Expand All @@ -23,7 +23,7 @@
### 2.setIndicatorStyle
BannerViewPager支持多种IndicatorViewStyle,同时还提供了完全自定义IndicatorView的功能。只要继承BaseIndicatorView或者实现IIndicator接口,并重写相应方法,就可以为所欲为的打造任意的Indicator了。

[IndicatorViewStyle Demo](https://github.com/zhpanvip/BannerViewPager/blob/master/app/src/main/java/com/example/zhpan/circleviewpager/activity/IndicatorStyleActivity.java)
[IndicatorViewStyle Demo](https://github.com/zhpanvip/BannerViewPager/blob/master/app/src/main/java/com/example/zhpan/circleviewpager/fragment/IndicatorFragment.java)

| CIRCLE | DASH | Custom |
|--|--|--|
Expand All @@ -38,7 +38,7 @@ BannerViewPager支持多种IndicatorViewStyle,同时还提供了完全自定义I

### 4.setPageTransformerStyle

[TransformStyle Demo](https://github.com/zhpanvip/BannerViewPager/blob/master/app/src/main/java/com/example/zhpan/circleviewpager/activity/PageTransformerActivity.java)
[TransformStyle Demo](https://github.com/zhpanvip/BannerViewPager/blob/master/app/src/main/java/com/example/zhpan/circleviewpager/activity/WelcomeActivity.java)

| 参数 | STACK | ROTATE | DEPTH | ACCORDION |
|--|--|--|--|--|
Expand Down Expand Up @@ -68,13 +68,14 @@ BannerViewPager支持多种IndicatorViewStyle,同时还提供了完全自定义I
| BannerViewPager<T, VH> setIndicatorWidth(int normalWidth, int checkWidth) | 设置指示器宽度,如果是圆形指示器,则为直径 | 默认值8dp |
| BannerViewPager<T, VH> setIndicatorHeight(int indicatorHeight) | 设置指示器高度,仅在Indicator样式为DASH时有效 | 默认值normalIndicatorWidth/2 |
| BannerViewPager<T, VH> setIndicatorGap(int indicatorMargin) | 指示器圆点间距| 默认值为指示器宽度(或者是圆的直径)|
| BannerViewPager<T, VH> setIndicatorView(IIndicator indicatorView) | 设置自定义指示器| 设置自定义指示器后以上关于IndicatorView的参数会部分失效|
| BannerViewPager<T, VH> setIndicatorView(IIndicator indicatorView) | 设置自定义指示器| |
| BannerViewPager<T, VH> setPageTransformerStyle(int style) | 设置页面Transformer内置样式 | |
| BannerViewPager<T, VH> setCurrentItem(int item) | Set the currently selected page. | 2.3.5新增 |
| void getCurrentItem() | 获取当前position | 2.3.5新增 |
| BannerViewPager<T, VH> setPageStyle(PageStyle pageStyle) | 设置页面样式 | 2.4.0新增 可选(MULTI_PAGE、NORMAL)MULTI_PAGE:一屏多页样式 |
| BannerViewPager<T, VH> setPageMargin(int pageMargin) | 设置页面间隔 | 2.4.0新增 |
| BannerViewPager<T, VH> setIndicatorMargin(int left, int top, int right, int bottom) | 设置Indicator边距 | 2.4.1新增 |
| BannerViewPager<T, VH> setOnPageChangeListener(OnPageChangeListener l) | 页面改变的监听事件 | 2.4.3新增 |
| void startLoop() |开启自动轮播 | 初始化BannerViewPager时不必调用该方法,设置setAutoPlay后会调用startLoop() |
| void stopLoop() | 停止自动轮播 | 如果开启自动轮播,为避免内存泄漏需要在onStop()或onDestroy中调用此方法 |
| List\<T> getList() | 获取Banner中的集合数据 | |
Expand Down Expand Up @@ -142,7 +143,7 @@ implementation 'com.zhpan.library:bannerview:latestVersion'
android:layout_height="160dp" />
```

### 3.Banner的页面布局
### 3.Banner的Item页面布局

```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
Expand Down Expand Up @@ -335,8 +336,9 @@ public class FigureIndicatorView extends BaseIndicatorView {
- [x] 增加IndicatorView的滑动样式(2.2.2)

- [x] 增添更多Indicator样式(2.3.+)
- [x] 支持一屏显示多页 (2.4.0)
- [ ] ViewPager更换为ViewPager2 (3.0.0)
- [x] 支持一屏显示多页 (2.4.0)
- [ ] 将v2.4.3版本中着重优化提升性能
- [ ] ViewPager更换为ViewPager2 (3.0.0)
- [ ] 目前Indicator部分代码比较乱,还有很大很大的优化空间,后续版本将持续优化


Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ android {
}
buildTypes {
release {
minifyEnabled false
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
Expand Down
11 changes: 11 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,14 @@
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

-keep class * extends androidx.fragment.app.Fragment {
public void setUserVisibleHint(boolean);
public void onHiddenChanged(boolean);
public void onResume();
public void onPause();
}
-keep public class * extends android.app.Activity
-keep class com.example.zhpan.circleviewpager.bean.** { *; }
-keep class com.example.zhpan.circleviewpager.net.** { *; }
-keep class com.zhpan.idea.** { *; }
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
import java.util.List;

public abstract class BaseDataActivity extends AppCompatActivity {

protected List<Integer> mDrawableList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initData();
}

private void initData() {
for (int i = 0; i <= 3; i++) {
int drawable = getResources().getIdentifier("t" + i, "drawable", getPackageName());
for (int i = 0; i <= 2; i++) {
int drawable = getResources().getIdentifier("guide" + i, "drawable", getPackageName());
mDrawableList.add(drawable);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import com.example.zhpan.circleviewpager.R;
import com.example.zhpan.circleviewpager.adapter.AdapterFragmentPager;
import com.zhpan.bannerview.view.CatchViewPager;

import butterknife.BindView;
import butterknife.ButterKnife;
Expand All @@ -21,7 +22,7 @@ public class MainActivity extends AppCompatActivity {
@BindView(R.id.rg_tab)
RadioGroup rgTab;
@BindView(R.id.vp_fragment)
ViewPager mViewPager;
CatchViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -35,6 +36,7 @@ protected void onCreate(Bundle savedInstanceState) {
private void initData() {
AdapterFragmentPager mAdapter = new AdapterFragmentPager(getSupportFragmentManager());
mViewPager.setAdapter(mAdapter);
mViewPager.disableTouchScroll(true);
mViewPager.setOffscreenPageLimit(2);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
Expand Down Expand Up @@ -87,5 +89,4 @@ private void setListener() {
public static void start(Context context) {
context.startActivity(new Intent(context, MainActivity.class));
}

}
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
package com.example.zhpan.circleviewpager.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.example.zhpan.circleviewpager.R;
import com.example.zhpan.circleviewpager.viewholder.PhotoViewHolder;
import com.zhpan.bannerview.BannerViewPager;
import com.zhpan.bannerview.constants.IndicatorSlideMode;

import java.util.ArrayList;
import java.util.List;

public class PhotoViewActivity extends AppCompatActivity {
private List<Integer> mDrawableList = new ArrayList<>();
public class PhotoViewActivity extends BaseDataActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_banner_photo_view);
setTitle(R.string.wrapper_photo_view);
initData();
initViewPager();
}

Expand All @@ -32,11 +27,4 @@ private void initViewPager() {
.create(mDrawableList);
bannerViewPager.setCurrentItem(1);
}

private void initData() {
for (int i = 0; i <= 3; i++) {
int drawable2 = getResources().getIdentifier("c" + i, "drawable", getPackageName());
mDrawableList.add(drawable2);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,81 +1,112 @@
package com.example.zhpan.circleviewpager.activity;


import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zhpan.circleviewpager.R;
import com.example.zhpan.circleviewpager.bean.CustomBean;
import com.example.zhpan.circleviewpager.viewholder.CustomPageViewHolder;
import com.zhpan.bannerview.BannerViewPager;
import com.zhpan.bannerview.adapter.OnPageChangeListenerAdapter;
import com.zhpan.bannerview.constants.IndicatorSlideMode;
import com.zhpan.bannerview.constants.TransformerStyle;
import com.zhpan.bannerview.indicator.CircleIndicatorView;
import com.zhpan.bannerview.indicator.IIndicator;
import com.zhpan.bannerview.holder.HolderCreator;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class WelcomeActivity extends AppCompatActivity {
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WelcomeActivity extends BaseDataActivity implements
HolderCreator<CustomPageViewHolder> {

private BannerViewPager<CustomBean, CustomPageViewHolder> mViewPager;
private int[] imgRes = {R.drawable.guide0, R.drawable.guide1, R.drawable.guide2};

private String[] des = {"在这里\n你可以听到周围人的心声", "在这里\nTA会在下一秒遇见你", "在这里\n不再错过可以改变你一生的人"};

private int[] transforms = {TransformerStyle.NONE, TransformerStyle.ACCORDION, TransformerStyle.STACK, TransformerStyle.DEPTH, TransformerStyle.ROTATE};

@BindView(R.id.btn_start)
TextView mTvStart;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
getData();
ButterKnife.bind(this);
setupViewPager();
}

@Override
protected void onDestroy() {
super.onDestroy();
mViewPager.stopLoop();
super.onDestroy();
}

private void setupViewPager() {
mViewPager = findViewById(R.id.viewpager);
mViewPager.setAutoPlay(false)
.setCanLoop(false)
.setPageTransformerStyle(transforms[new Random().nextInt(4)])
.setPageTransformerStyle(transforms[new Random().nextInt(5)])
.setIndicatorVisibility(View.GONE)
.setIndicatorView(getIndicatorView())
.setHolderCreator(() -> {
CustomPageViewHolder customPageViewHolder = new CustomPageViewHolder();
customPageViewHolder.setOnSubViewClickListener((view, position) -> {
MainActivity.start(WelcomeActivity.this);
finish();
});
return customPageViewHolder;
}).create(getData());
.setIndicatorView(findViewById(R.id.indicator))
.setIndicatorGap((int) getResources().getDimension(R.dimen.dp_10))
.setIndicatorColor(getResources().getColor(R.color.white),
getResources().getColor(R.color.white_alpha_75))
.setIndicatorSlideMode(IndicatorSlideMode.SMOOTH)
.setIndicatorRadius((int) getResources().getDimension(R.dimen.dp_3), (int) getResources().getDimension(R.dimen.dp_4_5))
.setOnPageChangeListener(new OnPageChangeListenerAdapter() {
@Override
public void onPageSelected(int position) {
showStartButton(position);
}
})
.setHolderCreator(this)
.create(getData());
}

private IIndicator getIndicatorView() {
CircleIndicatorView indicatorView = findViewById(R.id.indicator);
indicatorView.setNormalColor(getResources().getColor(R.color.white));
indicatorView.setCheckedColor(getResources().getColor(R.color.white_alpha_75));
indicatorView.setIndicatorWidth((int) getResources().getDimension(R.dimen.dp_6),
(int) getResources().getDimension(R.dimen.dp_9));
indicatorView.setIndicatorGap((int) getResources().getDimension(R.dimen.dp_10));
return indicatorView;
@OnClick(R.id.btn_start)
public void onClick(View view) {
MainActivity.start(WelcomeActivity.this);
finish();
}

private void showStartButton(int position) {
if (position == mViewPager.getList().size() - 1 && mTvStart.getVisibility() == View.GONE) {
mTvStart.setVisibility(View.VISIBLE);
ObjectAnimator
.ofFloat(mTvStart, "alpha", 0, 1)
.setDuration(1500).start();
} else {
mTvStart.setVisibility(View.GONE);
}
}

private List<CustomBean> getData() {
List<CustomBean> list = new ArrayList<>();
for (int i = 0; i < imgRes.length; i++) {
for (int i = 0; i < mDrawableList.size(); i++) {
CustomBean customBean = new CustomBean();
customBean.setImageRes(imgRes[i]);
customBean.setImageRes(mDrawableList.get(i));
customBean.setImageDescription(des[i]);
list.add(customBean);
}
return list;
}

@Override
public CustomPageViewHolder createViewHolder() {
CustomPageViewHolder customPageViewHolder = new CustomPageViewHolder();
customPageViewHolder.setOnSubViewClickListener((view, position) ->
Toast.makeText(WelcomeActivity.this, "Logo Clicked,Item: " + position, Toast.LENGTH_SHORT).show());
return customPageViewHolder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@


import com.example.zhpan.circleviewpager.fragment.BaseFragment;
import com.example.zhpan.circleviewpager.fragment.FindFragment;
import com.example.zhpan.circleviewpager.fragment.IndicatorFragment;
import com.example.zhpan.circleviewpager.fragment.PageFragment;
import com.example.zhpan.circleviewpager.fragment.HomeFragment;
import com.example.zhpan.circleviewpager.fragment.OthersFragment;

import java.util.List;

public class AdapterFragmentPager extends FragmentPagerAdapter {

Expand All @@ -34,8 +32,8 @@ public AdapterFragmentPager(FragmentManager fm) {
private SparseArray<BaseFragment> getFragments() {
SparseArray<BaseFragment> fragmentList = new SparseArray<>();
fragmentList.put(PAGE_HOME, HomeFragment.getInstance());
fragmentList.put(PAGE_FIND, FindFragment.getInstance());
fragmentList.put(PAGE_OTHERS, OthersFragment.getInstance());
fragmentList.put(PAGE_FIND, PageFragment.getInstance());
fragmentList.put(PAGE_OTHERS, IndicatorFragment.getInstance());
return fragmentList;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,17 @@ public class DataWrapper {

private List<BannerData> dataBeanList;

public List<BannerData> getDataBeanList() {
return dataBeanList;
}

public void setDataBeanList(List<BannerData> dataBeanList) {
public DataWrapper(List<ArticleWrapper.Article> articleList, List<BannerData> dataBeanList) {
this.articleList = articleList;
this.dataBeanList = dataBeanList;
}

public List<BannerData> getDataBeanList() {
return dataBeanList;
}

public List<ArticleWrapper.Article> getArticleList() {
return articleList;
}

public void setArticleList(List<ArticleWrapper.Article> articleList) {
this.articleList = articleList;
}


}
Loading

0 comments on commit 454ee03

Please sign in to comment.