Skip to content

Commit

Permalink
fix CategoryArticleFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
youlookwhat committed Jun 20, 2024
1 parent 66b487c commit bf00685
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 48 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId "com.example.jingbin.cloudreader"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 51
versionName "3.8.1"
versionCode 52
versionName "3.8.2"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
renderscriptTargetApi 19
renderscriptSupportModeEnabled true
Expand Down Expand Up @@ -102,7 +102,7 @@ dependencies {
implementation 'androidx.webkit:webkit:1.4.0'

// leakcanary
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.9.1'
// debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.14'
// bugly
implementation 'com.tencent.bugly:crashreport:3.4.4'
implementation 'com.tencent.bugly:nativecrashreport:3.9.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import android.content.Context;
import android.os.Bundle;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
Expand All @@ -17,6 +19,8 @@
import com.example.jingbin.cloudreader.viewmodel.wan.WanAndroidListViewModel;

import me.jingbin.bymvvm.base.BaseFragment;
import me.jingbin.bymvvm.databinding.LayoutLoadingEmptyBinding;
import me.jingbin.bymvvm.utils.CheckNetwork;
import me.jingbin.library.ByRecyclerView;
import me.jingbin.library.decoration.SpacesItemDecoration;

Expand Down Expand Up @@ -93,6 +97,9 @@ private void initRefreshView() {
bindingView.recyclerView.setRefreshHeaderView(new NeteaseRefreshHeaderView(activity));
bindingView.recyclerView.setLoadingMoreView(new NeteaseLoadMoreView(activity));
bindingView.recyclerView.setAdapter(mAdapter);
LayoutLoadingEmptyBinding emptyBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.layout_loading_empty, (ViewGroup) bindingView.recyclerView.getParent(), false);
emptyBinding.tvTipEmpty.setText(String.format("未找到与\"%s\"相关的内容", categoryName));
mAdapter.setPageEmptyView(emptyBinding.getRoot());

bindingView.recyclerView.setOnLoadMoreListener(new ByRecyclerView.OnLoadMoreListener() {
@Override
Expand All @@ -117,28 +124,14 @@ private void getHomeList() {
viewModel.getHomeArticleList(categoryId).observe(this, new Observer<HomeListBean>() {
@Override
public void onChanged(@Nullable HomeListBean homeListBean) {
if (homeListBean != null
&& homeListBean.getData() != null
&& homeListBean.getData().getDatas() != null
&& homeListBean.getData().getDatas().size() > 0) {
showContentView();
if (viewModel.getPage() == 0) {
mAdapter.setNewData(homeListBean.getData().getDatas());
} else {
mAdapter.addData(homeListBean.getData().getDatas());
bindingView.recyclerView.loadMoreComplete();
}
} else {
if (viewModel.getPage() == 0) {
if (homeListBean != null) {
showEmptyView(String.format("未找到与\"%s\"相关的内容", categoryName));
} else {
showError();
}
} else {
bindingView.recyclerView.loadMoreEnd();
}
if (!CheckNetwork.isNetworkConnected(activity)) {
showError();
return;
}
showContentView();
if (mAdapter == null) return;
// 如果在onDestroyView时没有执行viewModel.clear(),且将mAdapter=null,则有可能在数据回来时,mAdapter设置为Null了。
mAdapter.setPageData(viewModel.getPage() == 0, (homeListBean != null && homeListBean.getData() != null && homeListBean.getData().getDatas() != null) ? homeListBean.getData().getDatas() : null);
}
});
}
Expand All @@ -155,7 +148,7 @@ public void onDestroyView() {
mIsFirst = true;
viewModel.setPage(0);
if (mAdapter != null) {
mAdapter.getData().clear();
mAdapter.clear();
mAdapter = null;
}
bindingView.recyclerView.destroy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ public void setState(int state) {
llMoreLoading.setVisibility(VISIBLE);
tvNoMore.setVisibility(GONE);
tvMoreFailed.setVisibility(GONE);
if (!mAnimationDrawable.isRunning()) {
if (mAnimationDrawable == null) {
ImageView mIvProgress = findViewById(R.id.iv_progress);
mAnimationDrawable = (AnimationDrawable) mIvProgress.getDrawable();
}
if (mAnimationDrawable != null && !mAnimationDrawable.isRunning()) {
mAnimationDrawable.start();
}
break;
Expand All @@ -62,24 +66,27 @@ public void setState(int state) {
tvNoMore.setVisibility(GONE);
tvMoreFailed.setVisibility(GONE);
this.setVisibility(View.GONE);
if (mAnimationDrawable.isRunning()) {
if (mAnimationDrawable != null && mAnimationDrawable.isRunning()) {
mAnimationDrawable.stop();
mAnimationDrawable = null;
}
break;
case STATE_NO_MORE:
tvNoMore.setVisibility(VISIBLE);
llMoreLoading.setVisibility(GONE);
tvMoreFailed.setVisibility(GONE);
if (mAnimationDrawable.isRunning()) {
if (mAnimationDrawable != null && mAnimationDrawable.isRunning()) {
mAnimationDrawable.stop();
mAnimationDrawable = null;
}
break;
case STATE_FAILURE:
tvMoreFailed.setVisibility(VISIBLE);
llMoreLoading.setVisibility(GONE);
tvNoMore.setVisibility(GONE);
if (mAnimationDrawable.isRunning()) {
if (mAnimationDrawable != null && mAnimationDrawable.isRunning()) {
mAnimationDrawable.stop();
mAnimationDrawable = null;
}
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ public void setState(int state) {
return;
}

if (mAnimationDrawable.isRunning()) {
if (mAnimationDrawable != null && mAnimationDrawable.isRunning()) {
mAnimationDrawable.stop();
mAnimationDrawable = null;
}
tvRefreshTip.setVisibility(VISIBLE);
if (state == STATE_REFRESHING) {
Expand Down Expand Up @@ -127,7 +128,10 @@ public void setState(int state) {
break;
case STATE_REFRESHING:
tvRefreshTip.setText(R.string.custom_refreshing);
if (!mAnimationDrawable.isRunning()) {
if (mAnimationDrawable == null && mProgress != null) {
mAnimationDrawable = (AnimationDrawable) mProgress.getDrawable();
}
if (mAnimationDrawable != null && !mAnimationDrawable.isRunning()) {
mAnimationDrawable.start();
}
break;
Expand Down
26 changes: 11 additions & 15 deletions app/src/main/res/layout/fragment_category_article.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<FrameLayout xmlns:tools="http://schemas.android.com/tools"
<me.jingbin.library.ByRecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.jingbin.cloudreader.ui.wan.child.CategoryArticleFragment">

<me.jingbin.library.ByRecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarSize="4dp"
android:scrollbarThumbVertical="@color/tabBackground"
android:scrollbars="vertical"
tools:layoutManager="LinearLayoutManager"
tools:listitem="@layout/item_category_article" />

</FrameLayout>
android:scrollbarSize="4dp"
android:scrollbarThumbVertical="@color/tabBackground"
android:scrollbars="vertical"
tools:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/item_category_article"
tools:orientation="vertical" />
</layout>
2 changes: 1 addition & 1 deletion bymvvm/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,5 @@ dependencies {
// 高斯模糊和圆角等 要求minSdkVersion = 21
api 'jp.wasabeef:glide-transformations:4.3.0'

api 'com.github.youlookwhat:ByRecyclerView:1.3.7'
api 'com.github.youlookwhat:ByRecyclerView:1.4.1'
}
13 changes: 12 additions & 1 deletion bymvvm/src/main/java/me/jingbin/bymvvm/base/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ protected void showLoading() {
loadingView.setVisibility(View.VISIBLE);
}
// 开始动画
if (!mAnimationDrawable.isRunning()) {
if (mAnimationDrawable == null && loadingView != null) {
ImageView img = loadingView.findViewById(R.id.img_progress);
mAnimationDrawable = (AnimationDrawable) img.getDrawable();
}
if (mAnimationDrawable != null && !mAnimationDrawable.isRunning()) {
mAnimationDrawable.start();
}
if (bindingView.getRoot().getVisibility() != View.GONE) {
Expand All @@ -146,6 +150,7 @@ protected void showContentView() {
// 停止动画
if (mAnimationDrawable.isRunning()) {
mAnimationDrawable.stop();
mAnimationDrawable = null;
}
if (errorView != null) {
errorView.setVisibility(View.GONE);
Expand All @@ -162,6 +167,7 @@ protected void showError() {
// 停止动画
if (mAnimationDrawable.isRunning()) {
mAnimationDrawable.stop();
mAnimationDrawable = null;
}
if (errorView == null) {
ViewStub viewStub = (ViewStub) findViewById(R.id.vs_error_refresh);
Expand Down Expand Up @@ -227,6 +233,11 @@ public void onDestroy() {
// clear 和 dispose的区别是: disposed = true;
this.mCompositeDisposable.clear();
}
// 停止动画
if (mAnimationDrawable != null && mAnimationDrawable.isRunning()) {
mAnimationDrawable.stop();
mAnimationDrawable = null;
}
}

public void removeDisposable() {
Expand Down
28 changes: 28 additions & 0 deletions bymvvm/src/main/java/me/jingbin/bymvvm/base/BaseFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ protected void showLoading() {
loadingView.setVisibility(View.VISIBLE);
}
// 开始动画
if (mAnimationDrawable == null && loadingView != null) {
ImageView img = loadingView.findViewById(R.id.img_progress);
mAnimationDrawable = (AnimationDrawable) img.getDrawable();
}
if (mAnimationDrawable != null && !mAnimationDrawable.isRunning()) {
mAnimationDrawable.start();
}
Expand Down Expand Up @@ -173,6 +177,7 @@ protected void showContentView() {
// 停止动画
if (mAnimationDrawable != null && mAnimationDrawable.isRunning()) {
mAnimationDrawable.stop();
mAnimationDrawable = null;
}
if (errorView != null) {
errorView.setVisibility(View.GONE);
Expand Down Expand Up @@ -207,6 +212,7 @@ public void onClick(View v) {
// 停止动画
if (mAnimationDrawable != null && mAnimationDrawable.isRunning()) {
mAnimationDrawable.stop();
mAnimationDrawable = null;
}
if (bindingView.getRoot().getVisibility() != View.GONE) {
bindingView.getRoot().setVisibility(View.GONE);
Expand All @@ -233,6 +239,7 @@ protected void showEmptyView(String text) {
// 停止动画
if (mAnimationDrawable != null && mAnimationDrawable.isRunning()) {
mAnimationDrawable.stop();
mAnimationDrawable = null;
}
if (errorView != null) {
errorView.setVisibility(View.GONE);
Expand All @@ -249,12 +256,33 @@ public void addSubscription(Disposable disposable) {
this.mCompositeDisposable.add(disposable);
}

@Override
public void onDestroyView() {
super.onDestroyView();
if (viewModel != null && viewModel instanceof BaseViewModel) {
((BaseViewModel) viewModel).onCleared();
}
// 停止动画
if (mAnimationDrawable != null && mAnimationDrawable.isRunning()) {
mAnimationDrawable.stop();
mAnimationDrawable = null;
}
}

@Override
public void onDestroy() {
super.onDestroy();
if (this.mCompositeDisposable != null && !mCompositeDisposable.isDisposed()) {
this.mCompositeDisposable.clear();
}
if (viewModel != null && viewModel instanceof BaseViewModel) {
((BaseViewModel) viewModel).onCleared();
}
// 停止动画
if (mAnimationDrawable != null && mAnimationDrawable.isRunning()) {
mAnimationDrawable.stop();
mAnimationDrawable = null;
}
}

public void removeDisposable() {
Expand Down

0 comments on commit bf00685

Please sign in to comment.