How to select only one switch in a recycler view? - java

I have come across a problem while trying to add a switch to each element of my recycler adapter.
What I want to achieve is to have only ONE switch selected at a time, so whenever I check a switch every other switch should automatically be unchecked.
This is the recycler adapter code I am currently using:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private final DomainAdapter DA = DomainAdapter.getInstance();
private final ArrayList<ArrayList<String>> list;
private String selected;
public RecyclerAdapter(ArrayList<ArrayList<String>> list, String selected) {
this.list = list;
this.selected = selected;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.list_element, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
if (list.get(position).get(0).equals(selected)) holder.switchButton.setChecked(true);
else holder.switchButton.setChecked(false);
holder.switchButton.setOnCheckedChangeListener((toggleButton, isChecked) -> {
if (isChecked) {
try {
DA.select(list.get(position).get(0));
} catch (NoSuchMethodException ignore) {}
}
else {
}
});
}
#Override
public int getItemCount() {
return list.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
Switch switchButton;
public ViewHolder(#NonNull View itemView) {
super(itemView);
switchButton = itemView.findViewById(R.id.switchButton);
}
}
}
I would really appreciate it if someone could tell me what I'm doing wrong.
Thank you!

Your logic is a bit incorrect.
You do :
if (list.get(position).get(0).equals(selected)) holder.switchButton.setChecked(true);
else holder.switchButton.setChecked(false);
And this invokes the checkedChange listener that you set a line later, in the next iteration after it has been set for the first time.
Remove the checkChangeListener and set a click listener instead.
Also, set the click listener in the onCreateViewHolder rather than in the onBindViewHolder
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.list_element, parent, false);
ViewHolder vh = new ViewHolder(view);
vh.switchButton.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View view) {
setItemSelected(vh.getAdapterPosition());
}
});
return vh;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position)
{
boolean isSelected = list.get(position).get(0).equals(selected);
holder.switchButton.setChecked(isSelected);
}
private void setItemSelected(int position) {
DA.select(list.get(position).get(0));
selected = list.get(position).get(0);
notifyDataSetChanged();
}

Related

How to pass bundle from an activity to a recycleview in another activity

I am trying to pass a bundle to a recycler view in another activity .
Here is the adapter
public class ListEventAdapter extends RecyclerView.Adapter<ListEventViewHolder> {
private List<EventResponse> eventResponseList;
Context context;
String date, teamOne, teamTwo;
public ListEventAdapter(List<EventResponse> eventResponse){
this.eventResponseList = eventResponse;
this.context = context;
}
#NonNull
#Override
public ListEventViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_event_item, parent, false);
// Create a ViewHolder
ListEventViewHolder viewHolder = new ListEventViewHolder(itemView);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull ListEventViewHolder holder, int position) {
getBundleValues();
EventResponse eventResponse = eventResponseList.get(position);
holder.date.setText(date);
}
//getIntent() is highlighted in red
private void getBundleValues() {
date = String.valueOf(getIntent().getStringExtra("date"));
teamOne = String.valueOf(getIntent().getStringExtra("team_one"));
teamTwo = String.valueOf(getIntent().getStringExtra("team_two"));
}
#Override
public int getItemCount() {
// Return the size of the data
return eventResponseList.size();
}
}
Is there a better way of implementing this? Thank you

What might be the problem with my adapter i seem not to find it``

The view seems not to bind to the ArrayList(ie private ArrayList mEvents;) that i passed to the class ,on the onBindViewHolder(ie holder.mTextView2.setText(current.getcity());) i can seem to access the textView variables(ie public TextView mTextview2;), that i declared in the view holder class. I have tried to make the variables as a member variable but it seems not to work well
public class eventsAdapter extends RecyclerView.Adapter {
private ArrayList<recycleview_items> mEvents;
public eventsAdapter(ArrayList<recycleview_items> passedArray){
//constructor for the adapter calls for which i created
mEvents = passedArray;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
//we pass the layout of our card to the adapter here
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_listitem, parent, false);
eventViewHolder events = new eventViewHolder(v);
return events;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
//we pass data from the array list we created to the views
recycleview_items current = mEvents.get(position);
holder.mImageView.setImageResource(current.getImageResource());
holder.mTextView1.setText(current.getname());
holder.mTextView2.setText(current.getcity());
}
#Override
public int getItemCount() {
return mEvents.size();
};
public static class eventViewHolder extends RecyclerView.ViewHolder{
public ImageView mImageview ;
public TextView mTextview;
public TextView mTextview2;
public eventViewHolder(#NonNull View itemView) {
super(itemView);
mImageview = itemView.findViewById(R.id.ImageView1);
mTextview = itemView.findViewById(R.id.text_view1);
mTextview2 = itemView.findViewById(R.id.text_view2);
}
}
}
public class eventsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<recycleview_items> mEvents;
public eventsAdapter(ArrayList<recycleview_items> passedArray){
//constructor for the adapter calls for which i created
mEvents = passedArray;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
//we pass the layout of our card to the adapter here
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_listitem, parent, false);
eventViewHolder events = new eventViewHolder(v);
return events;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
//we pass data from the array list we created to the views
recycleview_items current = mEvents.get(position);
holder.bind(current);
}
#Override
public int getItemCount() {
return mEvents.size();
};
class eventViewHolder extends RecyclerView.ViewHolder{
public ImageView mImageview ;
public TextView mTextview;
public TextView mTextview2;
public eventViewHolder(#NonNull View itemView) {
super(itemView);
mImageview = itemView.findViewById(R.id.ImageView1);
mTextview = itemView.findViewById(R.id.text_view1);
mTextview2 = itemView.findViewById(R.id.text_view2);
public void bind(recycleview_items item){
//bind your items here
holder.mImageView.setImageResource(item.getImageResource());
holder.mTextView1.setText(item.getname());
holder.mTextView2.setText(item.getcity());
}
}
}
I've rewritten your code for you with slight modifications.
You should create adapter like this
extends RecyclerView.Adapter<eventsAdapter.eventViewHolder >
Also you should edit
public eventViewHolder onCreateViewHolder
And
onBindViewHolder(eventViewHolder holder, int position)
Try this
public class eventsAdapter extends RecyclerView.Adapter<eventsAdapter.eventViewHolder>{
private ArrayList<recycleview_items> mEvents;
public eventsAdapter(ArrayList<recycleview_items> passedArray){
//constructor for the adapter calls for which i created
mEvents = passedArray;
}
#NonNull
#Override
public eventViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_listitem, parent, false);
return new eventViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull eventViewHolder holder, int position) {
recycleview_items current = mEvents.get(position);
holder.mImageview.setImageResource(current.getImageResource());
holder.mTextview.setText(current.getname());
holder.mTextview2.setText(current.getcity());
}
#Override
public int getItemCount() {
return 0;
}
class eventViewHolder extends RecyclerView.ViewHolder {
ImageView mImageview;
TextView mTextview;
TextView mTextview2;
public eventViewHolder(View itemView) {
super(itemView);
mImageview = itemView.findViewById(R.id.ImageView1);
mTextview = itemView.findViewById(R.id.text_view1);
mTextview2 = itemView.findViewById(R.id.text_view2);
}
}
}
Almost every thing was in Your code that's i have done some modification.
Mate see your Holder View Class name its 'eventViewHolder' and for onBindViewHolder and onCreateViewHolder you're accessing the Recycler.ViewHolder so it's very obvious that you can't access those variable just change this..
#Override
public void onBindViewHolder(#NonNull eventsAdapter.eventViewHolder holder, int position) {
.....
}
and in
#NonNull
#Override
public eventsAdapter.eventViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
......
}

RecyclerView Adapter won't inflate the correct layout

I'm trying to have my application display a specific layout when the arraylist PinnedSongs is empty.
I've created different viewholders and tried to inflate them depending on the PinnedSongs.size. Meaning, if it's 0, then the onCreateViewHolder should inflate a special layout. If it's different, then inflate another one.
public class RecyclerAdapterHome extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public class Viewholder extends RecyclerView.ViewHolder implements View.OnClickListener{
CardView home_cardView;
TextView home_textView;
TextView home_textView_2;
Button home_button;
Button home_button_2;
RelativeLayout parentlayout;
OnNoteListener_2 onNoteListener2;
public Viewholder(#NonNull View itemView, OnNoteListener_2 onNoteListener_2) {
super(itemView);
home_cardView = itemView.findViewById(R.id.cardview_2);
home_textView = itemView.findViewById(R.id.cardview_2_textview);
home_textView_2 = itemView.findViewById(R.id.cardview_2_textview_sub);
home_button = itemView.findViewById(R.id.play_button);
home_button_2 = itemView.findViewById(R.id.push_button);
parentlayout = itemView.findViewById(R.id.home_parentlayout);
home_button.setOnClickListener(this);
this.onNoteListener2 = onNoteListener_2;
}
#Override
public void onClick(View v) {
onNoteListener2.onClick(getAdapterPosition());
}
}
public class Viewholder_2 extends RecyclerView.ViewHolder {
TextView textView_3;
ImageView imageView;
RelativeLayout parentlayout;
public Viewholder_2(#NonNull View itemView) {
super(itemView);
textView_3 = itemView.findViewById(R.id.text_warning);
imageView = itemView.findViewById(R.id.icon);
parentlayout = itemView.findViewById(R.id.home_parentlayout);
}
}
public RecyclerAdapterHome(OnNoteListener_2 mOnNoteListener2, ArrayList<String> pinnedSongs, ArrayList<String> pinnedSongsArtists, Context mContext) {
this.mOnNoteListener2 = mOnNoteListener2;
this.PinnedSongs = pinnedSongs;
this.PinnedSongsArtists = pinnedSongsArtists;
this.mContext = mContext;
}
private ArrayList<String> PinnedSongs = new ArrayList<>();
private ArrayList<String> PinnedSongsArtists = new ArrayList<>();
private Context mContext;
private OnNoteListener_2 mOnNoteListener2;
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = null;
if (PinnedSongs.size()==0){
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.nothing, parent, false);
return new Viewholder_2(v);
}else{
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.homeviewholder,
parent, false);
return new Viewholder(v,mOnNoteListener2 );
}
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
if (PinnedSongs.size()==0){
Viewholder_2 viewholder_2 = (Viewholder_2) holder;
}else{
Viewholder viewholder = (Viewholder) holder;
viewholder.home_textView.setText(PinnedSongs.get(position));
viewholder.home_textView_2.setText(PinnedSongsArtists.get(position));
}
}
#Override
public int getItemCount() {
return PinnedSongs.size();
}
public interface OnNoteListener_2 {
void onClick(int position);
}
}
So far, the recyclerview shows nothing when the PinnedSongs list is at zero. But it shows something when it's above zero. Any idea on how I can fix it?
(I'm using the androidx libraries if it has anything to do with that).
It doesn't work because the getItemCount method returns 0 and the RecyclerView never tries to instantiate a view.
It means that onBindViewHolder is not called when the size=0.
You have different options to do it. One of these is:
#Override
public int getItemCount() {
return PinnedSongs.size() > 0 ? PinnedSongs.size() : 1;
}
#Override
public int getItemViewType(int position) {
if (PinnedSongs.size() == 0) {
return EMPTY_VIEW;
}
return super.getItemViewType(position);
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
if (viewType == EMPTY_VIEW) {
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.nothing, parent, false);
EmptyViewHolder emptyViewHolder = new EmptyViewHolder(v);
return emptyViewHolder;
}
//normal case
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.homeviewholder, parent, false);
ViewHolder viewholder = new ViewHolder(v);
return viewholder;
}
public class EmptyViewHolder extends RecyclerView.ViewHolder {
public EmptyViewHolder(View itemView) {
super(itemView);
}
}
private static final int EMPTY_VIEW = 1000;

Error on adapter class. ViewHolder views must not be attached when created

I have a problem with my code, I created two adapter within one recyclerview. I put them both in a class, and I need this adapter to return an Viewholder depending on some situations.
Here is my Adapter code:
public class CurrentUserTaskListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public List<Task> taskList;
private static final int ITEM_TYPE_PUBLISHED_TASK = 1;
private static final int ITEM_TYPE_ASSIGNED_TASK = 2;
public CurrentUserTaskListAdapter(List<Task> taskList) {
this.taskList = taskList;
}
public class AssignedTaskViewHolder extends RecyclerView.ViewHolder {
View mView;
public TextView tvTagAndDate;
public TextView tvStatus;
public AssignedTaskViewHolder(View itemView) {
super(itemView);
mView = itemView;
tvTagAndDate = itemView.findViewById(R.id.tvTagAndDate);
tvStatus = itemView.findViewById(R.id.tvStatus);
}
}
public class PublishedTaskViewHolder extends RecyclerView.ViewHolder {
View mView;
public TextView tvTagAndDate;
public TextView tvStatus;
public PublishedTaskViewHolder(View itemView) {
super(itemView);
mView = itemView;
tvTagAndDate = itemView.findViewById(R.id.tvTagAndDate);
tvStatus = itemView.findViewById(R.id.tvStatus);
}
}
#Override
public int getItemViewType(int position) {
if (taskList.get(position).getAssignedTo() == null)
return ITEM_TYPE_PUBLISHED_TASK;
else
return ITEM_TYPE_ASSIGNED_TASK;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
if(i == ITEM_TYPE_ASSIGNED_TASK){
layoutInflater.inflate(R.layout.layout_curent_user_assigned_tasks_item, viewGroup, false);
return new AssignedTaskViewHolder(viewGroup);
}
else if (i == ITEM_TYPE_PUBLISHED_TASK){
layoutInflater.inflate(R.layout.layout_current_user_pending_tasks, viewGroup, false);
return new PublishedTaskViewHolder(viewGroup);
}
return null;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder viewHolder, int i) {
if(viewHolder instanceof AssignedTaskViewHolder){
final AssignedTaskViewHolder assignedTaskViewHolder = (AssignedTaskViewHolder) viewHolder;
}
else if (viewHolder instanceof PublishedTaskViewHolder) {
final PublishedTaskViewHolder publishedTaskViewHolder = (PublishedTaskViewHolder) viewHolder;
}
}
#Override
public int getItemCount() {
return taskList.size();
}}
The problem i have is that i get this error:
java.lang.IllegalStateException: ViewHolder views must not be attached when created. Ensure that you are not passing 'true' to the attachToRoot parameter of LayoutInflater.inflate(..., boolean attachToRoot)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6796)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5975)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)....and so on
In the method getItemViewType i verify which TYPE an object from my list is.
Fix this part it is in example:
if (i == ITEM_TYPE_ASSIGNED_TASK) {
View view = layoutInflater.inflate(R.layout.layout_curent_user_assigned_tasks_item, viewGroup, false);
return new AssignedTaskViewHolder(view);
} else if (i == ITEM_TYPE_PUBLISHED_TASK) {
View view = layoutInflater.inflate(R.layout.layout_current_user_pending_tasks, viewGroup, false);
return new PublishedTaskViewHolder(view);
}
Problem is that you are passing parent layout where ViewHolder's item layout should be.
Good luck!

Using different views in onCreateViewHolder

I followed this tutorial by Google Developer's YouTube channel to implement AdMob native express ads.
I got the following error:
required: packagename.adapter.viewHolder
found : packagename.adapter.NativeExpressAdViewHolder
Here is how my onCreateViewHolder look like:
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case AD_VIEW_TYPE:
View nativeExpressLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.native_express_add_container, parent, false);
return new NativeExpressAdViewHolder(nativeExpressLayoutView);
case MENU_ITEM_VIEW_TYPE:
default:
View myLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list, parent, false);
return new ViewHolder(myLayoutView);
}
}
and here is my 2 different ViewHolder classes:
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ViewHolder(View itemView) {
super(itemView);
}
}
public class NativeExpressAdViewHolder extends RecyclerView.ViewHolder {
NativeExpressAdViewHolder(View view) {
super(view);
}
}
Here is simular questions with no answers:
Error "Incompatible types" while adding NativeAds in recyclerView
How to add NativeExpressAdView with Custom Model List Android?
EDIT:
Here is my full adapter as requested:
public class MainActivityVideoAdapter extends Adapter<MainActivityVideoAdapter.ViewHolder> {
ArrayList<Bitmap> bitmapArrayList;
Context context;
LayoutInflater layoutInflater;
View myLayoutView;
ArrayList<PathModel> ThumbPathList;
ArrayList<PathModel> VideoPathList = new ArrayList();
static DBManager manager;
long _id;
private static final int MENU_ITEM_VIEW_TYPE = 0;
private static final int AD_VIEW_TYPE = 1;
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
//Video Title
TextView videoName;
//Video Image
CircularImageView videoThumb;
//PopupMenu
ImageButton viewholderOtions;
ViewHolder(View itemView) {
super(itemView);
viewholderOtions = (ImageButton) myLayoutView.findViewById(R.id.viewholderOptions);
videoName = (TextView) myLayoutView.findViewById(R.id.FilePath);
videoThumb = (CircularImageView) myLayoutView.findViewById(R.id.VideoThumbnail);
//View onClick
itemView.setOnClickListener(this);
//Popup onClick
viewholderOtions.setOnClickListener(this);
}
//Handling click events
#Override
public void onClick(View v) {
if (v == viewholderOtions) {
int position = (int) v.getTag();
showPopupMenu(viewholderOtions, position);
}
}
}
public class NativeExpressAdViewHolder extends RecyclerView.ViewHolder {
NativeExpressAdViewHolder(View view) {
super(view);
}
}
public MainActivityVideoAdapter(Context context, ArrayList<PathModel> ThumbPathList, ArrayList<PathModel> VideoPathList) {
this.context = context;
this.ThumbPathList = ThumbPathList;
this.VideoPathList = VideoPathList;
}
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case AD_VIEW_TYPE:
View nativeExpressLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.native_express_add_container, parent, false);
return new NativeExpressAdViewHolder(nativeExpressLayoutView);
case MENU_ITEM_VIEW_TYPE:
default:
View myLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list, parent, false);
return new ViewHolder(myLayoutView);
}
}
public void onBindViewHolder(final ViewHolder myHolder, final int position) {
int viewType = getItemViewType(position);
switch (viewType) {
case AD_VIEW_TYPE:
case MENU_ITEM_VIEW_TYPE:
default:
final PathModel videoPathModel = this.VideoPathList.get(position);
PathModel thumbathModel = this.ThumbPathList.get(position);
File file = new File(videoPathModel.getPath());
String filename = file.getName();
myHolder.videoName.setText(filename);
myHolder.videoThumb.setImageURI(Uri.parse(thumbathModel.getPath()));
myHolder.viewholderOtions.setTag(position);
myHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent= new Intent(context, VideoPlayerActivity.class);
intent.putExtra("fromFA2", "fromFA2");
context.startActivity(intent);
}
});
}
}
Hello #ClassA i found that you have been importing local class rather than RecyclerView.ViewHolder in onBindViewHolder()
Please go through below code, this may help you.
public class MainActivityVideoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
/*
------------------
your constructor goes here
-----------------
*/
#Override
public int getItemCount() {
return 0;
}
public void onBindViewHolder(final RecyclerView.ViewHolder myHolder, final int position) {
int viewType = getItemViewType(position);
switch (viewType) {
case AD_VIEW_TYPE:
break;
case MENU_ITEM_VIEW_TYPE:
break;
}
}
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case AD_VIEW_TYPE:
View nativeExpressLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.native_express_add_container, parent, false);
return new NativeExpressAdViewHolder(nativeExpressLayoutView);
case MENU_ITEM_VIEW_TYPE:
View myLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list, parent, false);
return new ViewHolder(myLayoutView);
}
}
class ViewHolder extends RecyclerView.ViewHolder {
ViewHolder(View itemView) {
super(itemView);
}
}
public class NativeExpressAdViewHolder extends RecyclerView.ViewHolder {
NativeExpressAdViewHolder(View view) {
super(view);
}
}
}
This code doesn't contain your variables and logic, This code is for perfect import and use of methods.
If this resolve your problem, please make this answer approved.
Happy Coding.
I have my doubts that calling the class ViewHolder messed you up. Call it rather MenuViewHolder or something, cause now when writing simply ViewHolder in the method like this:
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
It assumes that you're thinking of your OWN class ViewHolder, rather than the RecyclerView.ViewHolder and then the types are incompatible. You can fix it by either changing the class name to something else or by changing the method declaration to:
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Although the latter would solve it as well, I'd opt for name changing, cause it's not a good practice to have classes with same names as native ones (e.g. View, ViewHolder, BaseAdapter etc.) cause it might produce confusion as it did here.
use RecyclerView's ViewHolder, not your ViewHolder.
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case AD_VIEW_TYPE:
View nativeExpressLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.native_express_add_container, parent, false);
return new NativeExpressAdViewHolder(nativeExpressLayoutView);
case MENU_ITEM_VIEW_TYPE:
default:
View myLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list, parent, false);
return new ViewHolder(myLayoutView);
}
}

Categories

Resources