Skip to content

Commit

Permalink
修复空数据时,轮询任务未结束
Browse files Browse the repository at this point in the history
修复频繁刷新时,崩溃BUG
  • Loading branch information
clearloveforyou committed Aug 26, 2019
1 parent 3c5eb92 commit 5e58766
Show file tree
Hide file tree
Showing 11 changed files with 740 additions and 24 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ dependencies {
implementation 'com.github.iwgang:countdownview:2.1.6'
implementation 'cn.jzvd:jiaozivideoplayer:7.0.5'
implementation "com.hanks:htextview-rainbow:0.1.6"
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-28'
implementation project(':banner')
// implementation 'com.github.AlpsDog:Banner:v1.0.1'
// implementation 'com.github.AlpsDog:Banner:v1.0.2'
}
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".banner.BannerMzActivity"></activity>
<activity android:name=".banner.BannerDataActivity"></activity>
<activity android:name=".banner.BannerMzActivity" />
<activity android:name=".banner.BannerIndicatorActivity" />
<activity android:name=".banner.BannerViewActivity" />
<activity android:name=".banner.ShowAnimActivity" />
Expand Down
13 changes: 12 additions & 1 deletion app/src/main/java/com/test/banner/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import com.test.banner.banner.BannerDataActivity;
import com.test.banner.banner.BannerIndicatorActivity;
import com.test.banner.banner.BannerMzActivity;
import com.test.banner.banner.BannerViewActivity;
Expand All @@ -24,6 +25,8 @@ public class MainActivity extends AppCompatActivity {

@BindView(R.id.banner_base_use)
TextView bannerBaseUse;
@BindView(R.id.banner_empty_use)
TextView bannerEmptyUse;
@BindView(R.id.banner_mz_use)
TextView bannerMzUse;
@BindView(R.id.banner_anim_show)
Expand All @@ -48,11 +51,19 @@ public void onBaseUseClicked() {
BaseUseActivity.start(this);
}

/**
* 空数据、刷新
*/
@OnClick(R.id.banner_empty_use)
public void onBannerEmptyUseClicked() {
BannerDataActivity.start(this);
}

/**
* 仿魅族Banner
*/
@OnClick(R.id.banner_mz_use)
public void onViewClicked() {
public void onBannerMzClicked() {
BannerMzActivity.start(this);
}

Expand Down
37 changes: 37 additions & 0 deletions app/src/main/java/com/test/banner/application/AppContext.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package com.test.banner.application;

import android.app.Application;
import android.content.Context;
import android.support.annotation.NonNull;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshInitializer;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

/**
* @Author: HSL
Expand All @@ -21,6 +30,34 @@ public static AppContext getInstance() {
return instance;
}

static {
////刷新控件配置
////刷新控件配置
//设置全局默认配置(优先级最低,会被其他设置覆盖)
SmartRefreshLayout.setDefaultRefreshInitializer(new DefaultRefreshInitializer() {
@Override
public void initialize(@NonNull Context context, @NonNull RefreshLayout layout) {
//全局设置(优先级最低)
layout.setEnableLoadMore(false);
layout.setEnableAutoLoadMore(false);
layout.setEnableOverScrollDrag(false);
layout.setEnableOverScrollBounce(true);
//全局设置主题颜色
// layout.setPrimaryColorsId(R.color.c_fdfdfd, R.color.c_333333);
}
});
//设置全局头部
SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
@NonNull
@Override
public RefreshHeader createRefreshHeader(@NonNull Context context, @NonNull RefreshLayout layout) {
ClassicsHeader classicsHeader = new ClassicsHeader(context);
classicsHeader.setTextSizeTitle(14);
return classicsHeader;
}
});
}

@Override
public void onCreate() {
super.onCreate();
Expand Down
157 changes: 157 additions & 0 deletions app/src/main/java/com/test/banner/banner/BannerDataActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package com.test.banner.banner;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.geek.banner.Banner;
import com.geek.banner.loader.BannerEntry;
import com.geek.banner.loader.ImageLoader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.test.banner.R;
import com.test.banner.bean.BannerItem;
import com.test.banner.utils.DateUtils;

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

import butterknife.BindView;
import butterknife.ButterKnife;

/**
* @Author: HSL
* @Time: 2019/8/23 15:35
* @E-mail: xxx@163.com
* @Description: 数据展示~
*/
public class BannerDataActivity extends AppCompatActivity {

@BindView(R.id.pager_tv)
TextView pagerTv;
@BindView(R.id.banner)
Banner banner;
@BindView(R.id.pager_tv_2)
TextView pagerTv2;
@BindView(R.id.banner_2)
Banner banner2;
@BindView(R.id.pager_tv_3)
TextView pagerTv3;
@BindView(R.id.banner_mz)
Banner bannerMz;
@BindView(R.id.refresh_srl)
SmartRefreshLayout refreshSrl;

private List<BannerItem> mData = new ArrayList<>();

public static void start(Context context) {
Intent starter = new Intent(context, BannerDataActivity.class);
context.startActivity(starter);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_banner_data);
ButterKnife.bind(this);
initData();
initBanner(banner);
initBanner(banner2);
initBanner(bannerMz);
refreshSrl.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
refreshSrl.finishRefresh();
int max = new Random().nextInt(5);
List<BannerItem> data = new ArrayList<>();
for (int i = 0; i < mData.size(); i++) {
if (i > max || max == 0) break;
data.add(mData.get(i));
}
pagerTv.setText(data.size() + "张");
banner.loadImagePaths(data);
if (max < 2) {
banner2.loadImagePaths(data);
pagerTv2.setText(data.size() + "张");
} else {
bannerMz.loadImagePaths(data);
pagerTv3.setText(data.size() + "张");
}
}
});
}

private void initData() {
mData.add(new BannerItem(R.drawable.banner_1, ""));
mData.add(new BannerItem(R.drawable.banner_2, ""));
mData.add(new BannerItem(R.drawable.banner_3, ""));
mData.add(new BannerItem(R.drawable.banner_4, ""));
mData.add(new BannerItem(R.drawable.banner_5, ""));
pagerTv.setText("5张");
pagerTv2.setText("5张");
pagerTv3.setText("5张");
}

private void initBanner(Banner banner) {
// 1. 创建设置BannerLoader
banner.setBannerLoader(new ImageLoader() {
@Override
public void loadView(Context context, BannerEntry entry, int position, View imageView) {
RequestOptions requestOptions = new RequestOptions()
.placeholder(R.drawable.banner_default)
.error(R.drawable.banner_default)
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
Glide.with(context).load(entry.getBannerPath()).apply(requestOptions).into((ImageView) imageView);
}
});

// 2 设置页面点击事件
banner.setOnBannerClickListener(new Banner.OnBannerClickListener() {
@Override
public void onBannerClick(int position) {
Toast.makeText(BannerDataActivity.this, "点击了:" + position, Toast.LENGTH_SHORT).show();
}
});

// 3 翻页事件
banner.setBannerPagerChangedListener(new Banner.OnBannerSimplePagerListener() {
@Override
public void onPageSelected(int position) {
Log.d("hsl777", "onPageSelected: ==>"
+ position
+ "\n" + banner);
}
});

// 4 最重要一步,加载数据
banner.loadImagePaths(mData);
}

@Override
protected void onResume() {
super.onResume();
banner.startAutoPlay();
banner2.startAutoPlay();
bannerMz.startAutoPlay();
}

@Override
protected void onPause() {
super.onPause();
banner.stopAutoPlay();
banner2.stopAutoPlay();
bannerMz.stopAutoPlay();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
protected void onResume() {
super.onResume();
bannerMz.startAutoPlay();
}

@Override
protected void onPause() {
super.onPause();
bannerMz.stopAutoPlay();
}

private void initData() {
Expand Down
Loading

0 comments on commit 5e58766

Please sign in to comment.