From 5a33bc2d205f00436cb319c40cf9f1fa8fb93a77 Mon Sep 17 00:00:00 2001 From: xwdz Date: Mon, 4 Nov 2019 18:21:57 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B2=97=E7=95=A5=E6=B7=BB=E5=8A=A0=E9=9C=80?= =?UTF-8?q?=E6=B1=82=E6=8F=8F=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ddleArticleInsideRecyclerViewFragment.java | 36 +++-- .../sdksamples/test/NewsH5ViewAdapter.java | 137 ++++++++++++++++++ .../src/main/res/layout/cust_item_layout.xml | 13 ++ .../main/res/layout/fragment_rv_sample.xml | 2 + .../layout/recycler_layout_without_margin.xml | 17 +++ 5 files changed, 192 insertions(+), 13 deletions(-) create mode 100644 taboolasamples/src/main/java/com/taboola/android/sdksamples/test/NewsH5ViewAdapter.java create mode 100644 taboolasamples/src/main/res/layout/cust_item_layout.xml create mode 100644 taboolasamples/src/main/res/layout/recycler_layout_without_margin.xml diff --git a/taboolasamples/src/main/java/com/taboola/android/sdksamples/sdk_via_native/FeedWithMiddleArticleInsideRecyclerViewFragment.java b/taboolasamples/src/main/java/com/taboola/android/sdksamples/sdk_via_native/FeedWithMiddleArticleInsideRecyclerViewFragment.java index c84ebbb..4231779 100644 --- a/taboolasamples/src/main/java/com/taboola/android/sdksamples/sdk_via_native/FeedWithMiddleArticleInsideRecyclerViewFragment.java +++ b/taboolasamples/src/main/java/com/taboola/android/sdksamples/sdk_via_native/FeedWithMiddleArticleInsideRecyclerViewFragment.java @@ -25,7 +25,7 @@ public class FeedWithMiddleArticleInsideRecyclerViewFragment extends Fragment implements GlobalNotificationReceiver.OnGlobalNotificationsListener { - private static final String TAG = "FeedWithMiddleArticle"; + private static final String TAG = "FeedWithMiddleArticle"; private static final String TABOOLA_VIEW_ID = "123456"; private static TaboolaWidget mMiddleTaboolaWidget; @@ -45,7 +45,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, static TaboolaWidget createTaboolaWidget(Context context, boolean infiniteWidget) { TaboolaWidget taboolaWidget = new TaboolaWidget(context); - int height = infiniteWidget ? SdkDetailsHelper.getDisplayHeight(context) * 2 : ViewGroup.LayoutParams.WRAP_CONTENT; + int height = infiniteWidget ? SdkDetailsHelper.getDisplayHeight(context) * 2 : ViewGroup.LayoutParams.WRAP_CONTENT; taboolaWidget.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height)); return taboolaWidget; } @@ -88,7 +88,7 @@ private static void buildBottomArticleWidget(TaboolaWidget taboolaWidget) { @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); - RecyclerView recyclerView = view.findViewById(R.id.feed_rv); + RecyclerView recyclerView = view.findViewById(R.id.feed_rv); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(view.getContext()); recyclerView.setLayoutManager(linearLayoutManager); recyclerView.setAdapter(new RecyclerViewAdapter(mMiddleTaboolaWidget, mBottomTaboolaWidget)); @@ -112,8 +112,8 @@ public void onPause() { static class RecyclerViewAdapter extends RecyclerView.Adapter { private final List mData; - private final TaboolaWidget mMiddleTaboolaWidget; - private final TaboolaWidget mBottomTaboolaWidget; + private final TaboolaWidget mMiddleTaboolaWidget; + private final TaboolaWidget mBottomTaboolaWidget; RecyclerViewAdapter(TaboolaWidget taboolaWidget, TaboolaWidget taboolaWidgetBottom) { @@ -144,13 +144,16 @@ private ListItemsGenerator.FeedListItem getItem(int position) { @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { - switch (viewType) { + // TODO 需求 + // 布局大概是 RecyclerView 里面的子item也是一个RecycierView, 然后加载SDK 请求信息流 ,可重新写一个demo 复现下 + View customParent = LayoutInflater.from(parent.getContext()).inflate(R.layout.cust_item_layout, parent, false); + switch (viewType) { case ListItemsGenerator.FeedListItem.ItemType.TABOOLA_MID_ITEM: - return new ViewHolderTaboola(mMiddleTaboolaWidget); + return new ViewHolderTaboola((ViewGroup) customParent, mMiddleTaboolaWidget); case ListItemsGenerator.FeedListItem.ItemType.TABOOLA_ITEM: - return new ViewHolderTaboola(mBottomTaboolaWidget); + return new ViewHolderTaboola((ViewGroup) customParent, mBottomTaboolaWidget); default: case ListItemsGenerator.FeedListItem.ItemType.RANDOM_ITEM: @@ -165,9 +168,9 @@ public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int positi ListItemsGenerator.FeedListItem item = getItem(position); if (item.type == ListItemsGenerator.FeedListItem.ItemType.RANDOM_ITEM) { - RandomImageViewHolder vh = (RandomImageViewHolder) holder; + RandomImageViewHolder vh = (RandomImageViewHolder) holder; ListItemsGenerator.RandomItem randomItem = (ListItemsGenerator.RandomItem) item; - final ImageView imageView = vh.imageView; + final ImageView imageView = vh.imageView; imageView.setBackgroundColor(randomItem.color); vh.textView.setText(randomItem.randomText); } @@ -176,7 +179,7 @@ public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int positi static class RandomImageViewHolder extends RecyclerView.ViewHolder { private final ImageView imageView; - private final TextView textView; + private final TextView textView; RandomImageViewHolder(View view) { super(view); @@ -187,8 +190,15 @@ static class RandomImageViewHolder extends RecyclerView.ViewHolder { static class ViewHolderTaboola extends RecyclerView.ViewHolder { - ViewHolderTaboola(View view) { - super(view); + ViewHolderTaboola(ViewGroup viewGroup, View widget) { + super(viewGroup); + + if (widget.getParent() != null) { + ((ViewGroup) widget.getParent()).removeView(widget); + } + + viewGroup.addView(widget); + } } } diff --git a/taboolasamples/src/main/java/com/taboola/android/sdksamples/test/NewsH5ViewAdapter.java b/taboolasamples/src/main/java/com/taboola/android/sdksamples/test/NewsH5ViewAdapter.java new file mode 100644 index 0000000..6b5886f --- /dev/null +++ b/taboolasamples/src/main/java/com/taboola/android/sdksamples/test/NewsH5ViewAdapter.java @@ -0,0 +1,137 @@ +package com.taboola.android.sdksamples.test; + +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.LinearLayout; + +import com.taboola.android.TaboolaWidget; +import com.taboola.android.sdksamples.R; +import com.taboola.android.sdksamples.sdk_via_native.ListItemsGenerator; +import com.taboola.android.utils.SdkDetailsHelper; + +import java.util.HashMap; +import java.util.List; + +/** + * @author xingwei.huang (xwdz9989@gmail.com) + * @since v1.0.0 + */ +public class NewsH5ViewAdapter extends RecyclerView.Adapter { + + private final List mData; + + private TaboolaWidget mMiddleTaboolaWidget; + private TaboolaWidget mBottomTaboolaWidget; + + private static final String TABOOLA_VIEW_ID = "123456"; + + + private static void buildBottomArticleWidget(TaboolaWidget taboolaWidget) { + taboolaWidget + .setPublisher("sdk-tester") + .setPageType("article") + .setPageUrl("https://blog.taboola.com") + .setPlacement("Feed without video") + .setMode("thumbs-feed-01") + .setTargetType("mix") + .setViewId(TABOOLA_VIEW_ID); + + taboolaWidget.setInterceptScroll(true); + + HashMap optionalPageCommands = new HashMap<>(); + optionalPageCommands.put("useOnlineTemplate", "true"); + taboolaWidget.setExtraProperties(optionalPageCommands); + taboolaWidget.fetchContent(); + } + + static TaboolaWidget createTaboolaWidget(Context context, boolean infiniteWidget) { + TaboolaWidget taboolaWidget = new TaboolaWidget(context); + int height = infiniteWidget ? SdkDetailsHelper.getDisplayHeight(context) * 2 : ViewGroup.LayoutParams.WRAP_CONTENT; + taboolaWidget.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height)); + return taboolaWidget; + } + + private static void buildMiddleArticleWidget(TaboolaWidget taboolaWidget) { + taboolaWidget + .setPublisher("sdk-tester") + .setPageType("article") + .setPageUrl("https://blog.taboola.com") + .setPlacement("Mid Article") + .setMode("alternating-widget-without-video-1-on-1") + .setTargetType("mix") + .setViewId(TABOOLA_VIEW_ID); + + HashMap optionalPageCommands = new HashMap<>(); + optionalPageCommands.put("useOnlineTemplate", "true"); + taboolaWidget.setExtraProperties(optionalPageCommands); + taboolaWidget.fetchContent(); + } + + public NewsH5ViewAdapter(Context context, TaboolaWidget taboolaWidget, TaboolaWidget taboolaWidgetBottom) { + mData = ListItemsGenerator.getGeneratedDataForWidgetDynamic(true); + mMiddleTaboolaWidget = createTaboolaWidget(context, false); + mBottomTaboolaWidget = createTaboolaWidget(context, true); + buildMiddleArticleWidget(mMiddleTaboolaWidget); + } + + + @Override + public int getItemViewType(int position) { + ListItemsGenerator.FeedListItem item = getItem(position); + return item.type; + } + + + @Override + public int getItemCount() { + return mData.size(); + } + + @NonNull + private ListItemsGenerator.FeedListItem getItem(int position) { + return mData.get(position); + } + + + @NonNull + @Override + public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View customParent = LayoutInflater.from(parent.getContext()).inflate(R.layout.cust_item_layout, parent, false); + + switch (viewType) { + case ListItemsGenerator.FeedListItem.ItemType.TABOOLA_MID_ITEM: + return new ViewHolderTaboola((ViewGroup) customParent,mMiddleTaboolaWidget); + + case ListItemsGenerator.FeedListItem.ItemType.TABOOLA_ITEM: + return new ViewHolderTaboola((ViewGroup) customParent,mBottomTaboolaWidget); + default: + return new ViewHolderTaboola((ViewGroup) customParent,mBottomTaboolaWidget); + } +// return new ViewHolderTaboola(new TextView(parent.getContext())); + } + + + @Override + public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { + ListItemsGenerator.FeedListItem item = getItem(position); + + } + + + static class ViewHolderTaboola extends RecyclerView.ViewHolder { + + ViewHolderTaboola(ViewGroup viewGroup,View widget) { + super(viewGroup); + + if (widget.getParent() != null) { + ((ViewGroup) widget.getParent()).removeView(widget); + } + + viewGroup.addView(widget); + } + } +} diff --git a/taboolasamples/src/main/res/layout/cust_item_layout.xml b/taboolasamples/src/main/res/layout/cust_item_layout.xml new file mode 100644 index 0000000..0b13050 --- /dev/null +++ b/taboolasamples/src/main/res/layout/cust_item_layout.xml @@ -0,0 +1,13 @@ + + + + + + diff --git a/taboolasamples/src/main/res/layout/fragment_rv_sample.xml b/taboolasamples/src/main/res/layout/fragment_rv_sample.xml index 3e09cec..fb82cf5 100644 --- a/taboolasamples/src/main/res/layout/fragment_rv_sample.xml +++ b/taboolasamples/src/main/res/layout/fragment_rv_sample.xml @@ -6,3 +6,5 @@ android:layout_marginLeft="16dp" android:layout_marginBottom="10dp" android:layout_marginRight="16dp" /> + + diff --git a/taboolasamples/src/main/res/layout/recycler_layout_without_margin.xml b/taboolasamples/src/main/res/layout/recycler_layout_without_margin.xml new file mode 100644 index 0000000..237960f --- /dev/null +++ b/taboolasamples/src/main/res/layout/recycler_layout_without_margin.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file