【发布时间】:2018-01-10 16:45:43
【问题描述】:
我面临两个问题:
- 滚动监听器不起作用
-
RecyclerView在附加到NestedScrollView时从不回收任何视图。它就像 ScrollView 内的线性布局。它使用大量内存并产生滞后。
我在回收站视图顶部附加了一个 youtube 播放器片段,因为我无法将片段放在回收站视图中。在我的代码中你可以看到有一个框架布局。
我的布局是这样的:
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nestedScroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_alignParentTop="true"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/youtube_layout"
android:visibility="visible"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
我想使用滚动监听器来加载更多项目 所以我尝试了以下方法:
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
Log.i(TAG, "onScrolled: ");
// bail out if scrolling upward or already loading data
if (dy < 0 || dataLoading.isDataLoading()) return;
final int visibleItemCount = recyclerView.getChildCount();
final int totalItemCount = layoutManager.getItemCount();
final int firstVisibleItem = layoutManager.findFirstVisibleItemPosition();
if ((totalItemCount - visibleItemCount) <= (firstVisibleItem )) {
onLoadMore();
}
}
但是layoutManager.findFirstVisibleItemPosition()==0 所以它不起作用,而且自从我设置了onScrolled never called twice
recyclerview.setNestedScrollingEnabled(false)
所以我在onBindView 中尝试过这样的
public void onBindViewHolder(RecyclerView.ViewHolder customViewHolder, int i) {
Log.w("d","inside bind view");
if(i>=getItemCount()-1 && !datamanager.isDataLoading()){
datamanager.loadmoreData();
}
但是recylerview在我开始滚动之前一次绑定了所有视图,所以这个方法也不起作用。
【问题讨论】:
标签: android android-recyclerview android-nestedscrollview onscrolllistener