ita*_*lah 5 android android-recyclerview
我正在RecyclerView通过添加addOnScrollListener功能来实现分页逻辑。
onScrolled当 RecyclerView 适配器加载dy值为 0 时,该方法被调用一次。但是,当列表滚动时到达其末尾时,该onScrolled方法不会被触发,而onScrollStateChanged正确触发。
这是我的适配器:
HistoryAdapter(Context context, ArrayList<HistoryItem> items, RecyclerView recyclerView) {
inflater = LayoutInflater.from(context);
this.items = items;
preferences = PreferenceManager.getDefaultSharedPreferences(context);
this.context = context;
this.recyclerView = recyclerView;
final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
Timber.w("onScrolled.. %s", dy);
if (dy > 0) {
totalItemCount = linearLayoutManager.getItemCount();
lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
Timber.e("totalItemCount: %s", totalItemCount);
Timber.e("lastVisibleItem: %s", lastVisibleItem);
if (!isLoading && totalItemCount <= (lastVisibleItem + 1)) {
Timber.i("End reached...");
if (onLoadMoreListener != null) {
//Log.i(TAG, "Episodes Adapter: onLoadMore calling...");
onLoadMoreListener.onLoadMore();
}
isLoading = true;
}
}
}
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
});
}
Run Code Online (Sandbox Code Playgroud)
回收商:
private void setupRecycler() {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
historyAdapter = new HistoryAdapter(getActivity(), historyItems, recyclerView);
recyclerView.setAdapter(historyAdapter);
historyAdapter.setClickListener(this);
historyAdapter.setOnLoadMoreListener(this);
historyAdapter.setErrorClickListener(this);
}
Run Code Online (Sandbox Code Playgroud)
我的回收站 xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/medium_text_color_blue"
android:padding="12dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start"
android:text="#"
android:textColor="@android:color/white"
android:textStyle="bold" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4.5"
android:gravity="start"
android:text="CLASS"
android:textAllCaps="true"
android:textColor="@android:color/white"
android:textStyle="bold" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:text="Date"
android:textAllCaps="true"
android:textColor="@android:color/white"
android:textStyle="bold" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:text="time"
android:textAllCaps="true"
android:textColor="@android:color/white"
android:textStyle="bold" />
</LinearLayout>
<FrameLayout
android:layout_below="@+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/no_reflection_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_gravity="center"
android:text="No Reflection History found!" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/history_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
目前已经张贴在StackOverflow上许多此类问题,但大多指向一个解决方案,是消除父ScrollView的RecyclerView被停止onScrolled方法被触发,然而这似乎不是我现在所面临的问题。
RecyclerView.onScrolled() 没有被调用
使用 scrollToPosition 时不调用 RecyclerView onScrolled
RecyclerView onScrolled 根本没有被触发
RecyclerView 未在底部调用 onScrolled
另一种方法是检查回收器是否可以垂直滚动,如果不能,则意味着它已到达底部。
尝试这个
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
if (!recyclerView.canScrollVertically(1)) {
// Load more
}
}
}
`
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1022 次 |
| 最近记录: |