android – NativeAdsExpress强制RecyclerView滚动以使NativeAd在首次加载时完全可见

前端之家收集整理的这篇文章主要介绍了android – NativeAdsExpress强制RecyclerView滚动以使NativeAd在首次加载时完全可见前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个奇怪的“问题”,或者可能是一个“功能”,我只是不知道,只要在我的RecyclerView中加载NativeAdExpress,如果只有部分NativeAd可见,它会强制RecyclerView滚动直到原生广告变得完全可见,这种行为导致列表在我滚动时继续跳跃.

我的布局主要是:

活动>带Tabs和ViewPager的AppBar>寻呼机中的每个页面都包含PullToRefresh,里面有一个RecyclerView,
RecyclerView有两种类型的项目(Article和NativeAdExpress).

更新:我猜测为什么会发生这种情况主要是因为原生广告在webview中表达渲染,并且这个webview获得焦点然后这会导致RecyclerView滚动到它,但这只是一个猜测

更新2:显然这是支持库中的一个问题. 24.0.0,这太过于up2date的成本:(

Heres是我的完整XML /布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <my.package.custom.views.CustomDrawerLayout
  3. android:id="@+id/drawer_layout"
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. xmlns:app="http://schemas.android.com/apk/res-auto"
  6. xmlns:tools="http://schemas.android.com/tools"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. android:fitsSystemWindows="true"
  10. app:layout_behavior="@string/appbar_scrolling_view_behavior"
  11. tools:openDrawer="end">
  12.  
  13. <include
  14. layout="@layout/app_bar_main"
  15. android:layout_width="match_parent"
  16. android:layout_height="match_parent"/>
  17.  
  18. <android.support.design.widget.NavigationView
  19. android:id="@+id/nav_view"
  20. style="@style/AlertDialog.AppCompat.Light"
  21. fontPath="fonts/fonts/DroidKufi-Regular.ttf"
  22. android:layout_width="wrap_content"
  23. android:layout_height="match_parent"
  24. android:layout_gravity="end"
  25. android:fitsSystemWindows="true"
  26. app:elevation="5px"
  27. app:headerLayout="@layout/nav_header"
  28. app:itemIconTint="@color/colorPrimary"
  29. app:itemTextColor="@color/contentColor"
  30. app:actionLayout="@layout/nav_item_layout"
  31. app:menu="@menu/drawer_menu"
  32. app:theme="@style/NavDrawerStyle"
  33. tools:openDrawer="end"
  34. />
  35.  
  36. </my.package.custom.views.CustomDrawerLayout>

其中“app_bar_main.xml”如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.design.widget.CoordinatorLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:fitsSystemWindows="true"
  9. tools:context=".ui.activities.ArticlesListActivity">
  10.  
  11. <android.support.design.widget.AppBarLayout
  12. android:id="@+id/appbar"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:gravity="end">
  16.  
  17. <android.support.v7.widget.Toolbar
  18. android:id="@+id/toolbar"
  19. android:layout_width="match_parent"
  20. android:layout_height="52dp"
  21. android:background="?attr/colorPrimary"
  22. android:gravity="end"
  23. app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
  24.  
  25. <RelativeLayout
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content"
  28. android:gravity="center_vertical"
  29. android:orientation="horizontal"
  30. android:paddingEnd="14dp"
  31. android:paddingStart="14dp">
  32.  
  33. <android.support.v7.widget.AppCompatImageButton
  34. android:id="@+id/ivCustomDrawable"
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:layout_alignParentEnd="true"
  38. android:layout_alignParentRight="true"
  39. android:layout_centerVertical="true"
  40. android:background="@color/transparent"
  41. android:tint="@color/white"
  42. />
  43.  
  44. <TextView
  45. android:id="@+id/view_title"
  46. android:visibility="gone"
  47. style="@style/SectionTitle"
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"
  50. android:layout_centerInParent="true"
  51. />
  52.  
  53. <android.support.v7.widget.AppCompatSpinner
  54. android:id="@+id/sources_spinner"
  55. android:gravity="center"
  56. android:layout_width="wrap_content"
  57. android:layout_height="wrap_content"
  58. android:layout_alignParentLeft="true"
  59. android:transitionName="@string/transition_splash_logo"
  60. app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
  61. tools:targetApi="lollipop"/>
  62. </RelativeLayout>
  63.  
  64. </android.support.v7.widget.Toolbar>
  65.  
  66. <android.support.design.widget.TabLayout
  67. android:id="@+id/tabs"
  68. style="@style/TabsStyle"
  69. android:layout_width="match_parent"
  70. android:layout_height="42dp"
  71. android:layout_gravity="bottom"
  72. android:layout_marginTop="0dp"
  73. android:transitionGroup="true"
  74. app:tabContentStart="0dp"
  75. app:tabGravity="fill"
  76. app:tabIndicatorColor="@color/white"
  77. app:tabIndicatorHeight="3dp"
  78. app:tabMode="scrollable"
  79. app:tabPaddingBottom="0dp"
  80. app:tabPaddingTop="0dp"
  81. app:tabTextAppearance="@style/TextAppearance.RegularTextFont"
  82. />
  83.  
  84. </android.support.design.widget.AppBarLayout>
  85.  
  86. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  87. xmlns:app="http://schemas.android.com/apk/res-auto"
  88. xmlns:tools="http://schemas.android.com/tools"
  89. android:layout_width="match_parent"
  90. android:layout_height="match_parent"
  91. app:layout_behavior="@string/appbar_scrolling_view_behavior"
  92. tools:context=".ui.activities.ArticlesListActivity"
  93. tools:showIn="@layout/activity_newsitem_list">
  94.  
  95. <android.support.v4.view.ViewPager
  96. android:id="@+id/viewpager"
  97. android:layout_width="match_parent"
  98. android:layout_height="match_parent"
  99. />
  100. </RelativeLayout>
  101. </android.support.design.widget.CoordinatorLayout>

最后我有2个View Types Item和NativeAdExpress:

NativeAdExpress ViewHolder如下:

  1. public class NativeExpressAdViewHolder extends BaseCardAdViewHolder {
  2. private final NativeExpressAdView view;
  3. private boolean loaded = false;
  4. private AdListener adListener;
  5. private WeakReference<Context> context;
  6.  
  7. public NativeExpressAdViewHolder(View itemView,String adId,Context context) {
  8. super(itemView);
  9. view = new NativeExpressAdView(context);
  10. view.setAdUnitId(adId);
  11. ((LinearLayout) itemView.findViewById(R.id.express_ad_holder)).addView(view);
  12. this.context = new WeakReference<>(context);
  13. }
  14.  
  15. public void loadAd(float cardWidthInDips) {
  16. if (!loaded && null != context.get() && !view.isLoading()) {
  17. int width = cardWidthInDips > 0 ? (int) cardWidthInDips : 330;
  18. if (view.getAdSize() == null) {
  19. view.setAdSize(new AdSize(width,330));
  20. view.setAdListener(new AdListener() {
  21. @Override
  22. public void onAdLoaded() {
  23. super.onAdLoaded();
  24. loaded = true;
  25. if (adListener != null) {
  26. adListener.onAdLoaded();
  27. }
  28. }
  29.  
  30. @Override
  31. public void onAdFailedToLoad(int i) {
  32. super.onAdFailedToLoad(i);
  33. if (adListener != null) {
  34. adListener.onAdFailedToLoad(i);
  35. }
  36. }
  37.  
  38. @Override
  39. public void onAdOpened() {
  40. super.onAdOpened();
  41. if (adListener != null) {
  42. adListener.onAdOpened();
  43. }
  44. }
  45. });
  46. }
  47. new Handler(context.get().getMainLooper()).post(new Runnable() {
  48. @Override
  49. public void run() {
  50. view.loadAd(new AdRequest.Builder().build());
  51. }
  52. });
  53. }
  54. }
  55.  
  56. public NativeExpressAdView getView() {
  57. return view;
  58. }
  59.  
  60. public void setAdListener(AdListener adListener) {
  61. this.adListener = adListener;
  62. }
  63.  
  64. @Override
  65. public void destroyAd() {
  66. if (view != null) {
  67. view.destroy();
  68. loaded = false;
  69. }
  70. }
  71. }

并使用自定义适配器创建广告,如下所示:

  1. private BaseCardViewHolder createNativeExpressAdViewHolder(ViewGroup parent) {
  2. View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ad_express,parent,false);
  3. final NativeExpressAdViewHolder viewHolder = new NativeExpressAdViewHolder(
  4. view,adManager.getNativeExpressAdId(),context.get()
  5. );
  6. adViewHolders.add(viewHolder);
  7. viewHolder.setAdListener(new AdListener() {
  8. @Override
  9. public void onAdFailedToLoad(int errorCode) {
  10. Log.i("ADS","onAdFailedToLoad " + errorCode);
  11. }
  12.  
  13. @Override
  14. public void onAdLoaded() {
  15. super.onAdLoaded();
  16. // Do something
  17. }
  18. });
  19. viewHolder.loadAd(cardWidthInDips);
  20.  
  21. return viewHolder;
  22. }

解决方法

我不明白你在代码中实际实现了RecyclerView的地方,但无论如何这可能是你的情况下可以工作的东西.

我有一个类似的问题,GridView总是在Fragment第一次加载时获得焦点,它也会自动向下滚动到GridView.

经过大量测试后,我终于在GridView的父布局上使用这一行停止了这种行为:

  1. android:descendantFocusability="blocksDescendants"

这一行基本上意味着来自父布局的所有后代将不再获得焦点.您可以在RecyclerView父布局上试用它.

猜你在找的Android相关文章