【发布时间】:2018-08-03 11:25:57
【问题描述】:
我在片段视图中有一个回收器视图和一个横幅广告,当我向下滚动时,我想用回收器视图滚动广告。 我正在使用嵌套滚动,并且广告最初滚动良好。但是当我向下滚动它并且更多数据来自服务器时,它不会保持平滑并且有时会停止。 我已经尝试了堆栈上的每一个溶胶,但对我没有任何帮助。
这是一些代码。
fragment_all.xml:
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:id="@+id/nestedScroll"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:visibility="gone"
android:focusableInTouchMode="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
<android.support.v7.widget.RecyclerView
android:id="@+id/news_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:nestedScrollingEnabled="false"
/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
fragment_all.java:
nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if(v.getChildAt(v.getChildCount() - 1) != null) {
if ((scrollY >= (v.getChildAt(v.getChildCount() - 1).getMeasuredHeight() - v.getMeasuredHeight())) &&
scrollY > oldScrollY) {
visibleItemCount = layoutManager.getChildCount();
totalItemCount = layoutManager.getItemCount();
pastVisibleItems = ((LinearLayoutManager) recyclerView.getLayoutManager())
.findFirstVisibleItemPosition();
if ((visibleItemCount + pastVisibleItems) >=totalItemCount) {
if (!loadingInside) {
loadingInside = true;
postNo = postNo + 15;
getDataFromUrl(postNo);
}
}
}
}
}
});
注意:我尝试过类似的解决方案:
<app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:nestedScrollingEnabled="false"/>
【问题讨论】:
-
@NileshRathod 我在笔记中提到了我尝试过这样的解决方案。
-
一般情况下,请始终避免使用
wrap_content进行recyclerview。使用wrap_content消除了 recyclerview 一开始就试图为您提供的所有性能优势。如果您希望广告视图位于列表顶部,请尝试创建一个使用两种不同视图类型的回收器视图,并将广告作为第一项在回收器视图中。
标签: android scroll android-recyclerview smooth-scrolling android-nestedscrollview