I am implementing Facebook Native Ads in RecyclerView. The ads load fine, but the ads are not clickable. Other general items in my RecyclerView are clickable as i implemented OnClickListener for them. How do i get the facebook ads clickable? Can someone help me with this?
Here is my code:
private class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int VIEW_ITEM_TYPE = 0;
private static final int VIEW_FACEBOOK_AD_TYPE = 1;
Context context;
public RecyclerViewAdapter(Context context) {
this.context = context;
}
#Override
public int getItemViewType(int position) {
if (listItems.get(position).isAd())
return VIEW_FACEBOOK_AD_TYPE;
else
return VIEW_ITEM_TYPE;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_ITEM_TYPE) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_recyclerview, parent, false);
return new CustomViewHolder(v);
} else if (viewType == VIEW_FACEBOOK_AD_TYPE) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_recyclerview_dashboard_fb_ad, parent, false);
return new FacebookAdViewHolder(v);
}
return null;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Video video = listItems.get(position);
if (video.isAd()) {
FacebookAdViewHolder facebookAdViewHolder = (FacebookAdViewHolder) holder;
View adView = NativeAdView.render(context, nativeAd, NativeAdView.Type.HEIGHT_300);
List<View> clickableViews = new ArrayList<>();
clickableViews.add(adView);
clickableViews.add(facebookAdViewHolder.nativeAdContainer);
nativeAd.registerViewForInteraction(facebookAdViewHolder.nativeAdContainer, clickableViews);
facebookAdViewHolder.nativeAdContainer.addView(adView);
} else {
CustomViewHolder customViewHolder = (CustomViewHolder) holder;
Glide.with(context).load(URL_PART_1 + video.getVideoId() + URL_PART_2).into(customViewHolder.imageView);
customViewHolder.textViewTitle.setText(video.getTitle());
}
}
#Override
public int getItemCount() {
return listItems == null ? 0 : listItems.size();
}
private class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView imageView;
TextView textViewTitle;
public CustomViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
imageView = itemView.findViewById(R.id.imageView);
textViewTitle = itemView.findViewById(R.id.textView_title);
}
#Override
public void onClick(View v) {
...
....
.....
// Un-necessary code
}
}
private class FacebookAdViewHolder extends RecyclerView.ViewHolder {
LinearLayout nativeAdContainer;
public FacebookAdViewHolder(View facebookAd) {
super(facebookAd);
nativeAdContainer = facebookAd.findViewById(R.id.native_ad_container);
}
}
}
Ok, for those who are looking to integrate Facebook Ads to an android app using RecyclerView, here is the solution:
Here is a link to the sample, which has the implementation:
https://origincache.facebook.com/developers/resources/?id=audience-network-sdk-4.25.0.zip
Also, for quick code snippets:
item_recylerview_fb_ad.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<ImageView
android:id="#+id/native_ad_icon"
android:layout_width="50dp"
android:layout_height="50dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="5dp">
<TextView
android:id="#+id/native_ad_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:textColor="#android:color/black"
android:textSize="18sp" />
<TextView
android:id="#+id/native_ad_body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="2"
android:textColor="#android:color/black"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
<com.facebook.ads.MediaView
android:id="#+id/native_ad_media"
android:layout_width="match_parent"
android:layout_height="240dp"
android:gravity="center" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<TextView
android:id="#+id/native_ad_social_context"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:ellipsize="end"
android:lines="2"
android:paddingRight="5dp"
android:textColor="#android:color/black"
android:textSize="15sp" />
<Button
android:id="#+id/native_ad_call_to_action"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:textSize="16sp" />
</LinearLayout>
Activity.java
// Activity should implement NativeAdsManager.Listener
public class MainActivity extends AppCompatActivity implements
NativeAdsManager.Listener
// Listeners for NativeAdsManager
#Override
public void onAdsLoaded() {
mRecyclerViewAdapter.notifyDataSetChanged();
}
#Override
public void onAdError(AdError error) {
}
// Probably in onCreate()
NativeAdsManager mNativeAdsManager;
String placement_id = "Your ad placement id";
mNativeAdsManager = new NativeAdsManager(this, placement_id, 5);
mNativeAdsManager.loadAds();
mNativeAdsManager.setListener(this);
// Your CustomViewHolder class
private class FacebookAdViewHolder extends RecyclerView.ViewHolder {
MediaView mvAdMedia;
ImageView ivAdIcon;
TextView tvAdTitle;
TextView tvAdBody;
TextView tvAdSocialContext;
Button btnAdCallToAction;
public FacebookAdViewHolder(View facebookAd) {
super(facebookAd);
mvAdMedia = facebookAd.findViewById(R.id.native_ad_media);
tvAdTitle = facebookAd.findViewById(R.id.native_ad_title);
tvAdBody = facebookAd.findViewById(R.id.native_ad_body);
tvAdSocialContext = facebookAd.findViewById(R.id.native_ad_social_context);
btnAdCallToAction = facebookAd.findViewById(R.id.native_ad_call_to_action);
ivAdIcon = facebookAd.findViewById(R.id.native_ad_icon);
}
}
// Finally your onBindViewHolder method
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Video video = listItems.get(position);
if (holder.getItemViewType() == VIEW_FACEBOOK_AD_TYPE) {
NativeAd ad;
if (mAdItems.size() > position / 3) {
ad = mAdItems.get(position / 3);
} else {
ad = mNativeAdsManager.nextNativeAd();
mAdItems.add(ad);
}
if (ad != null) {
FacebookAdViewHolder facebookAdViewHolder = (FacebookAdViewHolder) holder;
facebookAdViewHolder.tvAdTitle.setText(ad.getAdTitle());
facebookAdViewHolder.tvAdBody.setText(ad.getAdBody());
facebookAdViewHolder.tvAdSocialContext.setText(ad.getAdSocialContext());
facebookAdViewHolder.mvAdMedia.setNativeAd(ad);
facebookAdViewHolder.btnAdCallToAction.setText(ad.getAdCallToAction());
NativeAd.Image adIcon = ad.getAdIcon();
NativeAd.downloadAndDisplayImage(adIcon, facebookAdViewHolder.ivAdIcon);
ad.registerViewForInteraction(facebookAdViewHolder.itemView);
}
} else {
CustomViewHolder customViewHolder = (CustomViewHolder) holder;
Glide.with(context).load(URL_PART_1 + video.getVideoId() + URL_PART_2).into(customViewHolder.imageView);
customViewHolder.textViewTitle.setText(video.getTitle());
}
}
Again, as i mentioned earlier, the link provided above has the solution.
You have to add this to your FacebookAdViewHolder in order to make native ad container clickable.
private static class AdViewHolder extends RecyclerView.ViewHolder {
TextView nativeAdTitle;
Button nativeAdCallToAction;
FrameLayout nativeAdContainer;
ImageView nativeAdIcon;
AdViewHolder(View view) {
super(view);
nativeAdContainer = view.findViewById(R.id.fb_native_ad_container);
nativeAdTitle = (TextView) view.findViewById(R.id.native_ad_title);
nativeAdSocialContext = (TextView) view.findViewById(R.id.native_ad_social_context);
nativeAdCallToAction = (Button) view.findViewById(R.id.native_ad_call_to_action);
nativeAdIcon = (ImageView) view.findViewById(R.id.native_ad_icon);
}
public void bindView(NativeAd ad) {
nativeAdSocialContext.setText(ad.getAdSocialContext());
nativeAdCallToAction.setText(ad.getAdCallToAction());
nativeAdTitle.setText(ad.getAdTitle());
NativeAd.Image adIcon = ad.getAdIcon();
NativeAd.downloadAndDisplayImage(adIcon, nativeAdIcon);
List<View> clickableViews = new ArrayList<>();
clickableViews.add(nativeAdTitle);
clickableViews.add(nativeAdCallToAction);
ad.registerViewForInteraction(nativeAdContainer, clickableViews);
} }
Related
I have implemented a RecyclerView inside a ConstraintLayout. I am having one child image element inside the layout. But when I click the child image, it always returns the ConstraintLayout, not the clicked image.
Could you please tell me why this is happening, what is the solution for this ?
I separately did bind listener to image, it is working but not able to get the RecyclerItem object. I need RecyclerItem object for the position to proceed.
I implemented it by binding elements via onBindViewholder method in Adapter. Below are the codes
customAdapter = new GridViewAdapter(recyclerItems, 1, this.getContext().getPackageName(),
new GridViewAdapter.OnItemClickListener(){
#Override
public void onItemClick(RecyclerItem item) {
CommonUtil.addFragment("REP", Constants.CONTAINER_HOME,
new ModifyFragment(), getActivity(), null);
}
}, R.layout.rec_view_item_stock, Constants.V_SPAN_LIST_8);
RecyclerView recyclerView = view.findViewById(R.id.stockListRecView);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setLayoutManager(new GridLayoutManager(this.getContext(), 1));
recyclerView.setAdapter(customAdapter);
//Adapter
public class GridViewAdapter extends RecyclerView.Adapter<GridViewAdapter.ViewHolder>{
private List<RecyclerItem> dataItems;
private int hSpan = 1;
private String packageName;
private final OnItemClickListener listener;
private int inflator;
private int vSpan;
public interface OnItemClickListener {
void onItemClick(RecyclerItem item);
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private final ConstraintLayout constraintLayout;
private final TextView textView;
private final ImageView imageView;
private Object obj;
public ViewHolder(View view) {
super(view);
constraintLayout = (ConstraintLayout) view.findViewById(R.id.rec_content_layout);
textView = constraintLayout.findViewById(R.id.recTextView);
imageView = constraintLayout.findViewById(R.id.recImage);
}
public void bind(final RecyclerItem item, final OnItemClickListener listener) {
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onItemClick(item);
}
});
}
public TextView getTextView() {
return textView;
}
public ImageView getImageView() {
return imageView;
}
public Object getObj() {
return obj;
}
public void setObj(Object obj) {
this.obj = obj;
}
}
public GridViewAdapter(List<RecyclerItem> items, int spanCount, String packageName, OnItemClickListener listener,
int inflator, int vSpan) {
dataItems = items;
this.hSpan = spanCount;
this.packageName = packageName;
this.listener = listener;
this.inflator = inflator;
this.vSpan = vSpan;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int gridType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(inflator, viewGroup, false);
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width = (viewGroup.getResources().getDisplayMetrics().widthPixels / hSpan) - 24;
if(Constants.V_SPAN_GRID == vSpan) {
layoutParams.height = layoutParams.width;
} else {
layoutParams.height = (viewGroup.getResources().getDisplayMetrics().widthPixels / vSpan);
}
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
viewHolder.getTextView().setText(dataItems.get(position).getText());
int imgId = viewHolder.getImageView().getResources().getIdentifier(
dataItems.get(position).getImageName(), "drawable", packageName);
viewHolder.getImageView().setImageResource(imgId);
viewHolder.setObj(dataItems.get(position));
viewHolder.bind(dataItems.get(position), listener);
}
#Override
public int getItemCount() {
return dataItems.size();
}
//item layout
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/rec_content_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_marginTop="8dp"
android:layout_marginLeft="4dp"
android:padding="8dp"
android:background="#drawable/border_top_bottom" >
<androidx.constraintlayout.widget.Guideline
android:id="#+id/viewstock_gline_1_v"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.6"/>
<ImageView
android:id="#+id/recImage"
android:background="#color/colorPrimaryDark"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<TextView
android:id="#+id/recTextView"
app:layout_constraintLeft_toRightOf="#+id/recImage"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<ImageView
android:id="#+id/editstock_image"
android:src="#drawable/ic_edit_stock"
android:background="#color/colorPrimaryDark"
app:layout_constraintRight_toLeftOf="#+id/deletestock_image"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<ImageView
android:id="#+id/deletestock_image"
android:src="#drawable/ic_delete_stock"
android:background="#color/colorPrimaryDark"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
</androidx.constraintlayout.widget.ConstraintLayout>```
Try this!
public void bind(final RecyclerItem item, final OnItemClickListener listener, TextView itemTextView) {
itemTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onItemClick(item);
}
});
}
Pass every item text view instance from the view holder or fro onBindViewHolder itself implement onclick listener
I am creating simple screen which has one textview and button with the use of Recyclerview and Cardview.
Xml code is ready but I dont know what to write in my OnBindViewHolder class which is in my Adapter.
This is My Adapter for RecycleView
public class PaperAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<String> papers;
public TextView paperName;
public PaperAdapter(List<String> papers) {
this.papers = papers;
}
public class MyViewHolder extends RecyclerView.ViewHolder{
public MyViewHolder(View itemView) {
super(itemView);
paperName = itemView.findViewById(R.id.tvPaperName);
}
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View paperView = layoutInflater.inflate(R.layout.paper_row, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(paperView);
return myViewHolder;
}
#Override
public void onBindViewHolder( RecyclerView.ViewHolder holder, int position) {
}
#Override
public int getItemCount() {
return papers.size();
}}
This is my MainActivity
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
List<String> papers = new ArrayList<>();
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paper);
papers.add("Paper 1");
papers.add("Paper 2");
papers.add("Paper 3");
papers.add("Paper 4");
recyclerView = findViewById(R.id.rvPapers);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
PaperAdapter adapter = new PaperAdapter(papers);
recyclerView.setAdapter(adapter);
}
}
This is my Pagelayout xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="4dp"
app:cardElevation="5dp"
app:cardBackgroundColor="#A4C639"
app:cardUseCompatPadding="true"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
>
<TextView
android:id="#+id/tvPaperName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="paper 1"
android:textColor="#000000"
android:textSize="20sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="download" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
App is working without writing anything in OnBindViewHolder but its not showing Paper1,Paper2,Paper3 like this list so thats why I need to define textview in OnBindViewHolder but its showing error.Please let me know how to define textview in that and what to write in OnBindViewHolder.Also wanna set OnCLickListner for button so help me in that too.Thank you
onBindViewHolder(ViewHolder, int) is called by RecyclerView to display the data at the specified position. This method should update the contents of the RecyclerView.ViewHolder.itemView to reflect the item at the given position.
So you need to use this method to update the contents of the itemView to reflect the item at the given position.
As per your question you want to show TextView with Button in your RecyclerView, so you need to implement onBindViewHolder() as:
#Override
public void onBindViewHolder( RecyclerView.ViewHolder holder, int position) {
holder.paperName.setText(papers.get(position));
holder.download.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// TODO: Your code to download
}
});
}
You also need to give id to your button as:
<Button
android:id="#+id/btnDownload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="download" />
And update MyViewHolder as:
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView paperName;
Button download;
public MyViewHolder(View itemView) {
super(itemView);
paperName = itemView.findViewById(R.id.tvPaperName);
download = itemView.findViewById(R.id.btnDownload);
}
}
Simply, copy and paste this code in your PageAdapter class:
public class PaperAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<String> papers;
private Context mContext;
public PaperAdapter(Context context, List<String> papers) {
this.papers = papers;
this.mContext = context;
}
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView paperName;
Button download;
public MyViewHolder(View itemView) {
super(itemView);
paperName = itemView.findViewById(R.id.tvPaperName);
download = itemView.findViewById(R.id.btnDownload);
}
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View paperView = layoutInflater.inflate(R.layout.paper_row, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(paperView);
return myViewHolder;
}
#Override
public void onBindViewHolder( RecyclerView.ViewHolder holder, int position) {
holder.paperName.setText(papers.get(position));
holder.download.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(mContext, papers.get(position), Toast.LENGTH_SHORT).show();
// TODO: Your code to download
}
});
}
#Override
public int getItemCount() {
return papers.size();
}
}
EDIT: You need Context to show Toast, so pass Context in your MainActivity as:
PaperAdapter adapter = new PaperAdapter(this, papers);
i already success to make one Recyclerview and i want to add new Recyclerview Horizontal on top. i will explain in my code :
<android.support.v7.widget.RecyclerView
android:id="#+id/arrayListUser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:divider="#color/white">
</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:id="#+id/arrayList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:divider="#color/white">
</android.support.v7.widget.RecyclerView>
id:arrayList is my first Recyclerview have name xml feeds_listview
id:arrayListUser is my new Recyclerview, i want make this Recyclerview Horizontal
xml for new Recylerview is feeds_listviewUser
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/profil"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:src="#drawable/cthprofil" />
<TextView
android:id="#+id/fullName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="5dp"
android:layout_marginBottom="20"
android:text="Megi Fernanda"
android:textSize="17sp"
android:textColor="#color/colordefault"
android:textStyle="bold" />
</LinearLayout>
and this is my class adapter
public class FeedsCustomAdapter extends RecyclerView.Adapter<FeedsCustomAdapter.ViewHolder> {
private Context context;
private List<FeedsAdapter> feeds_list;
private ArrayList<Feeds> mFeedsList = new ArrayList<Feeds>();
private OnItemClickListener mListener;
private OnItemClickListener mListener2;
public FeedsCustomAdapter(Context context, ArrayList<Feeds> mFeedsList) {
this.context = context;
this.mFeedsList = mFeedsList;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.feeds_listview, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Feeds feed = getFeeds().get(position);
int textColor = context.getResources().getColor(R.color.btn_next);
int textColor2 = context.getResources().getColor(R.color.text_color_black);
holder.fullName.setText(feed.user.fullName);
holder.location.setText(feed.user.location);
holder.topic.setText(Html.fromHtml( "Menyelesaikan Tantangan " + " <font color = '" + String.valueOf(textColor2) + "'>" + feed.topic + "</font>" ) );
Picasso.with(context)
.load(feed.user.avatar)
.into(holder.profile);
PrettyTime prettyTime = new PrettyTime();
String times = prettyTime.format(DateUtil.timeMilisTodate(feed.timestamp * 1000));
holder.times.setText(times);
}
#Override
public int getItemCount() {
return mFeedsList.size();
}
public ArrayList<Feeds> getFeeds() {
return mFeedsList;
}
public void setComplete(int position) {
mFeedsList.get(position).isComplete = 1;
}
public boolean last() {
boolean result = false;
int total = mFeedsList.size();
for (int i = 0; i < mFeedsList.size(); i++) {
if (mFeedsList.get(i).isComplete == 1) {
total--;
}
}
if (total == 1) {
result = true;
}
return result;
}
class ViewHolder extends RecyclerView.ViewHolder {
public TextView fullName;
public TextView location;
public TextView topic;
public ImageView profile;
public TextView times;
public ViewHolder(View itemView) {
super(itemView);
fullName = (TextView) itemView.findViewById(R.id.fullName);
location = (TextView) itemView.findViewById(R.id.location);
topic = (TextView) itemView.findViewById(R.id.topic);
profile = (ImageView) itemView.findViewById(R.id.profil);
times = (TextView) itemView.findViewById(R.id.times);
profile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mListener2 != null){
mListener2.onItemClick2(v ,getPosition());
}
}
});
topic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mListener != null){
mListener.onItemClick(v ,getPosition());
}
}
});
}
}
public void setClickListener(OnItemClickListener clickListener) {
this.mListener = clickListener;
}
public void setClickListenerProfile(OnItemClickListener clickListener2){
this.mListener2 = clickListener2;
}
public interface OnItemClickListener {
public abstract void onItemClick(View view, int position);
public abstract void onItemClick2(View view, int position);
}
so, in my code i success to display first recylerview with my first xml and i want add new recylerview horizontal with new xml feeds_listviewUser
You can use LinearLayout to wrap both recyclerView.
<android.support.v7.widget.RecyclerView
android:id="#+id/arrayListUser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#color/white">
</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:id="#+id/arrayList"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:divider="#color/white">
</android.support.v7.widget.RecyclerView>
And assign horizontal layout manager to one recyclerview and vertical layout manager to other
LinearLayoutManager userManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
arrayListUser.setLayoutManager(userManager);
LinearLayoutManager listManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
arrayList.setLayoutManager(listManager);
Put your recyclerviews in Relative layout.
First add horizontal recyclerView to alignParentTop true and fix height according to visibility of feeds_listviewUser next add vertical recyclerView with layout_below horizontal recyclerview id.
I am using RecyclerView in my fragment to show images with text in Grid format,the Recycler view grid_item.xml look like following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#id/card_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_margin="#dimen/card_margin_grid"
card_view:cardCornerRadius="#dimen/card_album_radius">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#id/thumbnail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="?selectableItemBackgroundBorderless"
android:clickable="true"
android:scaleType="fitCenter" />
<TextView
android:id="#id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/thumbnail"
android:layout_margin="#dimen/album_title_padding"
android:textColor="#color/album_title"
android:textSize="#dimen/album_title" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
I am using adapter to populate data in RecyclerView, the code for this is :
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private ArrayList<Movie> movies;
private Context context;
public DataAdapter(Context context, ArrayList<Movie> movies) {
this.movies = movies;
this.context = context;
}
public void addItems(ArrayList<Movie> movies) {
this.movies.addAll(movies);
}
#Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_view_grid_item, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {
viewHolder.title.setText(movies.get(i).getTitle());
Picasso.with(context).load(movies.get(i).getImage_url_medium()).placeholder(R.drawable.placeholder).into(viewHolder.thumbnail);
}
#Override
public int getItemCount() {
return movies.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView title;
private ImageView thumbnail;
public ViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
view.setOnClickListener(this);
}
// Handles the row being being clicked
#Override
public void onClick(View view) {
int position = getAdapterPosition(); // gets item position
if (position != RecyclerView.NO_POSITION) { // Check if an item was deleted, but the user clicked it before the UI removed it
Movie movie = movies.get(position);
// start detail activity
Intent i = new Intent(context, MovieDetail.class);
i.putExtra(Constants.MOVIES_OBJECT, movie);
context.startActivity(i);
}
}
}
}
My problem is click listener is only working on the TextView and not on the image , although I have set click listener on whole view which contains both image and text.Is something wrong with my implementation ?
try setting android:focusableInTouchMode="false" to your imageview.
and remove android:clickable="true" or set it to false
Use viewHolder.itemView like :
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//your action
});
This will enable click action on whole RecyclerView item.
Add android:clickable="true" to the root RelativeLayout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#id/card_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_margin="#dimen/card_margin_grid"
android:clickable="true"
android:focusableInTouchMode="true"
card_view:cardCornerRadius="#dimen/card_album_radius">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#id/thumbnail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="?selectableItemBackgroundBorderless"
android:clickable="false"
android:focusableInTouchMode="false"
android:scaleType="fitCenter" />
<TextView
android:id="#id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/thumbnail"
android:layout_margin="#dimen/album_title_padding"
android:textColor="#color/album_title"
android:textSize="#dimen/album_title" />
</RelativeLayout>
</android.support.v7.widget.CardView>
public class OffersRecycleViewAdapter extends RecyclerView.Adapter {
private Activity mActivity;
private List<OfferShopModel> offerShopModels = new ArrayList<>();
ArrayList<String> allColors = new ArrayList<String>();
public OffersRecycleViewAdapter(List<OfferShopModel> offerShopModels, Activity activity, ArrayList<String> allColors) {
this.offerShopModels = offerShopModels;
this.mActivity = activity;
}
// String[] allColors = mActivity.getResources().getStringArray(R.array.colors);
#Override
public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview_offers, viewGroup, false);
return new ItemViewHolder(v);
}
#TargetApi(Build.VERSION_CODES.M)
#Override
public void onBindViewHolder(ItemViewHolder itemViewHolder, final int position) {
itemViewHolder.mOffer.setText(offerShopModels.get(position).getOffer());
}
#Override
public int getItemCount() {
return offerShopModels.size();
}
class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView mLinearLayoutBack;
TextView mOffer;
ItemViewHolder(View itemView) {
super(itemView);
mOffer = (TextView) itemView.findViewById(R.id.offer);
mOffer.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// call click event
}
}
}
remove android:clickable="true" from your ImageView or change it to android:clickable="false"
I have to make a scrolling images inside a recycler view with dot as indicator. Please have a look at attached screenshot for the same. Circled image is the dot indicator.
So far i have used the RecyclerView and using the GridLayoutManager. Inside Adapter i have made another item which is HEADER_TYPE i.e for scrolling images. For scrolling i am using this library and have successfully imported to my project.
As i am new to android i am not able to move further i do not have any idea how can i use this inside my project.
Also please suggest whether its the right path to achieve the same result or suggest some another way of doing the same.
My code so far:
public static final int TYPE_HEADER = 1;
public static final int TYPE_ITEM = 0;
List<CatetoryListModel> data = Collections.emptyList();
LayoutInflater inflater;
Context context;
public CategoryRecyclerAdapter(Context context, List<CatetoryListModel> data) {
inflater = LayoutInflater.from(context);
this.data = data;
this.context = context;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_HEADER) {
View view = inflater.inflate(R.layout.recycler_header, parent, false);
MyViewHolderHeader myViewHolder = new MyViewHolderHeader(view, context);
return myViewHolder;
} else {
View view = inflater.inflate(R.layout.recycler_custom_row, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(view, context);
return myViewHolder;
}
}
#Override
// public void onBindViewHolder(MyViewHolder holder, int position) {
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
// StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
// layoutParams.setFullSpan(true);
if (holder instanceof MyViewHolder) {
CatetoryListModel current = data.get(position - 1); //potion now becomes 1 and data start from 0 index
//holder.title.setText(current.getCategoryName());
// holder.desp.setText(current.getDescription());
((MyViewHolder) holder).icon.setImageResource(current.getImgSrc());
} else {
// ((MyViewHolderHeader) holder).icon.setImageResource(R.drawable.banner);
}
}
#Override
public int getItemViewType(int position) {
if (position == 0)
return TYPE_HEADER;
return TYPE_ITEM;
}
//Returns the total number of items in the data set hold by the adapter.
//no of items to be rendered by adapter
#Override
public int getItemCount() {
return data.size() + 1;
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView title;
TextView desp;
ImageView icon;
Context cntxt;
public MyViewHolder(View itemView, Context c) {
super(itemView);
cntxt = c;
itemView.setClickable(true);
itemView.setOnClickListener(this);
// title = (TextView)itemView.findViewById(R.id.category);
// desp = (TextView)itemView.findViewById(R.id.description);
icon = (ImageView) itemView.findViewById(R.id.imgsrc);
}
#Override
public void onClick(View v) {
Toast.makeText(cntxt, "Hello", Toast.LENGTH_LONG).show();
}
}
class MyViewHolderHeader extends RecyclerView.ViewHolder {
//ImageView icon;
// ViewPager mPager;
// CirclePageIndicator mIndicator;
// TestFragmentAdapter mAdapter;
public MyViewHolderHeader(View itemView, Context c) {
super(itemView);
// mAdapter = new TestFragmentAdapter(getSupportFragmentManager());
// mPager = (ViewPager)itemView.findViewById(R.id.pager);
// mPager.setAdapter(mAdapter);
// mIndicator = (CirclePageIndicator)itemView.findViewById(R.id.indicator);
// mIndicator.setViewPager(mPager);
}
}
recycler_header.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/indicator"
android:padding="10dip"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
recycler_custom_row.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="4dp"
android:id="#+id/card_view"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imgsrc"
android:src="#drawable/close" />
</RelativeLayout>