Skip to content

Commit

Permalink
update 刷新banner角标会回到0,更名为SBannerViewHolder
Browse files Browse the repository at this point in the history
  • Loading branch information
youlookwhat committed May 25, 2020
1 parent fa252dd commit 481e426
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 51 deletions.
80 changes: 52 additions & 28 deletions app/src/main/java/me/jingbin/sbannerview/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.os.CountDownTimer;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatButton;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
Expand All @@ -16,10 +17,9 @@
import java.util.List;

import me.jingbin.sbanner.SBannerView;
import me.jingbin.sbanner.config.BannerConfig;
import me.jingbin.sbanner.config.OnBannerClickListener;
import me.jingbin.sbanner.config.ScaleRightTransformer;
import me.jingbin.sbanner.holder.BannerViewHolder;
import me.jingbin.sbanner.holder.SBannerViewHolder;
import me.jingbin.sbanner.holder.HolderCreator;

/**
Expand All @@ -41,32 +41,21 @@ protected void onCreate(Bundle savedInstanceState) {
banner = findViewById(R.id.banner);
banner2 = findViewById(R.id.banner2);

final List<BannerItemBean> list = new ArrayList<>();
for (int i = 0; i < 3; i++) {
BannerItemBean itemBean = new BannerItemBean();
itemBean.setTitle("药妆店必BUY扫货指南-" + i);
// list.add("药妆店必BUY扫货指南-" + i);
long applyEndTime = 1555671600;
if (i == 0) {
applyEndTime = 1555671600;
} else if (i == 1) {
applyEndTime = 1555689600;
} else if (i == 2) {
applyEndTime = 1556035200;
}
itemBean.setApplyEndTime(applyEndTime);
list.add(itemBean);
}
banner
.setPageRightMargin(dip2px(this, 59))
final List<BannerItemBean> list = getList(4);
setBannerView(list);
setBanner2View(list);
}

private void setBannerView(final List<BannerItemBean> list) {
banner.setPageRightMargin(dip2px(this, 59))
// .setAutoPlay(true)
// .setBannerStyle(BannerConfig.NOT_INDICATOR)
.setBannerAnimation(ScaleRightTransformer.class)
.setOffscreenPageLimit(list.size())
.setDelayTime(3000)
.setPages(list, new HolderCreator<BannerViewHolder>() {
.setPages(list, new HolderCreator<SBannerViewHolder>() {
@Override
public BannerViewHolder createViewHolder() {
public SBannerViewHolder createViewHolder() {
return new CustomViewHolder();
}
})
Expand All @@ -93,16 +82,18 @@ public void onPageScrollStateChanged(int state) {

}
});
}

private void setBanner2View(final List<BannerItemBean> list) {
banner2
// .setAutoPlay(true)
// .setBannerStyle(BannerConfig.NOT_INDICATOR)
.setBannerAnimation(ScaleRightTransformer.class)
.setOffscreenPageLimit(list.size())
.setDelayTime(3000)
.setPages(list, new HolderCreator<BannerViewHolder>() {
.setPages(list, new HolderCreator<SBannerViewHolder>() {
@Override
public BannerViewHolder createViewHolder() {
public SBannerViewHolder createViewHolder() {
return new CustomViewHolder2();
}
})
Expand All @@ -115,19 +106,21 @@ public void onBannerClick(int position) {
});
}

class CustomViewHolder implements BannerViewHolder<BannerItemBean> {
class CustomViewHolder implements SBannerViewHolder<BannerItemBean> {

private TextView mTextView;
private TextView tvDay;
private TextView tvHour;
private TextView tvMin;
private TextView tvMiao;
private AppCompatButton btRefresh;
CountDownTimer countDownTimer;

@Override
public View createView(Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.banner_item, null);
mTextView = (TextView) view.findViewById(R.id.text);
btRefresh = (AppCompatButton) view.findViewById(R.id.bt_refresh);
tvDay = (TextView) view.findViewById(R.id.tv_day);
tvHour = (TextView) view.findViewById(R.id.tv_hour);
tvMin = (TextView) view.findViewById(R.id.tv_min);
Expand All @@ -137,6 +130,19 @@ public View createView(Context context) {

@Override
public void onBind(Context context, int position, BannerItemBean data) {
if (position == 3) {
btRefresh.setText("刷新");
btRefresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setBannerView(getList(2));
setBanner2View(getList(2));
}
});
} else {
btRefresh.setText("立即申请");
btRefresh.setOnClickListener(null);
}
// 数据绑定
mTextView.setText(String.format("%d:%s", position, data.getTitle()));

Expand Down Expand Up @@ -174,12 +180,11 @@ public void onFinish() {
}
}

class CustomViewHolder2 implements BannerViewHolder<BannerItemBean> {
static class CustomViewHolder2 implements SBannerViewHolder<BannerItemBean> {

@Override
public View createView(Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.item_banner_two, null);
return view;
return LayoutInflater.from(context).inflate(R.layout.item_banner_two, null);
}

@Override
Expand Down Expand Up @@ -211,6 +216,25 @@ public static int dip2px(Context context, float dpValue) {
return (int) (dpValue * scale + 0.5f);
}

private List<BannerItemBean> getList(int size) {
List<BannerItemBean> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
BannerItemBean itemBean = new BannerItemBean();
itemBean.setTitle("我是标题-" + i);
long applyEndTime;
if (i == 0) {
applyEndTime = System.currentTimeMillis() / 1000 + 6000;
} else if (i == 1) {
applyEndTime = System.currentTimeMillis() / 1000 + 5000;
} else {
applyEndTime = System.currentTimeMillis() / 1000 + 4000;
}
itemBean.setApplyEndTime(applyEndTime);
list.add(itemBean);
}
return list;
}

@Override
protected void onPause() {
super.onPause();
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<me.jingbin.sbanner.SBannerView
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="370dp"
android:layout_height="330dp"
app:indicator_height="6dp"
app:indicator_margin="15dp"
app:indicator_padding="6dp"
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/res/layout/banner_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="30dp"
android:layout_marginBottom="20dp"
android:elevation="4dp">

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
Expand Down Expand Up @@ -125,6 +125,7 @@
</LinearLayout>

<android.support.v7.widget.AppCompatButton
android:id="@+id/bt_refresh"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.1'
classpath 'com.android.tools.build:gradle:3.4.2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
23 changes: 7 additions & 16 deletions sbanner/src/main/java/me/jingbin/sbanner/SBannerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
Expand All @@ -30,7 +29,7 @@
import me.jingbin.sbanner.config.BannerViewPager;
import me.jingbin.sbanner.config.OnBannerClickListener;
import me.jingbin.sbanner.config.WeakHandler;
import me.jingbin.sbanner.holder.BannerViewHolder;
import me.jingbin.sbanner.holder.SBannerViewHolder;
import me.jingbin.sbanner.holder.HolderCreator;

import static android.support.v4.view.ViewPager.OnPageChangeListener;
Expand Down Expand Up @@ -60,7 +59,7 @@ public class SBannerView extends FrameLayout implements OnPageChangeListener {
private int count = 0;
private int gravity = -1;
private List mDatas;
private HolderCreator<BannerViewHolder> creator;
private HolderCreator<SBannerViewHolder> creator;
private List<ImageView> indicatorImages;
private Context context;
private BannerViewPager viewPager;
Expand Down Expand Up @@ -245,7 +244,7 @@ public SBannerView setViewPagerIsScroll(boolean isScroll) {
return this;
}

public SBannerView setPages(List<?> datas, HolderCreator<BannerViewHolder> creator) {
public SBannerView setPages(List<?> datas, HolderCreator<SBannerViewHolder> creator) {
this.mDatas = datas;
this.creator = creator;
this.count = datas.size();
Expand Down Expand Up @@ -367,15 +366,10 @@ private void setData() {
}
} else {
if (isLoop) {
//currentItem = 1;
if (currentItem == -1) {
currentItem = NUM / 2 - ((NUM / 2) % count) + 1;
}
currentItem = NUM / 2 - ((NUM / 2) % count) + 1;
lastPosition = 1;
} else {
if (currentItem == -1) {
currentItem = 0;
}
currentItem = 0;
lastPosition = 0;
}
}
Expand All @@ -384,7 +378,6 @@ private void setData() {
viewPager.addOnPageChangeListener(this);
}
viewPager.setAdapter(adapter);
Log.e("currentItem", currentItem + "");
viewPager.setCurrentItem(currentItem);
viewPager.setOffscreenPageLimit(count);
if (isScroll && count > 1) {
Expand Down Expand Up @@ -444,11 +437,9 @@ public void run() {
if (isLoop) {
// 最后一个 向前滑
if (currentItem == adapter.getCount()) {
// Log.e("currentItem1", currentItem + "");
viewPager.setCurrentItem(currentItem);
handler.post(task);
} else {
// Log.e("currentItem2", currentItem + "");
viewPager.setCurrentItem(currentItem);
handler.postDelayed(task, delayTime);
}
Expand Down Expand Up @@ -548,15 +539,15 @@ public Object instantiateItem(ViewGroup container, final int position) {
if (creator == null) {
throw new RuntimeException("[Banner] --> The layout is not specified,请指定 holder");
}
BannerViewHolder holder;
SBannerViewHolder holder;
View view;
if (mViewCache.size() == 0) {
holder = creator.createViewHolder();
view = holder.createView(container.getContext());
view.setTag(holder);
} else {
view = mViewCache.removeFirst();
holder = (BannerViewHolder) view.getTag();
holder = (SBannerViewHolder) view.getTag();
}

if (mDatas != null && mDatas.size() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* 需要 --
* 可以 --
*/
public interface HolderCreator<VH extends BannerViewHolder> {
public interface HolderCreator<VH extends SBannerViewHolder> {

/**
* 创建ViewHolder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.content.Context;
import android.view.View;

public interface BannerViewHolder<T> {
public interface SBannerViewHolder<T> {

/**
* 创建View
Expand Down

0 comments on commit 481e426

Please sign in to comment.