Skip to content

Commit

Permalink
2.5.1 (#44)
Browse files Browse the repository at this point in the history
* Fix bug #41

* Optimized Code For Issue #42

* Update README
  • Loading branch information
zhpanvip authored Nov 26, 2019
1 parent f258a13 commit 8839ba6
Show file tree
Hide file tree
Showing 16 changed files with 111 additions and 165 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
[![JitPack](https://jitpack.io/v/zhpanvip/BannerViewPager.svg)](https://jitpack.io/#zhpanvip/BannerViewPager)
[ ![JCenter](https://api.bintray.com/packages/zhpanvip/CircleViewPager/bannerview/images/download.svg) ](https://bintray.com/zhpanvip/CircleViewPager/bannerview/_latestVersion)


> 腾讯视频、QQ音乐、酷狗音乐、支付宝、天猫、淘宝、优酷视频、喜马拉雅、网易云音乐、哔哩哔哩、全民K歌等App的Banner样式都可以通过BannerViewPager实现哦!

## 效果预览

### [点击或扫描二维码下载apk](https://github.com/zhpanvip/BannerViewPager/raw/master/download/app.apk)
Expand Down Expand Up @@ -384,14 +388,19 @@ public class FigureIndicatorView extends BaseIndicatorView {
- [x] 目前Indicator部分代码比较乱,还有很大很大的优化空间,后续版本将持续优化(2.5.0对Indicator再次进行了重构,重构后代码已经很整洁,但仍然有优化空间)
- [ ] ViewPager更换为ViewPager2 (3.0.0)



## 有问题可以扫码加QQ群交流

![QQ交流群60902509](https://github.com/zhpanvip/BannerViewPager/blob/master/image/qq_group.png)


## 更多详情请参看以下链接

[《打造一个丝滑般自动轮播无限循环Android库》](https://juejin.im/post/5d6bce24f265da03db0790d1)

[《BannerViewPager源码解析》](https://juejin.im/post/5d74d3faf265da03b5747015)

[《剖析BannerViewPager中Indicator的设计思想》](https://juejin.im/post/5dda0b6d518825731f569a8c)

## 感谢

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ 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.IndicatorStyle
import com.zhpan.bannerview.constants.TransformerStyle
import com.zhpan.bannerview.holder.HolderCreator
import com.zhpan.bannerview.utils.BannerUtils
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ abstract class BaseFragment : RxFragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(layout, container, false)
initData()
initData(3)
initTitle()
initView(savedInstanceState, view)
return view
}

private fun initData() {
for (i in 0..3) {
protected fun initData(j: Int) {
mDrawableList.clear()
for (i in 0..j) {
val drawable = resources.getIdentifier("t$i", "drawable", mContext.packageName)
mDrawableList.add(drawable)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package com.example.zhpan.circleviewpager.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.zhpan.circleviewpager.R;
import com.example.zhpan.circleviewpager.viewholder.ImageResourceViewHolder;
import com.zhpan.bannerview.BannerViewPager;
Expand All @@ -19,6 +14,8 @@
import com.zhpan.bannerview.utils.BannerUtils;
import com.zhpan.idea.utils.ToastUtils;

import java.util.Random;

/**
* Created by zhpan on 2018/7/24.
*/
Expand Down Expand Up @@ -53,6 +50,15 @@ protected void initView(Bundle savedInstanceState, View view) {
.setOnPageClickListener(position -> ToastUtils.show("position:" + position))
.setInterval(5000);
initRadioGroup();

view.findViewById(R.id.btn_refresh).setOnClickListener(v -> updateData());
}

private void updateData() {
// 生成[-1,3]整数
initData(new Random().nextInt(5) - 1);
ToastUtils.show("size=" + getMDrawableList().size());
mViewPager.create(getMDrawableList());
}

public static PageFragment getInstance() {
Expand Down Expand Up @@ -84,6 +90,10 @@ private void initRadioGroup() {
// mViewPager.resetIndicator();
setupOverlapBanner();
break;
case R.id.rb_multi_page_wy:
indicatorView.setVisibility(View.INVISIBLE);
setNetEaseMusicStyle();
break;
}
});
radioButton.performClick();
Expand All @@ -96,4 +106,16 @@ private void setupOverlapBanner() {
.setIndicatorView(indicatorView)
.create(getMDrawableList());
}

// 网易云音乐、QQ音乐类似的Banner都可以通过设置不同的pargeMargin和revealWidth来实现
private void setNetEaseMusicStyle() {
mViewPager
.setPageMargin(BannerUtils.dp2px(30))
.setRevealWidth(BannerUtils.dp2px(-15))
.setPageStyle(PageStyle.MULTI_PAGE)
.setHolderCreator(() -> new ImageResourceViewHolder(BannerUtils.dp2px(5)))
.setIndicatorColor(getColor(R.color.red_normal_color), getColor(R.color.red_checked_color))
.setOnPageClickListener(position -> ToastUtils.show("position:" + position))
.setInterval(5000).create(getMDrawableList());
}
}
9 changes: 0 additions & 9 deletions app/src/main/res/layout/activity_custom_indicator.xml

This file was deleted.

49 changes: 0 additions & 49 deletions app/src/main/res/layout/activity_indicator_slide_mode.xml

This file was deleted.

54 changes: 0 additions & 54 deletions app/src/main/res/layout/activity_page_style.xml

This file was deleted.

17 changes: 16 additions & 1 deletion app/src/main/res/layout/fragment_find.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/indicator_view"
android:layout_marginStart="10dp"
android:text="@string/text_page_style"
android:textColor="#000000"
android:layout_marginStart="10dp"
android:textSize="18sp"
android:textStyle="bold" />

Expand Down Expand Up @@ -58,6 +58,21 @@
android:layout_marginTop="15dp"
android:text="MULTI_PAGE_OVERLAP" />

<RadioButton
android:id="@+id/rb_multi_page_wy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="仿网易云(QQ)音乐Banner" />

</RadioGroup>

<Button
android:id="@+id/btn_refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Refresh Data" />

</RelativeLayout>
50 changes: 23 additions & 27 deletions bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ private void setTouchListener() {

@Override
public void onPageSelected(int position) {
// Optimized For Issue #42
if (mList.size() > 0 && isCanLoop() && position == 0) {
position = MAX_VALUE / 2 - ((MAX_VALUE / 2) % mList.size()) + 1;
setCurrentItem(BannerUtils.getRealPosition(isCanLoop(), position, mList.size()));
}
currentPosition = BannerUtils.getRealPosition(isCanLoop(), position, mList.size());
if (mOnPageChangeListener != null)
mOnPageChangeListener.onPageSelected(currentPosition);
Expand Down Expand Up @@ -167,37 +172,29 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse
private void handlePosition() {
if (mList.size() > 1) {
currentPosition = mViewPager.getCurrentItem() + 1;
if (isCanLoop()) {
if (currentPosition == MAX_VALUE - 1) {
currentPosition = 0;
mViewPager.setCurrentItem(currentPosition, false);
mHandler.post(mRunnable);
} else {
mViewPager.setCurrentItem(currentPosition);
mHandler.postDelayed(mRunnable, getInterval());
}
} else {
if (currentPosition >= MAX_VALUE) {
stopLoop();
} else {
mViewPager.setCurrentItem(currentPosition);
mHandler.postDelayed(mRunnable, getInterval());
}
}
mViewPager.setCurrentItem(currentPosition);
mHandler.postDelayed(mRunnable, getInterval());
}
}

private void initBannerData(List<T> list) {
if (list != null) {
mList.clear();
mList.addAll(list);
if (mList.size() > 0) {
if (mList.size() > 1) setIndicatorValues();
if (isCanLoop())
currentPosition = MAX_VALUE / 2 - ((MAX_VALUE / 2) % mList.size()) + 1;
setupViewPager();
initRoundCorner();
}
initList(list);
setupViewPager();
initRoundCorner();
}
}

private void initList(List<T> list) {
mList.clear();
mList.addAll(list);
if (mList.size() > 1) {
setIndicatorValues();
} else if (mIndicatorView != null) {
mIndicatorView.setPageSize(mList.size());
}
if (mList.size() > 0 && isCanLoop()) {
currentPosition = MAX_VALUE / 2 - ((MAX_VALUE / 2) % mList.size()) + 1;
}
}

Expand All @@ -206,7 +203,6 @@ private void setIndicatorValues() {
if (bannerOptions.isCustomIndicator() && null != mIndicatorView) {
initIndicator(mIndicatorView);
} else {
// initIndicator(IndicatorFactory.createIndicatorView(getContext(), bannerOptions.getIndicatorStyle()));
initIndicator(new IndicatorView(getContext()));
}
mIndicatorView.setIndicatorOptions(bannerOptions.getIndicatorOptions());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public abstract class BaseDrawer implements IDrawer {

public class MeasureResult {

public int measureWidth;
int measureWidth;

public int measureHeight;
int measureHeight;

public void setMeasureResult(int measureWidth, int measureHeight) {
void setMeasureResult(int measureWidth, int measureHeight) {
this.measureWidth = measureWidth;
this.measureHeight = measureHeight;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class CircleDrawer extends BaseDrawer {

private float minWidth;

public CircleDrawer(IndicatorOptions indicatorOptions) {
CircleDrawer(IndicatorOptions indicatorOptions) {
super(indicatorOptions);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class DashDrawer extends BaseDrawer {

private float minWidth;

public DashDrawer(IndicatorOptions indicatorOptions) {
DashDrawer(IndicatorOptions indicatorOptions) {
super(indicatorOptions);
}

Expand Down
Loading

0 comments on commit 8839ba6

Please sign in to comment.