mirror of https://github.com/FongMi/TV.git
parent
0f49438988
commit
f902612714
@ -0,0 +1,55 @@ |
||||
package com.fongmi.bear.ui.custom; |
||||
|
||||
import android.os.Handler; |
||||
import android.util.Log; |
||||
import android.view.View; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.recyclerview.widget.RecyclerView; |
||||
|
||||
public class Scroller extends RecyclerView.OnScrollListener { |
||||
|
||||
private final Callback callback; |
||||
private boolean loading; |
||||
private int page; |
||||
|
||||
public Scroller(Callback callback) { |
||||
this.callback = callback; |
||||
this.page = 1; |
||||
} |
||||
|
||||
@Override |
||||
public void onScrollStateChanged(@NonNull RecyclerView view, int newState) { |
||||
if (isLoading() || !isBottom(view) || newState != RecyclerView.SCROLL_STATE_IDLE) return; |
||||
setLoading(true); callback.onLoadMore(String.valueOf(++page)); |
||||
} |
||||
|
||||
private boolean isBottom(RecyclerView view) { |
||||
View lastChildView = view.getLayoutManager().getChildAt(view.getLayoutManager().getChildCount() - 1); |
||||
int lastChildBottom = lastChildView.getBottom(); |
||||
int recyclerBottom = view.getBottom() - view.getPaddingBottom(); |
||||
int lastPosition = view.getLayoutManager().getPosition(lastChildView); |
||||
return lastChildBottom == recyclerBottom && lastPosition == view.getLayoutManager().getItemCount() - 1; |
||||
} |
||||
|
||||
public void reset() { |
||||
page = 1; |
||||
} |
||||
|
||||
public boolean isLoading() { |
||||
return loading; |
||||
} |
||||
|
||||
private void setLoading(boolean loading) { |
||||
this.loading = loading; |
||||
} |
||||
|
||||
public void endLoading(boolean empty) { |
||||
new Handler().postDelayed(() -> setLoading(false), 1000); |
||||
if (empty) page--; |
||||
} |
||||
|
||||
public interface Callback { |
||||
void onLoadMore(String page); |
||||
} |
||||
} |
||||
@ -0,0 +1,32 @@ |
||||
package com.fongmi.bear.ui.presenter; |
||||
|
||||
import android.view.LayoutInflater; |
||||
import android.view.ViewGroup; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.leanback.widget.Presenter; |
||||
|
||||
import com.fongmi.bear.databinding.AdapterProgressBinding; |
||||
|
||||
public class ProgressPresenter extends Presenter { |
||||
|
||||
@Override |
||||
public Presenter.ViewHolder onCreateViewHolder(ViewGroup parent) { |
||||
return new ViewHolder(AdapterProgressBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false)); |
||||
} |
||||
|
||||
@Override |
||||
public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object object) { |
||||
} |
||||
|
||||
@Override |
||||
public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) { |
||||
} |
||||
|
||||
public static class ViewHolder extends Presenter.ViewHolder { |
||||
|
||||
public ViewHolder(@NonNull AdapterProgressBinding binding) { |
||||
super(binding.getRoot()); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,14 @@ |
||||
<?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" |
||||
android:orientation="vertical" |
||||
android:padding="16dp"> |
||||
|
||||
<include |
||||
layout="@layout/view_progress" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_gravity="center" /> |
||||
|
||||
</FrameLayout> |
||||
Loading…
Reference in new issue