From c94d262b4b06d725a62448e775b1802b831b3398 Mon Sep 17 00:00:00 2001 From: laishujie Date: Thu, 28 Jun 2018 14:30:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=A2=84=E8=A7=88=E9=A1=B5?= =?UTF-8?q?=EF=BC=8C=E4=BC=98=E5=8C=96=E7=9B=B8=E5=85=B3=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E7=AD=89.....?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 51 +++- app/objectbox-models/default.json.bak | 10 +- .../main/java/com/lai/mtc/MainActivity.java | 6 +- .../java/com/lai/mtc/TestMainActivity.java | 12 +- .../com/lai/mtc/bean/ComicListDetail.java | 14 +- .../main/java/com/lai/mtc/comm/Parameter.java | 14 + .../mtc/comm/widget/MyPagerSnapHelper.java | 74 ------ .../java/com/lai/mtc/di/module/NetModule.java | 4 +- .../mvp/contract/ComicsPreviewContract.java | 4 +- .../presenter/ComicsListDetailPresenter.java | 48 ++-- .../mvp/presenter/ComicsPreviewPresenter.java | 58 +++-- .../activity/ComicListDetailActivity.java | 52 ++-- .../comics/activity/ComicPreviewActivity.java | 240 ++++++++++-------- .../mvp/ui/comics/adapter/ChapterAdapter.java | 2 - .../comics/adapter/ChapterDetailAdapter.java | 15 +- .../mvp/ui/comics/adapter/PreviewAdapter.java | 74 +++++- .../mvp/ui/comics/dialog/ModuleDialog.java | 5 +- .../com/lai/mtc/mvp/utlis/CommonUtil.java | 29 +++ .../com/lai/mtc/mvp/utlis/ScreenUtils.java | 15 ++ .../main/res/layout/comic_item_preview.xml | 12 +- build.gradle | 18 +- config.gradle | 60 +++++ settings.gradle | 2 +- 23 files changed, 497 insertions(+), 322 deletions(-) create mode 100644 app/src/main/java/com/lai/mtc/comm/Parameter.java delete mode 100644 app/src/main/java/com/lai/mtc/comm/widget/MyPagerSnapHelper.java create mode 100644 config.gradle diff --git a/app/build.gradle b/app/build.gradle index 4877b18..543b1ae 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -26,13 +26,15 @@ android { } getSigningProperties() - compileSdkVersion 26 + compileSdkVersion rootProject.ext.android.compileSdkVersion + defaultConfig { applicationId "com.lai.mtc" - minSdkVersion 15 - targetSdkVersion 26 - versionCode 1 - versionName "1.0" + minSdkVersion rootProject.ext.android.minSdkVersion + targetSdkVersion rootProject.ext.android.targetSdkVersion + versionCode rootProject.ext.android.versionCode + versionName rootProject.ext.android.versionName + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" //只保留了中文和英文的语言资源 resConfigs "zh", "en" @@ -95,7 +97,42 @@ dependencies { androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) - implementation "com.android.support:design:$support" + androidTestImplementation rootProject.ext.dependencies.runner + androidTestImplementation rootProject.ext.dependencies.espresso + + api rootProject.ext.dependencies.appcompat_v7 + api rootProject.ext.dependencies.design + api rootProject.ext.dependencies.cardview + api rootProject.ext.dependencies.rxjava + api rootProject.ext.dependencies.rxandroid + api rootProject.ext.dependencies.retrofit + api rootProject.ext.dependencies.retrofit_converter_gson + api rootProject.ext.dependencies.retrofit_adapter_rxjava2 + api rootProject.ext.dependencies.interceptor + api rootProject.ext.dependencies.rxlifecycle2 + api rootProject.ext.dependencies.butterknife + annotationProcessor rootProject.ext.dependencies.butterknife_annotationProcessor + + api rootProject.ext.dependencies.dagger + annotationProcessor rootProject.ext.dependencies.dagger_annotationProcessor + provided rootProject.ext.dependencies.dagger_provided + api rootProject.ext.dependencies.dagger_support + annotationProcessor rootProject.ext.dependencies.dagger_android_annotationProcessor + + api rootProject.ext.dependencies.buttonStyle + api rootProject.ext.dependencies.BaseRecyclerViewAdapterHelper + api rootProject.ext.dependencies.lottie + api rootProject.ext.dependencies.glide + annotationProcessor rootProject.ext.dependencies.glide_annotationProcessor + api(rootProject.ext.dependencies.integration) { + transitive = false + } + + implementation(rootProject.ext.dependencies.glide_recyclerview) { + // Excludes the support library because it's already included by Glide. + transitive = false + } + /*implementation "com.android.support:design:$support" implementation "com.android.support:appcompat-v7:$support" implementation "com.android.support:cardview-v7:$support" testCompile 'junit:junit:4.12' @@ -131,5 +168,5 @@ dependencies { annotationProcessor "com.github.bumptech.glide:compiler:$glide" implementation("com.github.bumptech.glide:okhttp3-integration:$glide") { transitive = false - } + }*/ } diff --git a/app/objectbox-models/default.json.bak b/app/objectbox-models/default.json.bak index f02de59..c5851d3 100644 --- a/app/objectbox-models/default.json.bak +++ b/app/objectbox-models/default.json.bak @@ -5,7 +5,7 @@ "entities": [ { "id": "1:8699208959836505960", - "lastPropertyId": "5:3948644640204751108", + "lastPropertyId": "7:3513099458287364368", "name": "MyCollection", "properties": [ { @@ -27,6 +27,14 @@ { "id": "5:3948644640204751108", "name": "name" + }, + { + "id": "6:1459824881156873142", + "name": "status" + }, + { + "id": "7:3513099458287364368", + "name": "author" } ], "relations": [] diff --git a/app/src/main/java/com/lai/mtc/MainActivity.java b/app/src/main/java/com/lai/mtc/MainActivity.java index b071831..3be09d5 100644 --- a/app/src/main/java/com/lai/mtc/MainActivity.java +++ b/app/src/main/java/com/lai/mtc/MainActivity.java @@ -224,10 +224,10 @@ public boolean onCreateOptionsMenu(Menu menu) { public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.action_module) { mMenuItem = item; - if ("多列".equals(item.getTitle())) { + if ("多列".equals(item.getTitle().toString())) { ((ComicsMainFragment) currentFragment).changeMode(ComicsPresenter.SINGLE_MODE, new Consumer() { @Override - public void accept(Boolean aBoolean) throws Exception { + public void accept(Boolean aBoolean) { mMenuItem.setIcon(getResources().getDrawable(R.drawable.ic_line_weight_black_24dp)); mMenuItem.setTitle("单列"); } @@ -235,7 +235,7 @@ public void accept(Boolean aBoolean) throws Exception { } else { ((ComicsMainFragment) currentFragment).changeMode(ComicsPresenter.MANY_MODE, new Consumer() { @Override - public void accept(Boolean aBoolean) throws Exception { + public void accept(Boolean aBoolean) { mMenuItem.setTitle("多列"); mMenuItem.setIcon(getResources().getDrawable(R.drawable.ic_dashboard_black_24dp)); } diff --git a/app/src/main/java/com/lai/mtc/TestMainActivity.java b/app/src/main/java/com/lai/mtc/TestMainActivity.java index 3284b72..eae4b03 100644 --- a/app/src/main/java/com/lai/mtc/TestMainActivity.java +++ b/app/src/main/java/com/lai/mtc/TestMainActivity.java @@ -1,6 +1,7 @@ package com.lai.mtc; import android.os.Bundle; +import android.util.Log; import android.view.View; import android.widget.ImageView; @@ -9,6 +10,7 @@ import com.lai.mtc.mvp.base.impl.BaseMvpActivity; import com.lai.mtc.mvp.contract.ComicsPreviewContract; import com.lai.mtc.mvp.presenter.ComicsPreviewPresenter; +import com.lai.mtc.mvp.utlis.ScreenUtils; import com.lai.mtc.mvp.utlis.glide.GlideCacheUtil; import com.lai.mtc.mvp.utlis.glide.ImageUtils; @@ -25,7 +27,9 @@ public void init(Bundle savedInstanceState) { findViewById(R.id.btn_1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - mPresenter.testList(TestMainActivity.this); + int[] imageWidthHeight = ScreenUtils.getImageWidthHeight(url); + Log.w("1111","ima"); + //mPresenter.testList(TestMainActivity.this); } }); findViewById(R.id.btn_2).setOnClickListener(new View.OnClickListener() { @@ -53,7 +57,6 @@ public View getTitleBar() { return null; } - @Override public void showPreview(ComicPreView comicPreView) { } @@ -63,6 +66,11 @@ public void showInfo(ComicListDetail comicListDetail) { } + @Override + public void showPreview(ComicPreView comicPreView, boolean isRefresh) { + + } + //@Inject // Retrofit mComicApi; diff --git a/app/src/main/java/com/lai/mtc/bean/ComicListDetail.java b/app/src/main/java/com/lai/mtc/bean/ComicListDetail.java index dc3240f..9ec5f7f 100644 --- a/app/src/main/java/com/lai/mtc/bean/ComicListDetail.java +++ b/app/src/main/java/com/lai/mtc/bean/ComicListDetail.java @@ -146,6 +146,7 @@ public List getChapters() { public void setChapters(List chapters) { this.chapters = chapters; + this.ascendingOrderChapters = chapters; } public List getMirrors() { @@ -167,6 +168,17 @@ public void setMirrors(List mirrors) { private java.util.List tag_list; private java.util.List chapters; + public List getAscendingOrderChapters() { + return ascendingOrderChapters; + } + + public void setAscendingOrderChapters(List ascendingOrderChapters) { + this.ascendingOrderChapters = ascendingOrderChapters; + } + + //升序 + private java.util.List ascendingOrderChapters; + public List getShowChapters() { return mShowChapters; } @@ -237,7 +249,7 @@ public void setTrack_url(String track_url) { } - public static class MirrorsBean implements IPopMenu,Serializable { + public static class MirrorsBean implements IPopMenu, Serializable { private int id; private String source; diff --git a/app/src/main/java/com/lai/mtc/comm/Parameter.java b/app/src/main/java/com/lai/mtc/comm/Parameter.java new file mode 100644 index 0000000..8c8f96c --- /dev/null +++ b/app/src/main/java/com/lai/mtc/comm/Parameter.java @@ -0,0 +1,14 @@ +package com.lai.mtc.comm; + +//常见参数 +public class Parameter { + + //配置 + public static final String SP_CONFIG = "config"; + //预览模式 + public static final String SP_PREVIEW_MODE = "module"; + //集数分页标准 + public static final int PAGING_STANDARD = 52; + //提前加载多少页 + public static final int MAX_PRELOAD = 4; +} diff --git a/app/src/main/java/com/lai/mtc/comm/widget/MyPagerSnapHelper.java b/app/src/main/java/com/lai/mtc/comm/widget/MyPagerSnapHelper.java deleted file mode 100644 index 5908633..0000000 --- a/app/src/main/java/com/lai/mtc/comm/widget/MyPagerSnapHelper.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.lai.mtc.comm.widget; - -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.LinearSmoothScroller; -import android.support.v7.widget.OrientationHelper; -import android.support.v7.widget.PagerSnapHelper; -import android.support.v7.widget.RecyclerView; -import android.util.DisplayMetrics; -import android.view.View; - -public class MyPagerSnapHelper extends PagerSnapHelper { - - private RecyclerView mRecyclerView; - private static final float MILLISECONDS_PER_INCH = 100f; - private static final int MAX_SCROLL_ON_FLING_DURATION = 100; // ms - - private int mExtraSpace = 0; - - public MyPagerSnapHelper(int extraSpace) { - mExtraSpace = extraSpace; - } - - @Override - public void attachToRecyclerView(@Nullable RecyclerView recyclerView) throws IllegalStateException { - super.attachToRecyclerView(recyclerView); - this.mRecyclerView = recyclerView; - } - - @Override - protected LinearSmoothScroller createSnapScroller(RecyclerView.LayoutManager layoutManager) { - if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) { - return null; - } - return new LinearSmoothScroller(mRecyclerView.getContext()) { - @Override - protected void onTargetFound(View targetView, RecyclerView.State state, RecyclerView.SmoothScroller.Action action) { - int[] snapDistances = calculateDistanceToFinalSnap(mRecyclerView.getLayoutManager(), - targetView); - int dx = snapDistances[0]; - final int dy = snapDistances[1]; - if (dx < 0) { - dx += mExtraSpace * mRecyclerView.getWidth(); - if (dx > 0) { - LinearLayoutManager linearLayoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager(); - int firstItemPosition = linearLayoutManager.findFirstVisibleItemPosition(); - if (firstItemPosition == 1) { - dx = snapDistances[0] + mRecyclerView.getWidth(); - } else { - dx = -dx; - } - } - } - - final int time = calculateTimeForDeceleration(Math.max(Math.abs(dx), Math.abs(dy))); - if (time > 0) { - action.update(dx, dy, time, mDecelerateInterpolator); - } - } - - @Override - protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) { - return MILLISECONDS_PER_INCH / displayMetrics.densityDpi; - } - - @Override - protected int calculateTimeForScrolling(int dx) { - return Math.min(MAX_SCROLL_ON_FLING_DURATION, super.calculateTimeForScrolling(dx)); - } - }; - } - -} diff --git a/app/src/main/java/com/lai/mtc/di/module/NetModule.java b/app/src/main/java/com/lai/mtc/di/module/NetModule.java index ca01807..78b40a5 100644 --- a/app/src/main/java/com/lai/mtc/di/module/NetModule.java +++ b/app/src/main/java/com/lai/mtc/di/module/NetModule.java @@ -139,9 +139,9 @@ public Response intercept(Chain chain) throws IOException { @Provides @Singleton - OkHttpClient provideOkHttpClient(HttpLoggingInterceptor httpLoggingInterceptor,CommonInterceptor commonInterceptor,CacheInterceptor cacheInterceptor) { + OkHttpClient provideOkHttpClient(HttpLoggingInterceptor httpLoggingInterceptor, CommonInterceptor commonInterceptor, CacheInterceptor cacheInterceptor) { OkHttpClient.Builder builder = new OkHttpClient.Builder(); - builder.cache(new Cache(new File(Environment.getExternalStorageDirectory().getPath(), "HttpCache"), 1024 * 1024 * 10)). + builder.cache(new Cache(new File(MTC.getApp().getFilesDir(), "HttpCache"), 1024 * 1024 * 10)). addInterceptor(httpLoggingInterceptor) .addInterceptor(commonInterceptor) .addNetworkInterceptor(cacheInterceptor).retryOnConnectionFailure(true) diff --git a/app/src/main/java/com/lai/mtc/mvp/contract/ComicsPreviewContract.java b/app/src/main/java/com/lai/mtc/mvp/contract/ComicsPreviewContract.java index 123965a..c7787d1 100644 --- a/app/src/main/java/com/lai/mtc/mvp/contract/ComicsPreviewContract.java +++ b/app/src/main/java/com/lai/mtc/mvp/contract/ComicsPreviewContract.java @@ -17,11 +17,11 @@ public class ComicsPreviewContract { //对于经常使用的关于UI的方法可以定义到IView中,如显示隐藏进度条,和显示文字消息 public interface View extends IView { - void showPreview(ComicPreView comicPreView); + void showPreview(ComicPreView comicPreView, boolean isRefresh); } //Model层定义接口,外部只需关心Model返回的数据,无需关心内部细节,如是否使用缓存 public interface Model extends IModel { - void requestPreview(int id, int cid, Context context); + void requestPreview(int id, int cid, Context context, boolean isRefresh); } } diff --git a/app/src/main/java/com/lai/mtc/mvp/presenter/ComicsListDetailPresenter.java b/app/src/main/java/com/lai/mtc/mvp/presenter/ComicsListDetailPresenter.java index 2db9145..254e68c 100644 --- a/app/src/main/java/com/lai/mtc/mvp/presenter/ComicsListDetailPresenter.java +++ b/app/src/main/java/com/lai/mtc/mvp/presenter/ComicsListDetailPresenter.java @@ -28,8 +28,7 @@ * @author Lai * @time 2017/12/11 17:04 * @describe 漫画详情业务桥梁P - * - * @see SimplePresenter + * @see SimplePresenter */ public class ComicsListDetailPresenter extends BasePresenter implements ComicsListDetailContract.Model { @@ -47,27 +46,26 @@ public void onDestroy() { } @Override - public void getComicById(final int id) { + public void getComicById(int id) { mRootView.showLoading(); - Observable.timer(1, TimeUnit.SECONDS).compose(mRootView.bindToLifecycle()).subscribe(new Consumer() { - @Override - public void accept(Long aLong) throws Exception { - mComicApi.getComicById(id).subscribeOn(Schedulers.io()).observeOn(Schedulers.computation()) - .compose(mRootView.bindToLifecycle()).map(new Function() { - @Override - public ComicListDetail apply(ComicListDetail comicListDetail) throws Exception { - if (comicListDetail != null && !ListUtils.isEmpty(comicListDetail.getChapters())) { - List mirrors = comicListDetail.getChapters(); - if (mirrors.size() > 52) { - comicListDetail.setShowChapters(new ArrayList<>(mirrors.subList(0, 52))); - comicListDetail.setLastChapters(new ArrayList<>(mirrors.subList(52, mirrors.size()))); - } - } - return comicListDetail; - } - }).compose(mRootView.bindToLifecycle()) - .onErrorResumeNext(new HttpResultFunction()) - .observeOn(AndroidSchedulers.mainThread()).subscribe(new HttpRxObserver(new HttpRxObserver.IResult() { + mComicApi.getComicById(id) + .compose(RxUtlis.toMain()) + .compose(mRootView.bindToLifecycle()) + /* .doOnNext(new Consumer() { + @Override + public void accept(ComicListDetail comicListDetail) { + if (comicListDetail != null && !ListUtils.isEmpty(comicListDetail.getChapters())) { + List mirrors = comicListDetail.getChapters(); + if (mirrors.size() > 52) { + comicListDetail.setShowChapters(new ArrayList<>(mirrors.subList(0, 52))); + comicListDetail.setLastChapters(new ArrayList<>(mirrors.subList(52, mirrors.size()))); + } + } + } + })*/ + .onErrorResumeNext(new HttpResultFunction()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(new HttpRxObserver<>(new HttpRxObserver.IResult() { @Override public void onSuccess(ComicListDetail comicListDetail) { mRootView.showDetail(comicListDetail); @@ -79,8 +77,6 @@ public void onError(ApiException e) { mRootView.handleError(e); } })); - } - }); } @Override @@ -88,7 +84,7 @@ public void reverse(final ComicListDetail comicListDetail) { mRootView.showLoading(); Observable.create(new ObservableOnSubscribe() { @Override - public void subscribe(ObservableEmitter e) throws Exception { + public void subscribe(ObservableEmitter e) { if (comicListDetail != null && !ListUtils.isEmpty(comicListDetail.getChapters())) { Collections.reverse(comicListDetail.getChapters()); List mirrors = comicListDetail.getChapters(); @@ -101,7 +97,7 @@ public void subscribe(ObservableEmitter e) throws Exception { } }).compose(RxUtlis.toMain()) .onErrorResumeNext(new HttpResultFunction()) - .subscribe(new HttpRxObserver(new HttpRxObserver.IResult() { + .subscribe(new HttpRxObserver<>(new HttpRxObserver.IResult() { @Override public void onSuccess(ComicListDetail comicListDetail) { mRootView.hideLoading(); diff --git a/app/src/main/java/com/lai/mtc/mvp/presenter/ComicsPreviewPresenter.java b/app/src/main/java/com/lai/mtc/mvp/presenter/ComicsPreviewPresenter.java index 3650a80..b20ac1c 100644 --- a/app/src/main/java/com/lai/mtc/mvp/presenter/ComicsPreviewPresenter.java +++ b/app/src/main/java/com/lai/mtc/mvp/presenter/ComicsPreviewPresenter.java @@ -1,6 +1,7 @@ package com.lai.mtc.mvp.presenter; +import android.annotation.SuppressLint; import android.content.Context; import android.graphics.drawable.Drawable; import android.support.annotation.Nullable; @@ -11,12 +12,15 @@ import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.target.Target; import com.google.gson.Gson; +import com.lai.mtc.R; import com.lai.mtc.api.ComicApi; +import com.lai.mtc.bean.ComicListDetail; import com.lai.mtc.bean.ComicPreView; import com.lai.mtc.comm.ApiException; import com.lai.mtc.comm.HttpRxObserver; import com.lai.mtc.mvp.base.impl.BasePresenter; import com.lai.mtc.mvp.contract.ComicsPreviewContract; +import com.lai.mtc.mvp.utlis.RxUtlis; import com.lai.mtc.mvp.utlis.glide.GlideApp; import java.util.ArrayList; @@ -31,13 +35,14 @@ import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.functions.Consumer; import io.reactivex.functions.Function; +import io.reactivex.functions.Predicate; import io.reactivex.schedulers.Schedulers; /** * @author Lai * @time 2018/1/23 16:37 * @describe 预览业务桥梁 - * @see SimplePresenter + * @see SimplePresenter */ public class ComicsPreviewPresenter extends BasePresenter implements ComicsPreviewContract.Model { @@ -130,35 +135,17 @@ public boolean onResourceReady(Drawable resource, Object model, Target @Override - public void requestPreview(int id, int index, final Context context) { + public void requestPreview(int id, int index, final Context context, final boolean isRefresh) { mRootView.showLoading(); - mComicApi.getComicPreViewById(id, index).subscribeOn(Schedulers.io()).observeOn(Schedulers.computation()) + mComicApi.getComicPreViewById(id, index) .compose(mRootView.bindToLifecycle()) - .flatMap(new Function>() { - @Override - public ObservableSource apply(final ComicPreView comicPreView) throws Exception { - final List list; - if (comicPreView.getPages().size() > 4) { - list = new ArrayList<>(comicPreView.getPages().subList(0, 4)); - } else { - list = comicPreView.getPages(); - } - List> glideObservable = createGlideObservable(list, context); - return Observable.combineLatest(glideObservable, new Function() { - @Override - public ComicPreView apply(Object[] objects) throws Exception { - return comicPreView; - } - }).compose(mRootView.bindToLifecycle()); - } - }).compose(mRootView.bindToLifecycle()) - .observeOn(AndroidSchedulers.mainThread()) + .compose(RxUtlis.toMain()) .onErrorResumeNext(new HttpResultFunction()) .subscribe(new HttpRxObserver<>(new HttpRxObserver.IResult() { @Override public void onSuccess(ComicPreView comicPreView) { mRootView.hideLoading(); - mRootView.showPreview(comicPreView); + mRootView.showPreview(comicPreView, isRefresh); } @Override @@ -167,4 +154,29 @@ public void onError(ApiException e) { } })); } + + /** + * 当前index所在集合的位置 + * + * @param index + */ + @SuppressLint("CheckResult") + public void getIndexByPosition(final int index, final List chapters, final Consumer callBack) { + Observable.fromIterable(chapters) + .filter(new Predicate() { + @Override + public boolean test(ComicListDetail.ChaptersBean chaptersBean) { + return chaptersBean.getIndex() == index; + } + }) + .compose(mRootView.bindToLifecycle()) + .compose(RxUtlis.toMain()) + .subscribe(new Consumer() { + @Override + public void accept(ComicListDetail.ChaptersBean chaptersBean) throws Exception { + callBack.accept(chapters.lastIndexOf(chaptersBean)); + } + }); + } + } diff --git a/app/src/main/java/com/lai/mtc/mvp/ui/comics/activity/ComicListDetailActivity.java b/app/src/main/java/com/lai/mtc/mvp/ui/comics/activity/ComicListDetailActivity.java index b1da599..8f96f5b 100644 --- a/app/src/main/java/com/lai/mtc/mvp/ui/comics/activity/ComicListDetailActivity.java +++ b/app/src/main/java/com/lai/mtc/mvp/ui/comics/activity/ComicListDetailActivity.java @@ -30,6 +30,7 @@ import com.lai.mtc.mvp.contract.ComicsListDetailContract; import com.lai.mtc.mvp.presenter.ComicsListDetailPresenter; import com.lai.mtc.mvp.ui.comics.adapter.ChapterDetailAdapter; +import com.lai.mtc.mvp.utlis.CommonUtil; import com.lai.mtc.mvp.utlis.IPopMenu; import com.lai.mtc.mvp.utlis.ListUtils; import com.lai.mtc.mvp.utlis.PopupMenuUtil; @@ -39,6 +40,7 @@ import com.lai.mtc.mvp.utlis.glide.ImageUtils; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.concurrent.TimeUnit; @@ -49,6 +51,7 @@ import butterknife.OnClick; import io.reactivex.Observable; import io.reactivex.functions.Consumer; +import io.reactivex.internal.util.ArrayListSupplier; /** * @author Lai @@ -119,8 +122,13 @@ public int getLayoutResId(Bundle savedInstanceState) { @OnClick(R.id.iv_reverse) public void reverse() { - if (mComicListDetail != null) - mPresenter.reverse(mComicListDetail); + if (commonAdapter != null) { + Collections.reverse(commonAdapter.getData()); + if (commonAdapter != null) { + // commonAdapter.setIndex(mRecord.getIndex()); + commonAdapter.notifyDataSetChanged(); + } + } } @OnClick(R.id.btn_find) @@ -160,8 +168,6 @@ public void init(Bundle savedInstanceState) { init(entriesBean); - // ShareElementUtils.startAnimator(getIntent(), this, mCoverImageView, mBgView); - ViewUtils.setTitleBarByTop(mAppBarLayout, this); setToolBar(mToolbar, getTitle().toString(), true); @@ -191,18 +197,10 @@ public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { commonAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() { @Override public void onLoadMoreRequested() { - Observable.timer(1, TimeUnit.SECONDS) - .compose(ComicListDetailActivity.this.bindToLifecycle()) - .compose(RxUtlis.toMain()) - .subscribe(new Consumer() { - @Override - public void accept(Long aLong) throws Exception { - List lastChapters = mComicListDetail.getLastChapters(); - if (lastChapters != null) - commonAdapter.setData(false, lastChapters); - commonAdapter.loadMoreEnd(); - } - }); + List chaptersBeans = CommonUtil.transformationLastList(mComicListDetail.getChapters()); + if (chaptersBeans != null) + commonAdapter.setData(false, chaptersBeans); + commonAdapter.loadMoreEnd(); } }, mRvList); } @@ -305,21 +303,19 @@ public void init(ComicListInfo.EntriesBean info) { @Override public void showDetail(ComicListDetail comicListDetail) { mComicListDetail = comicListDetail; - //查看集合是否有经过裁剪、有则显示处理果的列表 - List showChapters = comicListDetail.getShowChapters(); - List chapters = !ListUtils.isEmpty(showChapters) ? showChapters : comicListDetail.getChapters(); - if (!ListUtils.isEmpty(chapters)) - mChaptersBean = chapters.get(0); - - commonAdapter.setData(true, chapters); - commonAdapter.disableLoadMoreIfNotFullPage(); + //裁剪前面的第52个。太多 加载会很慢 + List chaptersBeans = CommonUtil.transformationStartList(comicListDetail.getChapters()); + commonAdapter.setData(true, chaptersBeans); commonAdapter.setComicListDetail(comicListDetail); - //commonAdapter.updateOnItemChildClickListener(); - //更当前列表记录 - comicId = mChaptersBean.getComic_id(); + if (!ListUtils.isEmpty(chaptersBeans)) { + mChaptersBean = chaptersBeans.get(0); + + //更当前列表记录 + comicId = mChaptersBean.getComic_id(); + } + updateCurrRecordInfo(); - //如果展示的集合不为空,说明还有集数没加载完 } diff --git a/app/src/main/java/com/lai/mtc/mvp/ui/comics/activity/ComicPreviewActivity.java b/app/src/main/java/com/lai/mtc/mvp/ui/comics/activity/ComicPreviewActivity.java index 0f3cc11..4b30172 100644 --- a/app/src/main/java/com/lai/mtc/mvp/ui/comics/activity/ComicPreviewActivity.java +++ b/app/src/main/java/com/lai/mtc/mvp/ui/comics/activity/ComicPreviewActivity.java @@ -13,11 +13,14 @@ import android.widget.SeekBar; import android.widget.TextView; +import com.bumptech.glide.Glide; +import com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader; +import com.bumptech.glide.util.ViewPreloadSizeProvider; import com.chad.library.adapter.base.BaseQuickAdapter; import com.lai.mtc.R; import com.lai.mtc.bean.ComicListDetail; import com.lai.mtc.bean.ComicPreView; -import com.lai.mtc.comm.widget.MyPagerSnapHelper; +import com.lai.mtc.comm.Parameter; import com.lai.mtc.comm.widget.PreCacheLayoutManager; import com.lai.mtc.comm.widget.TouchRecyclerView; import com.lai.mtc.dao.RecordDao; @@ -28,20 +31,17 @@ import com.lai.mtc.mvp.ui.comics.adapter.PreviewAdapter; import com.lai.mtc.mvp.ui.comics.dialog.ModuleDialog; import com.lai.mtc.mvp.ui.comics.pop.WindowLightPop; -import com.lai.mtc.mvp.utlis.ListUtils; -import com.lai.mtc.mvp.utlis.RxUtlis; import com.lai.mtc.mvp.utlis.SPUtils; import com.lai.mtc.mvp.utlis.ScreenUtils; import com.lai.mtc.mvp.utlis.ViewUtils; +import java.util.Collections; import java.util.List; -import java.util.concurrent.TimeUnit; import javax.inject.Inject; import butterknife.BindView; import butterknife.OnClick; -import io.reactivex.Observable; import io.reactivex.functions.Consumer; import static android.support.v7.widget.RecyclerView.SCROLL_STATE_IDLE; @@ -105,13 +105,48 @@ public class ComicPreviewActivity extends BaseMvpActivity mRecyclerViewPreLoader; + @Override public int getLayoutResId(Bundle savedInstanceState) { return R.layout.comic_activity_preview; } + + @Override + public void init(Bundle savedInstanceState) { + mComicListDetail = (ComicListDetail) getIntent().getSerializableExtra("comicListDetail"); + index = getIntent().getIntExtra("index", 1); + hideLayout(); + initPreLoaderAdapter(); + if (mComicListDetail != null) { + setToolBar(mToolbar, mComicListDetail.getName(), true); + showInfo(mComicListDetail); + request(index, true); + } + } + + /** + * 初始化预加载Adapter + */ + public void initPreLoaderAdapter() { + final int module = SPUtils.getInstance(Parameter.SP_CONFIG).getInt(Parameter.SP_PREVIEW_MODE, 0); + int layoutRes = module == 0 ? R.layout.comic_item_preview : R.layout.comic_item_preview2; + initModule(module); + final ViewPreloadSizeProvider preloadSizeProvider = new ViewPreloadSizeProvider<>(); + mPreviewAdapter = new PreviewAdapter(layoutRes, Collections.emptyList(), preloadSizeProvider); + if (mRecyclerViewPreLoader != null) { + mRvList.removeOnScrollListener(mRecyclerViewPreLoader); + } + mRecyclerViewPreLoader = new RecyclerViewPreloader<>(Glide.with(this), mPreviewAdapter, preloadSizeProvider, Parameter.MAX_PRELOAD); + mRvList.addOnScrollListener(mRecyclerViewPreLoader); + + mPreviewAdapter.bindToRecyclerView(mRvList); + mPreviewAdapter.setOnLoadMoreListener(mRequestLoadMoreListener, mRvList); + } + + /** * 底部菜单点击事件 * @@ -142,8 +177,9 @@ private void openModuleDialog() { moduleDialog.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - initModule(SPUtils.getInstance("config").getInt("module", 0)); - request(index); + //initModule(SPUtils.getInstance(Parameter.SP_CONFIG).getInt(Parameter.SP_PREVIEW_MODE, 0)); + initPreLoaderAdapter(); + request(index, true); } }); } @@ -155,37 +191,45 @@ private void openLightPop() { windowLightPop.showAtLocation(mFrameLayout, Gravity.BOTTOM, 0, 0); } - @Override - public void init(Bundle savedInstanceState) { - mComicListDetail = (ComicListDetail) getIntent().getSerializableExtra("comicListDetail"); - index = getIntent().getIntExtra("index", 1); - hideLayout(); - if (mComicListDetail != null) { - setToolBar(mToolbar, mComicListDetail.getName(), true); - showInfo(mComicListDetail); - request(index); - } - } /** * 根据标识去请求集数图片 * * @param index 标识 */ - public void request(int index) { + public void request(int index, boolean isRefresh) { this.index = index; if (mComicListDetail != null) - mPresenter.requestPreview(mComicListDetail.getId(), index, this); + mPresenter.requestPreview(mComicListDetail.getId(), index, this, isRefresh); + } + + @Override + public void showPreview(ComicPreView comicPreView, boolean isRefresh) { + firstVisibleItem = comicPreView; + List pages = comicPreView.getPages(); + //更新记录 + mPreviewAdapter.setComicPreView(comicPreView); + mPreviewAdapter.setData(isRefresh, pages); + + updateCurrObject(mRvList); + if (!isRefresh) { + //更新左边菜单 + mLeftChaptersAdapter.updatePosition(nextPosition, index); + mPreviewAdapter.loadMoreComplete(); + } else { + mRvList.scrollToPosition(0); + //更新数据 + updateCurrIndex(0); + } } @Override protected void bindEvent() { //为了更好的提高滚动的流畅性,可以加大 RecyclerView 的缓存,用空间换时间 mRvList.setHasFixedSize(true); - mRvList.setItemViewCacheSize(10); + //mRvList.setItemViewCacheSize(10); mRvList.setDrawingCacheEnabled(true); mRvList.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH); - initModule(SPUtils.getInstance("config").getInt("module", 0)); mRvList.setITouchCallBack(new TouchRecyclerView.ITouchCallBack() { @Override public void click() { @@ -197,6 +241,7 @@ public void click() { } } }); + mRvList.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { @@ -206,15 +251,6 @@ public void onScrollStateChanged(RecyclerView recyclerView, int newState) { updateCurrIndex(updateCurrObject(recyclerView)); } } - - @Override - public void onScrolled(RecyclerView recyclerView, int dx, int dy) { - if (isUpdate) { - updateCurrIndex(updateCurrObject(recyclerView)); - isUpdate = false; - } - super.onScrolled(recyclerView, dx, dy); - } }); mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @@ -230,21 +266,39 @@ public void onStartTrackingTouch(SeekBar seekBar) { @Override public void onStopTrackingTouch(SeekBar seekBar) { - //先更新当前图片所在的对象 - updateCurrObject(mRvList); int progress = seekBar.getProgress() <= 0 ? 0 : seekBar.getProgress() - 1; ComicPreView.PagesBean pagesBean = firstVisibleItem.getPages().get(progress); if (pagesBean != null) { int i = mPreviewAdapter.getData().indexOf(pagesBean); if (i != -1) { mRvList.scrollToPosition(i); - isUpdate = true; + //先更新当前图片所在的对象 + updateCurrIndex(updateCurrObject(i)); } } } }); } + final BaseQuickAdapter.RequestLoadMoreListener mRequestLoadMoreListener = new BaseQuickAdapter.RequestLoadMoreListener() { + @Override + public void onLoadMoreRequested() { + //这里可以提前加载下一话 + int lastPosition = mLeftChaptersAdapter.getLastPosition(); + List data = mLeftChaptersAdapter.getData(); + nextPosition = lastPosition + 1; + if ((nextPosition == data.size())) { + //已经是最后一话不在自动加载 + mPreviewAdapter.loadMoreEnd(); + } else { + ComicListDetail.ChaptersBean item = mLeftChaptersAdapter.getItem(nextPosition); + if (item != null) { + request(item.getIndex(), false); + } + } + } + }; + /** * 更新当前图片所在的对象 * @@ -263,6 +317,21 @@ public int updateCurrObject(RecyclerView recyclerView) { return firstVisibleItemPosition; } + /** + * 更新当前图片所在的对象 + * + * @return 当前位置 + */ + public int updateCurrObject(int position) { + ComicPreView.PagesBean item = mPreviewAdapter.getItem(position); + if (item != null) { + ComicPreView comicPreViewByIndex = mPreviewAdapter.getComicPreViewByIndex(item.getIndex()); + if (comicPreViewByIndex != null) + firstVisibleItem = comicPreViewByIndex; + } + return position; + } + /** * 更新当前右下角最新集数以及更新seekBar进度 * @@ -339,49 +408,6 @@ public View getTitleBar() { return mToolbar; } - @Override - public void showPreview(ComicPreView comicPreView) { - final int module = SPUtils.getInstance("config").getInt("module", 0); - //设置设置的预览模式 - //用哪个布局 - int layoutRes = module == 0 ? R.layout.comic_item_preview : R.layout.comic_item_preview2; - // - List pages = comicPreView.getPages(); - if (mPreviewAdapter == null) { - mPreviewAdapter = new PreviewAdapter(layoutRes, pages, comicPreView); - firstVisibleItem = comicPreView; - int size = pages.size(); - String format = String.format(getString(R.string.current_set_number), comicPreView.getName(), 1, size); - mTvCurrPager.setText(format); - mSeekBar.setMax(size); - mPreviewAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() { - @Override - public void onLoadMoreRequested() { - //这里可以提前加载下一话 - int lastPosition = mLeftChaptersAdapter.getLastPosition(); - List data = mLeftChaptersAdapter.getData(); - nextPosition = lastPosition + 1; - if ((nextPosition == data.size())) { - //已经是最后一话不在自动加载 - mPreviewAdapter.loadMoreEnd(); - } else { - ComicListDetail.ChaptersBean item = mLeftChaptersAdapter.getItem(nextPosition); - if (item != null) { - request(item.getIndex()); - } - } - } - }, mRvList); - mRvList.setAdapter(mPreviewAdapter); - } else { - //更新记录 - mPreviewAdapter.setComicPreView(comicPreView); - mPreviewAdapter.setData(false, pages); - //更新左边菜单 - mLeftChaptersAdapter.updatePosition(nextPosition, index); - mPreviewAdapter.loadMoreComplete(); - } - } /** * 初始化观看模式 @@ -389,9 +415,11 @@ public void onLoadMoreRequested() { * @param module 模式 */ private void initModule(int module) { - PreCacheLayoutManager linearLayoutManager = new PreCacheLayoutManager(this); - linearLayoutManager.setExtraSpace(2); + // PreCacheLayoutManager linearLayoutManager = new PreCacheLayoutManager(this); + //提前加载 + // linearLayoutManager.setExtraSpace(2); //默认的模式 + LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); if (module == 0) { mRvList.setLayoutManager(linearLayoutManager); if (mPagerSnapHelper != null) @@ -399,51 +427,45 @@ private void initModule(int module) { } else { linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); mRvList.setLayoutManager(linearLayoutManager); - mPagerSnapHelper = new MyPagerSnapHelper(linearLayoutManager.getExtraSpace()); + mPagerSnapHelper = new PagerSnapHelper(); mPagerSnapHelper.attachToRecyclerView(mRvList); } mPreviewAdapter = null; } + /** + * 初始化基本信息 + * + * @param comicListDetail + */ public void showInfo(ComicListDetail comicListDetail) { if (comicListDetail == null || comicListDetail.getChapters() == null || mLeftChaptersAdapter != null) return; mRvLeftList.setLayoutManager(new LinearLayoutManager(this)); - List showChapters = comicListDetail.getShowChapters(); - List chapters = !ListUtils.isEmpty(showChapters) ? showChapters : comicListDetail.getChapters(); - mLeftChaptersAdapter = new ChapterAdapter(chapters); - mRvLeftList.setAdapter(mLeftChaptersAdapter); + mLeftChaptersAdapter = new ChapterAdapter(mComicListDetail.getChapters()); mLeftChaptersAdapter.setIndex(index); mLeftChaptersAdapter.bindToRecyclerView(mRvLeftList); - mLeftChaptersAdapter.setItemClickListener(new BaseQuickAdapter.OnItemClickListener() { + mLeftChaptersAdapter.setItemClickListener(mLeftItemClickListener); + //跳转到对应的集数 + mPresenter.getIndexByPosition(index, comicListDetail.getChapters(), new Consumer() { @Override - public void onItemClick(BaseQuickAdapter adapter, View view, int position) { - returnAllStatus(); - mPreviewAdapter = null; - ComicListDetail.ChaptersBean o = (ComicListDetail.ChaptersBean) adapter.getData().get(position); - index = o.getIndex(); - request(index); + public void accept(Integer integer) { + mRvLeftList.scrollToPosition(integer); } }); - //如果展示的集合不为空,说明还有集数没加载完 - if (!ListUtils.isEmpty(showChapters)) - mLeftChaptersAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() { - @Override - public void onLoadMoreRequested() { - Observable.timer(1, TimeUnit.SECONDS) - .compose(ComicPreviewActivity.this.bindToLifecycle()) - .compose(RxUtlis.toMain()) - .subscribe(new Consumer() { - @Override - public void accept(Long aLong) throws Exception { - mLeftChaptersAdapter.setData(false, mComicListDetail.getLastChapters()); - mLeftChaptersAdapter.loadMoreEnd(); - } - }); - } - }, mRvList); } + //左边集数点击 + public final BaseQuickAdapter.OnItemClickListener mLeftItemClickListener = new BaseQuickAdapter.OnItemClickListener() { + @Override + public void onItemClick(BaseQuickAdapter adapter, View view, int position) { + returnAllStatus(); + ComicListDetail.ChaptersBean o = (ComicListDetail.ChaptersBean) adapter.getData().get(position); + mRvList.scrollToPosition(0); + request(o.getIndex(), true); + } + }; + @Override public void onBackPressed() { if (!returnAllStatus()) diff --git a/app/src/main/java/com/lai/mtc/mvp/ui/comics/adapter/ChapterAdapter.java b/app/src/main/java/com/lai/mtc/mvp/ui/comics/adapter/ChapterAdapter.java index d53870c..c54d1c4 100644 --- a/app/src/main/java/com/lai/mtc/mvp/ui/comics/adapter/ChapterAdapter.java +++ b/app/src/main/java/com/lai/mtc/mvp/ui/comics/adapter/ChapterAdapter.java @@ -26,8 +26,6 @@ public void setLastPosition(int lastPosition) { this.lastPosition = lastPosition; } - private SparseIntArray mindexMap = new SparseIntArray(); - public int getLastPosition() { return lastPosition; } diff --git a/app/src/main/java/com/lai/mtc/mvp/ui/comics/adapter/ChapterDetailAdapter.java b/app/src/main/java/com/lai/mtc/mvp/ui/comics/adapter/ChapterDetailAdapter.java index 143a9c9..aef9636 100644 --- a/app/src/main/java/com/lai/mtc/mvp/ui/comics/adapter/ChapterDetailAdapter.java +++ b/app/src/main/java/com/lai/mtc/mvp/ui/comics/adapter/ChapterDetailAdapter.java @@ -25,6 +25,15 @@ public class ChapterDetailAdapter extends CommonAdapter { private int lastPosition; + + public int getIndex() { + return index; + } + + public void setIndex(int index) { + this.index = index; + } + private int index; private RecordDao mRecordDao; @@ -41,7 +50,7 @@ public ChapterDetailAdapter(@Nullable List data, R updateOnItemChildClickListener(); } - public void updateOnItemChildClickListener() { + private void updateOnItemChildClickListener() { setOnItemChildClickListener(new OnItemChildClickListener() { @Override public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) { @@ -76,9 +85,9 @@ public void init() { protected void convert(BaseViewHolder baseViewHolder, ComicListDetail.ChaptersBean chaptersBean, int position) { baseViewHolder.addOnClickListener(R.id.list_item); ButtonStyle buttonStyle = baseViewHolder.getView(R.id.list_item); - if (lastPosition != -1 && lastPosition == position) { + /*if (lastPosition != -1 && lastPosition == position) { buttonStyle.setNormalColor(R.color.colorEEE); - } else if (index == chaptersBean.getIndex()) { + } else */if (index == chaptersBean.getIndex()) { lastPosition = position; buttonStyle.setNormalColor(R.color.colorEEE); } else { diff --git a/app/src/main/java/com/lai/mtc/mvp/ui/comics/adapter/PreviewAdapter.java b/app/src/main/java/com/lai/mtc/mvp/ui/comics/adapter/PreviewAdapter.java index 453ffde..2933219 100644 --- a/app/src/main/java/com/lai/mtc/mvp/ui/comics/adapter/PreviewAdapter.java +++ b/app/src/main/java/com/lai/mtc/mvp/ui/comics/adapter/PreviewAdapter.java @@ -3,27 +3,35 @@ import android.graphics.Bitmap; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.util.Log; import android.util.SparseArray; import android.view.ViewGroup; import android.widget.ImageView; -import android.widget.RelativeLayout; -import com.bumptech.glide.GenericTransitionOptions; +import com.bumptech.glide.ListPreloader; +import com.bumptech.glide.RequestBuilder; import com.bumptech.glide.load.DataSource; import com.bumptech.glide.load.engine.DiskCacheStrategy; import com.bumptech.glide.load.engine.GlideException; import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.target.Target; +import com.bumptech.glide.util.ViewPreloadSizeProvider; import com.chad.library.adapter.base.BaseViewHolder; +import com.lai.mtc.MTC; import com.lai.mtc.R; import com.lai.mtc.bean.ComicPreView; import com.lai.mtc.comm.CommonAdapter; +import com.lai.mtc.comm.Parameter; +import com.lai.mtc.mvp.utlis.DensityUtil; +import com.lai.mtc.mvp.utlis.ListUtils; import com.lai.mtc.mvp.utlis.SPUtils; import com.lai.mtc.mvp.utlis.ScreenUtils; import com.lai.mtc.mvp.utlis.glide.GlideApp; import com.lai.mtc.mvp.utlis.glide.GlideRequest; +import java.util.Collections; import java.util.List; /** @@ -32,12 +40,19 @@ * @describe describe */ -public class PreviewAdapter extends CommonAdapter { +public class PreviewAdapter extends CommonAdapter implements ListPreloader.PreloadModelProvider { //预览模式 private int module; - - private SparseArray mComicPreViewSparseArray = new SparseArray<>(); + //加载图片的宽度 + private int tagWidth; + //加载图片的高度 + private int tagHeight; + //记录每一个集数的对象 + private final SparseArray mComicPreViewSparseArray = new SparseArray<>(); + //预加载的ViewPreLoad + private ViewPreloadSizeProvider preloadSizeProvider; + private int screenWidth; public void setComicPreView(ComicPreView comicPreView) { mComicPreView = comicPreView; @@ -54,10 +69,15 @@ public ComicPreView getComicPreViewByIndex(int index) { private ComicPreView mComicPreView; - public PreviewAdapter(int layoutResId, @Nullable List data, ComicPreView currComicPreView) { + public PreviewAdapter(int layoutResId + , List data + , ViewPreloadSizeProvider preloadSizeProvider) { super(layoutResId, data); - setComicPreView(currComicPreView); - module = SPUtils.getInstance("config").getInt("module", 0); + module = SPUtils.getInstance(Parameter.SP_CONFIG).getInt(Parameter.SP_PREVIEW_MODE, 0); + this.preloadSizeProvider = preloadSizeProvider; + tagHeight = module == 0 ? DensityUtil.dp2px(MTC.getApp(), 500f) : ScreenUtils.getScreenHeight() / 2; + screenWidth = ScreenUtils.getScreenWidth(); + tagWidth = screenWidth / 2; } @@ -69,10 +89,11 @@ protected void convert(BaseViewHolder baseViewHolder, ComicPreView.PagesBean pag pagesBean.setIndex(mComicPreView.getIndex()); GlideRequest transition = GlideApp.with(mContext) .asBitmap() + .override(tagWidth, tagHeight) .load(pagesBean.getTrack_url()) .placeholder(new ColorDrawable(Color.BLACK)) - .diskCacheStrategy(DiskCacheStrategy.ALL) - .transition(new GenericTransitionOptions()); + .diskCacheStrategy(DiskCacheStrategy.ALL); + if (module == 0) { RequestListener requestListener = new RequestListener() { @Override @@ -89,7 +110,7 @@ public boolean onResourceReady(Bitmap bitmap, Object model, Target targe if (imageView.getScaleType() != ImageView.ScaleType.FIT_XY) { imageView.setScaleType(ImageView.ScaleType.FIT_XY); } - /* ViewGroup.LayoutParams params = imageView.getLayoutParams(); + /* ViewGroup.LayoutParams params = imageView.getLayoutParams(); int vw = imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight(); float scale = (float) vw / (float) resource.getWidth(); int vh = Math.round(resource.getHeight() * scale); @@ -100,8 +121,8 @@ public boolean onResourceReady(Bitmap bitmap, Object model, Target targe int height = bitmap.getHeight(); float scale = ((float) height) / width; ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams(); - layoutParams.width = ScreenUtils.getScreenWidth(mContext); - layoutParams.height = (int) (scale * ScreenUtils.getScreenWidth(mContext)); + layoutParams.width = screenWidth; + layoutParams.height = (int) (scale * screenWidth); imageView.setLayoutParams(layoutParams); return false; @@ -111,5 +132,32 @@ public boolean onResourceReady(Bitmap bitmap, Object model, Target targe } else { transition.into(imageView); } + preloadSizeProvider.setView(imageView); + } + + + @NonNull + @Override + public List getPreloadItems(int position) { + try { + if (ListUtils.isEmpty(getData())) + return Collections.emptyList(); + return getData().subList(position, position + 1); + } catch (Exception e) { + e.printStackTrace(); + return Collections.emptyList(); + } + } + + @Nullable + @Override + public RequestBuilder getPreloadRequestBuilder(ComicPreView.PagesBean item) { + Log.w("11111", "预加载:" + item.getTrack_url()); + return GlideApp.with(mContext) + .asBitmap() + .load(item.getTrack_url()) + .override(tagWidth, tagHeight) + .placeholder(new ColorDrawable(Color.BLACK)) + .diskCacheStrategy(DiskCacheStrategy.ALL); } } diff --git a/app/src/main/java/com/lai/mtc/mvp/ui/comics/dialog/ModuleDialog.java b/app/src/main/java/com/lai/mtc/mvp/ui/comics/dialog/ModuleDialog.java index 715ac41..d0f1263 100644 --- a/app/src/main/java/com/lai/mtc/mvp/ui/comics/dialog/ModuleDialog.java +++ b/app/src/main/java/com/lai/mtc/mvp/ui/comics/dialog/ModuleDialog.java @@ -9,6 +9,7 @@ import android.widget.RadioButton; import com.lai.mtc.R; +import com.lai.mtc.comm.Parameter; import com.lai.mtc.mvp.utlis.SPUtils; /** @@ -46,7 +47,7 @@ protected void onCreate(Bundle savedInstanceState) { } private void init() { - int module = SPUtils.getInstance("config").getInt("module", 0); + int module = SPUtils.getInstance(Parameter.SP_CONFIG).getInt(Parameter.SP_PREVIEW_MODE, 0); if (module == 0) mCbPull.setChecked(true); else @@ -62,7 +63,7 @@ public void onClick(View v) { @Override public void onClick(View v) { int module = mCbPager.isChecked() ? 1 : 0; - SPUtils.getInstance("config").put("module", module); + SPUtils.getInstance(Parameter.SP_CONFIG).put(Parameter.SP_PREVIEW_MODE, module); if (mOnClickListener != null) mOnClickListener.onClick(v); dismiss(); diff --git a/app/src/main/java/com/lai/mtc/mvp/utlis/CommonUtil.java b/app/src/main/java/com/lai/mtc/mvp/utlis/CommonUtil.java index aab599b..d89ef19 100644 --- a/app/src/main/java/com/lai/mtc/mvp/utlis/CommonUtil.java +++ b/app/src/main/java/com/lai/mtc/mvp/utlis/CommonUtil.java @@ -4,6 +4,11 @@ import android.net.ConnectivityManager; import android.net.NetworkInfo; +import com.lai.mtc.comm.Parameter; + +import java.util.ArrayList; +import java.util.List; + /** * Created by hcc on 16/8/4 21:18 * 100332338@qq.com @@ -54,5 +59,29 @@ private static NetworkInfo getNetworkInfo(Context context) { } + /** + * 以52为分页标准,切割最后 + * + * @param list + * @return + */ + public static List transformationLastList(List list) { + if (!ListUtils.isEmpty(list) && list.size() > Parameter.PAGING_STANDARD) { + return new ArrayList<>(list.subList(Parameter.PAGING_STANDARD, list.size())); + } + return null; + } + /** + * 以52为分页标准,切割最前 + * + * @param list + * @return + */ + public static List transformationStartList(List list) { + if (!ListUtils.isEmpty(list) && list.size() > Parameter.PAGING_STANDARD) { + return new ArrayList<>(list.subList(0, Parameter.PAGING_STANDARD)); + } + return null; + } } diff --git a/app/src/main/java/com/lai/mtc/mvp/utlis/ScreenUtils.java b/app/src/main/java/com/lai/mtc/mvp/utlis/ScreenUtils.java index 2c6ff7a..c72bf86 100644 --- a/app/src/main/java/com/lai/mtc/mvp/utlis/ScreenUtils.java +++ b/app/src/main/java/com/lai/mtc/mvp/utlis/ScreenUtils.java @@ -7,6 +7,7 @@ import android.content.res.Configuration; import android.content.res.Resources; import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.graphics.Point; import android.os.Build; import android.provider.Settings; @@ -167,6 +168,20 @@ public static void switchScreen(@NonNull final Activity activity) { } } + public static int[] getImageWidthHeight(String path){ + BitmapFactory.Options options = new BitmapFactory.Options(); + + /** + * 最关键在此,把options.inJustDecodeBounds = true; + * 这里再decodeFile(),返回的bitmap为空,但此时调用options.outHeight时,已经包含了图片的高了 + */ + options.inJustDecodeBounds = true; + Bitmap bitmap = BitmapFactory.decodeFile(path, options); // 此时返回的bitmap为null + /** + *options.outHeight为原始图片的高 + */ + return new int[]{options.outWidth,options.outHeight}; + } /** * 获取屏幕旋转角度 diff --git a/app/src/main/res/layout/comic_item_preview.xml b/app/src/main/res/layout/comic_item_preview.xml index a76325e..d153eaf 100644 --- a/app/src/main/res/layout/comic_item_preview.xml +++ b/app/src/main/res/layout/comic_item_preview.xml @@ -1,19 +1,17 @@ + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:orientation="vertical"> + android:layout_height="500dp" /> + android:background="@android:color/black" /> \ No newline at end of file diff --git a/build.gradle b/build.gradle index d07b164..42f05d7 100644 --- a/build.gradle +++ b/build.gradle @@ -1,22 +1,8 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. -ext.support = "26.1.0" -ext.test_runner_version="1.0.1" -ext.test_espresso_version="3.0.1" -ext.rxjava="2.1.6" -ext.rxandroid="2.0.1" -ext.retrofit="2.3.0" -ext.converter_gson="2.3.0" -ext.adapter_rxjava2="2.3.0" -ext.logging_interceptor="3.8.1" -ext.rxlifecycle2="2.2.0" -ext.butterknife="8.8.1" -ext.dagger="2.12" -ext.buttonStyle="1.0.2" -ext.BaseRecyclerViewAdapterHelper="2.9.35" -ext.lottie="2.2.5" -ext.glide="4.4.0" + buildscript { + apply from: "config.gradle" ext.objectboxVersion = '1.4.0' repositories { google() diff --git a/config.gradle b/config.gradle new file mode 100644 index 0000000..7dd0d92 --- /dev/null +++ b/config.gradle @@ -0,0 +1,60 @@ +ext { + android = [ + compileSdkVersion: 26, + minSdkVersion : 15, + targetSdkVersion : 26, + versionCode : 1, + versionName : "1.0" + ] + libsVersion = [ + support = "26.1.0", + test_runner_version = "1.0.1", + test_espresso_version = "3.0.1", + rxjava = "2.1.6", + retrofit = "2.3.0", + rxandroid = "2.0.1", + retrofit = "2.3.0", + converter_gson = "2.3.0", + adapter_rxjava2 = "2.3.0", + logging_interceptor = "3.8.1", + rxlifecycle2 = "2.2.0", + butterknife = "8.8.1", + dagger = "2.12", + buttonStyle = "1.0.2", + BaseRecyclerViewAdapterHelper = "2.9.35", + lottie = "2.2.5", + glide = "4.4.0" + + ] + + dependencies = [ + appcompat_v7 : "com.android.support:appcompat-v7:$rootProject.support", + design : "com.android.support:design:$rootProject.support", + runner : "com.android.support.test:runner:$rootProject.test_runner_version", + cardview : "com.android.support:cardview-v7:$rootProject.support", + espresso : "com.android.support.test.espresso:espresso-core:$rootProject.test_espresso_version", + rxjava : "io.reactivex.rxjava2:rxjava:$rootProject.rxjava", + rxandroid : "io.reactivex.rxjava2:rxandroid:$rootProject.rxandroid", + retrofit : "com.squareup.retrofit2:retrofit:$rootProject.retrofit", + retrofit_converter_gson : "com.squareup.retrofit2:converter-gson:$rootProject.converter_gson", + retrofit_adapter_rxjava2 : "com.squareup.retrofit2:adapter-rxjava2:$rootProject.adapter_rxjava2", + interceptor : "com.squareup.okhttp3:logging-interceptor:$rootProject.logging_interceptor", + rxlifecycle2 : "com.trello.rxlifecycle2:rxlifecycle-components:$rootProject.rxlifecycle2", + //注解 + butterknife : "com.jakewharton:butterknife:$rootProject.butterknife", + butterknife_annotationProcessor : "com.jakewharton:butterknife-compiler:$rootProject.butterknife", + dagger : "com.google.dagger:dagger:$rootProject.dagger", + dagger_annotationProcessor : "com.google.dagger:dagger-compiler:$rootProject.dagger", + dagger_provided : 'org.glassfish:javax.annotation:10.0-b28', + dagger_support : "com.google.dagger:dagger-android-support:$rootProject.dagger", + dagger_android_annotationProcessor: "com.google.dagger:dagger-android-processor:$rootProject.dagger", + buttonStyle : "com.lai:buttonStyle:$rootProject.buttonStyle", + BaseRecyclerViewAdapterHelper : "com.github.CymChad:BaseRecyclerViewAdapterHelper:$rootProject.BaseRecyclerViewAdapterHelper", + lottie : "com.airbnb.android:lottie:$rootProject.lottie", + glide : "com.github.bumptech.glide:glide:$rootProject.glide", + glide_annotationProcessor : "com.github.bumptech.glide:compiler:$rootProject.glide", + integration : "com.github.bumptech.glide:okhttp3-integration:$rootProject.glide", + glide_recyclerview : "com.github.bumptech.glide:recyclerview-integration:$rootProject.glide" + ] + +} diff --git a/settings.gradle b/settings.gradle index e7b4def..5baabbf 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1 @@ -include ':app' +include ':app', ':common_module'