Use withLoadStateFooter to show load state.

This commit is contained in:
Alex Ning 2021-09-06 22:59:05 +08:00
parent 1f02fcd0da
commit a725f7eb3a
3 changed files with 146 additions and 2 deletions

View File

@ -0,0 +1,72 @@
package ml.docilealligator.infinityforreddit.adapters;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.paging.LoadState;
import androidx.paging.LoadStateAdapter;
import androidx.recyclerview.widget.RecyclerView;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
public class Paging3LoadingStateAdapter extends LoadStateAdapter<Paging3LoadingStateAdapter.LoadStateViewHolder> {
private CustomThemeWrapper mCustomThemeWrapper;
private int mErrorStringId;
private View.OnClickListener mRetryCallback;
public Paging3LoadingStateAdapter(CustomThemeWrapper customThemeWrapper, int errorStringId, View.OnClickListener retryCallback) {
this.mCustomThemeWrapper = customThemeWrapper;
this.mErrorStringId = errorStringId;
this.mRetryCallback = retryCallback;
}
@Override
public void onBindViewHolder(@NonNull LoadStateViewHolder loadStateViewHolder, @NonNull LoadState loadState) {
loadStateViewHolder.bind(loadState);
}
@NonNull
@Override
public LoadStateViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, @NonNull LoadState loadState) {
return new LoadStateViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_paging_3_load_state, viewGroup, false), mRetryCallback);
}
class LoadStateViewHolder extends RecyclerView.ViewHolder {
private ProgressBar mProgressBar;
private RelativeLayout mErrorView;
private TextView mErrorMsg;
private Button mRetry;
LoadStateViewHolder(@NonNull View itemView, @NonNull View.OnClickListener retryCallback) {
super(itemView);
mProgressBar = itemView.findViewById(R.id.progress_bar_item_paging_3_load_state);
mErrorView = itemView.findViewById(R.id.error_view_relative_layout_item_paging_3_load_state);
mErrorMsg = itemView.findViewById(R.id.error_text_view_item_paging_3_load_state);
mRetry = itemView.findViewById(R.id.retry_button_item_paging_3_load_state);
mErrorMsg.setText(mErrorStringId);
mErrorMsg.setTextColor(mCustomThemeWrapper.getPrimaryTextColor());
mRetry.setOnClickListener(retryCallback);
mErrorView.setOnClickListener(retryCallback);
Log.i("asfasdf", "asdf ");
}
public void bind(LoadState loadState) {
Log.i("asfasdf", "asdf bind");
mProgressBar.setVisibility(loadState instanceof LoadState.Loading
? View.VISIBLE : View.GONE);
mErrorView.setVisibility(loadState instanceof LoadState.Error
? View.VISIBLE : View.GONE);
}
}
}

View File

@ -37,6 +37,7 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.res.ResourcesCompat; import androidx.core.content.res.ResourcesCompat;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import androidx.paging.LoadState;
import androidx.recyclerview.widget.ItemTouchHelper; import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearSmoothScroller; import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
@ -78,6 +79,7 @@ import ml.docilealligator.infinityforreddit.SortType;
import ml.docilealligator.infinityforreddit.activities.BaseActivity; import ml.docilealligator.infinityforreddit.activities.BaseActivity;
import ml.docilealligator.infinityforreddit.activities.FilteredPostsActivity; import ml.docilealligator.infinityforreddit.activities.FilteredPostsActivity;
import ml.docilealligator.infinityforreddit.activities.ViewSubredditDetailActivity; import ml.docilealligator.infinityforreddit.activities.ViewSubredditDetailActivity;
import ml.docilealligator.infinityforreddit.adapters.Paging3LoadingStateAdapter;
import ml.docilealligator.infinityforreddit.adapters.PostRecyclerViewAdapter; import ml.docilealligator.infinityforreddit.adapters.PostRecyclerViewAdapter;
import ml.docilealligator.infinityforreddit.asynctasks.LoadSubredditIcon; import ml.docilealligator.infinityforreddit.asynctasks.LoadSubredditIcon;
import ml.docilealligator.infinityforreddit.asynctasks.LoadUserData; import ml.docilealligator.infinityforreddit.asynctasks.LoadUserData;
@ -214,6 +216,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
private boolean isLazyModePaused = false; private boolean isLazyModePaused = false;
private boolean hasPost = false; private boolean hasPost = false;
private boolean isShown = false; private boolean isShown = false;
private boolean isInitialRefreshingStarted = false;
private boolean savePostFeedScrolledPosition; private boolean savePostFeedScrolledPosition;
private boolean rememberMutingOptionInPostFeed; private boolean rememberMutingOptionInPostFeed;
private Boolean masterMutingOption; private Boolean masterMutingOption;
@ -257,8 +260,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
isShown = true; isShown = true;
if (mPostRecyclerView.getAdapter() != null) { if (mAdapter != null) {
((PostRecyclerViewAdapter) mPostRecyclerView.getAdapter()).setCanStartActivity(true); mAdapter.setCanStartActivity(true);
} }
if (isInLazyMode) { if (isInLazyMode) {
resumeLazyMode(false); resumeLazyMode(false);
@ -1179,6 +1182,30 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
private void bindPostViewModel() { private void bindPostViewModel() {
mPostViewModel.getPosts().observe(getViewLifecycleOwner(), posts -> mAdapter.submitData(getViewLifecycleOwner().getLifecycle(), posts)); mPostViewModel.getPosts().observe(getViewLifecycleOwner(), posts -> mAdapter.submitData(getViewLifecycleOwner().getLifecycle(), posts));
mAdapter.addLoadStateListener(combinedLoadStates -> {
LoadState refreshLoadState = combinedLoadStates.getRefresh();
LoadState appendLoadState = combinedLoadStates.getAppend();
mSwipeRefreshLayout.setRefreshing(refreshLoadState instanceof LoadState.Loading);
if (refreshLoadState instanceof LoadState.NotLoading) {
if (refreshLoadState.getEndOfPaginationReached() && mAdapter.getItemCount() < 1) {
if (isInLazyMode) {
stopLazyMode();
}
mFetchPostInfoLinearLayout.setOnClickListener(null);
showErrorView(R.string.no_posts);
}
} else if (refreshLoadState instanceof LoadState.Error) {
mFetchPostInfoLinearLayout.setOnClickListener(view -> refresh());
showErrorView(R.string.load_posts_error);
}
return null;
});
mPostRecyclerView.setAdapter(mAdapter.withLoadStateFooter(new Paging3LoadingStateAdapter(mCustomThemeWrapper, R.string.load_more_posts_error,
view -> mAdapter.retry())));
/*mPostViewModel.hasPost().observe(getViewLifecycleOwner(), hasPost -> { /*mPostViewModel.hasPost().observe(getViewLifecycleOwner(), hasPost -> {
this.hasPost = hasPost; this.hasPost = hasPost;
mSwipeRefreshLayout.setRefreshing(false); mSwipeRefreshLayout.setRefreshing(false);

View File

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProgressBar
android:id="@+id/progress_bar_item_paging_3_load_state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="8dp"
android:layout_marginBottom="16dp" />
<RelativeLayout
android:id="@+id/error_view_relative_layout_item_paging_3_load_state"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp">
<TextView
android:id="@+id/error_text_view_item_paging_3_load_state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_toStartOf="@id/retry_button_item_paging_3_load_state"
android:textSize="?attr/font_18"
android:fontFamily="?attr/font_family" />
<Button
android:id="@+id/retry_button_item_paging_3_load_state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:text="@string/retry"
android:textSize="?attr/font_default"
android:fontFamily="?attr/font_family" />
</RelativeLayout>
</FrameLayout>