diff --git a/app/src/main/java/me/jingbin/sbannerview/MainActivity.java b/app/src/main/java/me/jingbin/sbannerview/MainActivity.java index eb1500c..dc4c8a7 100644 --- a/app/src/main/java/me/jingbin/sbannerview/MainActivity.java +++ b/app/src/main/java/me/jingbin/sbannerview/MainActivity.java @@ -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; @@ -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; /** @@ -41,32 +41,21 @@ protected void onCreate(Bundle savedInstanceState) { banner = findViewById(R.id.banner); banner2 = findViewById(R.id.banner2); - final List 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 list = getList(4); + setBannerView(list); + setBanner2View(list); + } + + private void setBannerView(final List list) { + banner.setPageRightMargin(dip2px(this, 59)) // .setAutoPlay(true) // .setBannerStyle(BannerConfig.NOT_INDICATOR) .setBannerAnimation(ScaleRightTransformer.class) .setOffscreenPageLimit(list.size()) .setDelayTime(3000) - .setPages(list, new HolderCreator() { + .setPages(list, new HolderCreator() { @Override - public BannerViewHolder createViewHolder() { + public SBannerViewHolder createViewHolder() { return new CustomViewHolder(); } }) @@ -93,16 +82,18 @@ public void onPageScrollStateChanged(int state) { } }); + } + private void setBanner2View(final List list) { banner2 // .setAutoPlay(true) // .setBannerStyle(BannerConfig.NOT_INDICATOR) .setBannerAnimation(ScaleRightTransformer.class) .setOffscreenPageLimit(list.size()) .setDelayTime(3000) - .setPages(list, new HolderCreator() { + .setPages(list, new HolderCreator() { @Override - public BannerViewHolder createViewHolder() { + public SBannerViewHolder createViewHolder() { return new CustomViewHolder2(); } }) @@ -115,19 +106,21 @@ public void onBannerClick(int position) { }); } - class CustomViewHolder implements BannerViewHolder { + class CustomViewHolder implements SBannerViewHolder { 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); @@ -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())); @@ -174,12 +180,11 @@ public void onFinish() { } } - class CustomViewHolder2 implements BannerViewHolder { + static class CustomViewHolder2 implements SBannerViewHolder { @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 @@ -211,6 +216,25 @@ public static int dip2px(Context context, float dpValue) { return (int) (dpValue * scale + 0.5f); } + private List getList(int size) { + List 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(); diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 98a0ead..00061e1 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -11,7 +11,7 @@ creator; + private HolderCreator creator; private List indicatorImages; private Context context; private BannerViewPager viewPager; @@ -245,7 +244,7 @@ public SBannerView setViewPagerIsScroll(boolean isScroll) { return this; } - public SBannerView setPages(List datas, HolderCreator creator) { + public SBannerView setPages(List datas, HolderCreator creator) { this.mDatas = datas; this.creator = creator; this.count = datas.size(); @@ -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; } } @@ -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) { @@ -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); } @@ -548,7 +539,7 @@ 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(); @@ -556,7 +547,7 @@ public Object instantiateItem(ViewGroup container, final int position) { view.setTag(holder); } else { view = mViewCache.removeFirst(); - holder = (BannerViewHolder) view.getTag(); + holder = (SBannerViewHolder) view.getTag(); } if (mDatas != null && mDatas.size() > 0) { diff --git a/sbanner/src/main/java/me/jingbin/sbanner/holder/HolderCreator.java b/sbanner/src/main/java/me/jingbin/sbanner/holder/HolderCreator.java index 7db04b0..b18a3e9 100755 --- a/sbanner/src/main/java/me/jingbin/sbanner/holder/HolderCreator.java +++ b/sbanner/src/main/java/me/jingbin/sbanner/holder/HolderCreator.java @@ -8,7 +8,7 @@ * 需要 -- * 可以 -- */ -public interface HolderCreator { +public interface HolderCreator { /** * 创建ViewHolder diff --git a/sbanner/src/main/java/me/jingbin/sbanner/holder/BannerViewHolder.java b/sbanner/src/main/java/me/jingbin/sbanner/holder/SBannerViewHolder.java similarity index 86% rename from sbanner/src/main/java/me/jingbin/sbanner/holder/BannerViewHolder.java rename to sbanner/src/main/java/me/jingbin/sbanner/holder/SBannerViewHolder.java index 5c5c66d..4846256 100755 --- a/sbanner/src/main/java/me/jingbin/sbanner/holder/BannerViewHolder.java +++ b/sbanner/src/main/java/me/jingbin/sbanner/holder/SBannerViewHolder.java @@ -3,7 +3,7 @@ import android.content.Context; import android.view.View; -public interface BannerViewHolder { +public interface SBannerViewHolder { /** * 创建View