Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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));
Expand All @@ -112,8 +112,8 @@ public void onPause() {
static class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private final List<ListItemsGenerator.FeedListItem> mData;
private final TaboolaWidget mMiddleTaboolaWidget;
private final TaboolaWidget mBottomTaboolaWidget;
private final TaboolaWidget mMiddleTaboolaWidget;
private final TaboolaWidget mBottomTaboolaWidget;


RecyclerViewAdapter(TaboolaWidget taboolaWidget, TaboolaWidget taboolaWidgetBottom) {
Expand Down Expand Up @@ -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:
Expand All @@ -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);
}
Expand All @@ -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);
Expand All @@ -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);

}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<RecyclerView.ViewHolder> {

private final List<ListItemsGenerator.FeedListItem> 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<String, String> 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<String, String> 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);
}
}
}
13 changes: 13 additions & 0 deletions taboolasamples/src/main/res/layout/cust_item_layout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:orientation="vertical">


</RelativeLayout>

2 changes: 2 additions & 0 deletions taboolasamples/src/main/res/layout/fragment_rv_sample.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
android:layout_marginLeft="16dp"
android:layout_marginBottom="10dp"
android:layout_marginRight="16dp" />


Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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="wrap_content"
>

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:nestedScrollingEnabled="false"
android:focusableInTouchMode="true"
/>

</RelativeLayout>