Why i am having this error ?
java.lang.ClassCastException: com.example.pc.mytest.MainItemsRCVAdapter$ViewHolderHeader cannot be cast to com.example.pc.mytest.MainItemsRCVAdapter$ViewHolder
I am trying to have to a Recycler one header and one horizontal.
This is the class:
public class MainItemsRCVAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<ListMainItem> mainItems;
private Context mContext;
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
public MainItemsRCVAdapter(Context context,ArrayList<ListMainItem> maindata) {
setMaindata(maindata);
this.mainItems = maindata;
this.mContext = context;
}
public void setMaindata(ArrayList<ListMainItem> maindatahorizon) {
this.mainItems = maindatahorizon;
}
public ArrayList<ListMainItem> getMaindata() {
return mainItems;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
if (viewType == TYPE_ITEM) {
//inflate your layout and pass it to view holder
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.main_adapter_horizontal, viewGroup, false);
return new ViewHolder(v);
} else if (viewType == TYPE_HEADER) {
//inflate your layout and pass it to view holder
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.main_item_adapter, viewGroup, false);
return new ViewHolderHeader(v);
}
throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int i) {
if (holder instanceof ViewHolder) {
// String name = getDataVertical().get(position-1).getImages();
String name =mainItems.get(i).geImgName();
String urlimage =mainItems.get(i).getImgUrl();
((ViewHolder) holder).tv_items.setText(mainItems.get(i).geImgName());
// holder.tv_items.setText(mainItems.get(i).geImgName());
Log.d("###", "Setting name: " + name);
Picasso.with(mContext)
.load(R.drawable.upload).resize(240, 120)
.into(((ViewHolder) holder).img_items);
Log.d("###", "Setting url: " + urlimage);
} else if (holder instanceof ViewHolderHeader) {
//cast holder to VHHeader and set data for header.
((ViewHolder) holder).tv_items.setText(mainItems.get(i).geImgName());
Log.d("####", "HEADER");
}
//
// viewHolder.tv_items.setText(mainItems.get(i).geImgName());
// Picasso.with(mContext)
// .load(mainItems.get(i)
// .getImgUrl()).resize(240, 120)
// .into(viewHolder.img_items);
}
#Override
public int getItemCount() {
return mainItems.size();
}
#Override
public int getItemViewType(int position) {
if (isPositionHeader(position)) {
return TYPE_HEADER;
}
return TYPE_ITEM;
}
private boolean isPositionHeader(int position) {
return position == 0;
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView tv_items;
private ImageView img_items;
private ImageView mDataImgView;
public ViewHolder(View view) {
super(view);
tv_items = (TextView)view.findViewById(R.id.tv_items_horz);
img_items = (ImageView) view.findViewById(R.id.img_items_horz);
}
public ImageView getmDataImgView()
{
return mDataImgView;
}
}
public class ViewHolderHeader extends RecyclerView.ViewHolder{
private final RecyclerView mHorizontalRecyclerView;
public ViewHolderHeader(View v) {
super(v);
mHorizontalRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view_horizontal);
MainItemsRCVAdapterHorizontal mAdapter = new MainItemsRCVAdapterHorizontal(getMaindata(), mContext);
LinearLayoutManager layoutManager
= new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
mHorizontalRecyclerView.setLayoutManager(layoutManager);
mHorizontalRecyclerView.setAdapter(mAdapter);
}
}
}
this is the horizontal
public class MainItemsRCVAdapterHorizontal extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final ArrayList<ListMainItem> mData;
private final Context mContext;
public MainItemsRCVAdapterHorizontal(ArrayList<ListMainItem> dataHorizontal,Context context) {
mData = dataHorizontal;
mContext=context;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_adapter_horizontal, parent, false);
return new MainItemsRCVAdapterHorizontal.MyViewHolderHeader(v);
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
String name = mData.get(position).geImgName();
Log.d("###", "Setting name: " + name);
// ((RecyclerViewAdapterHorizontal.MyViewHolderHeader) holder).getmDataTextView().setText(name);
((MainItemsRCVAdapterHorizontal.MyViewHolderHeader) holder).getmDataImgView().setImageResource(mContext.getResources().getIdentifier(name, "drawable", "com.example.mohamadmouazen.lebgame.Adapters"));
}
#Override
public int getItemCount() {
return mData.size();
}
private class MyViewHolderHeader extends RecyclerView.ViewHolder {
// private final TextView mDataTextView;
private final ImageView mDataImgView;
public MyViewHolderHeader(View v) {
super(v);
// mDataTextView = (TextView) v.findViewById(R.id.data_vertical);
mDataImgView = (ImageView) v.findViewById(R.id.img_items_horz);
}
public ImageView getmDataImgView() {
return mDataImgView;
}
/* public TextView getmDataTextView() {
return mDataTextView;
}*/
}
}
Because you are making Invalid Casting.
Check your code:
} else if (holder instanceof ViewHolderHeader) {
//cast holder to VHHeader and set data for header.
((ViewHolder) holder).tv_items.setText(mainItems.get(i).geImgName());
Log.d("####", "HEADER");
}
You are casting ViewHolderHeader to ViewHolder
Related
I have three ViewHolders CardViewHolder, TitleViewHolder, GridViewHolder, for switching between it when user choose a specific layout, everything working good expect when I trying to switch to Title and Grid Layout/Views, I see only the "Dummy title and default image" that I put it in it's layout
Here's my PostAdapter class:
public class PostAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<Item> items;
public static final int CARD_LIST = 0;
public static final int CARD_MAGAZINE = 1;
public static final int TITLE = 2;
public static final int GRID = 3;
private int viewType;
PostAdapter(Context context, List<Item> items) {
this.context = context;
this.items = items;
}
public void setViewType(int viewType) {
this.viewType = viewType;
notifyDataSetChanged();
}
public int getViewType() {
return this.viewType;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view;
if (this.viewType == CARD_LIST) {
view = inflater.inflate(R.layout.card_layout, parent, false);
return new CardViewHolder(view);
} else if (this.viewType == CARD_MAGAZINE) {
view = inflater.inflate(R.layout.card_magazine_layout, parent, false);
return new CardViewHolder(view);
} else if(this.viewType == TITLE){
view = inflater.inflate(R.layout.title_layout, parent, false);
return new TitleViewHolder(view);
}else {
view = inflater.inflate(R.layout.grid_layout,parent,false);
return new GridViewHolder(view);
}
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
int itemType = getItemViewType(position);
final Item item = items.get(position);
final Document document = Jsoup.parse(item.getContent());
final Elements elements = document.select("img");
if (itemType == CARD_LIST || itemType == CARD_MAGAZINE) {
if (holder instanceof CardViewHolder) {
CardViewHolder cardViewHolder = (CardViewHolder) holder;
cardViewHolder.postTitle.setText(item.getTitle());
cardViewHolder.postDescription.setText(document.text());
Log.d("IMAGE", elements.get(0).attr("src"));
Glide.with(context).load(elements.get(0).attr("src"))
.into(cardViewHolder.postImage);
} else if (itemType == TITLE) {
if (holder instanceof TitleViewHolder) {
TitleViewHolder titleViewHolder = (TitleViewHolder) holder;
titleViewHolder.postTitle.setText(item.getTitle());
Log.d("IMAGE", elements.get(0).attr("src"));
Glide.with(context).load(elements.get(0).attr("src"))
.into(titleViewHolder.postImage);
}
}else {
GridViewHolder gridViewHolder = (GridViewHolder) holder;
gridViewHolder.postTitle.setText(item.getTitle());
Log.d("IMAGE", elements.get(0).attr("src"));
Glide.with(context).load(elements.get(0).attr("src"))
.into(gridViewHolder.postImage);
}
}
#Override
public int getItemCount() {
return items.size();
}
public class CardViewHolder extends RecyclerView.ViewHolder {
ImageView postImage;
TextView postTitle;
TextView postDescription;
public CardViewHolder(View itemView) {
super(itemView);
postImage = itemView.findViewById(R.id.postImage);
postTitle = itemView.findViewById(R.id.postTitle);
postDescription = itemView.findViewById(R.id.postDescription);
}
}
public class TitleViewHolder extends RecyclerView.ViewHolder {
TextView postTitle;
MyImageview postImage;
public TitleViewHolder(#NonNull View itemView) {
super(itemView);
postTitle = itemView.findViewById(R.id.postTitle);
postImage = itemView.findViewById(R.id.postImage);
}
}
public class GridViewHolder extends RecyclerView.ViewHolder {
TextView postTitle;
MyImageview postImage;
public GridViewHolder(#NonNull View itemView) {
super(itemView);
postTitle = itemView.findViewById(R.id.postTitle);
postImage = itemView.findViewById(R.id.postImage);
}
}
}
It seems that the first ViewHolder is only implemented in first if block, I tried to find out why the problem was caused by the log, but I don't know why unfortunately.
It is because you didn't give the view type by overriding getItemViewType(int position) method. You need to add the following method in your Adapter:
#Override
public int getItemViewType(int position) {
// this is just an example for returning the type.
// Assuming each item containing a type
return items.get(position).type;
// you can also use an if condition here
// for example
// if(position % 2 == 0) {
// return CARD_LIST;
// } else {
// return CARD_MAGAZINE;
// }
}
[Solved]
The problem was happens because I was checking for the wrong variable getting by method getItemViewType it should be like this
int itemType = getViewType();
I need your help to display the array texts as image, displaying them as text is working, but is image is not working. below are some explanation.
I have an array of texts that I want to display them in recycler view as an image.
String[] images =
{
"for_honor.png",
"R.drawable.ghost_recon.png",
"R.drawable.horizon_zerod.png",
"R.drawable.mass_effect.png"
};
setting them in this way seems wrong as i am not getting image
((MyViewHolder) holder).getmDataImgView().getResources().getIdentifier(name,"drawable","com.example.pkg.pg.Adapters");
however this code is working it show me them as text not image
// ((MyViewHolder) holder).getmDataTextView().setText(name);
this code im my main activity
MainGridRecView = (RecyclerView) findViewById(R.id.maingridrc);
MainGridRecViewLayoutManager = new LinearLayoutManager(context);
MainGridRecView.setLayoutManager(MainGridRecViewLayoutManager);
MainGridRecViewAdapter = new MainGridRvAdapter( dataHorizontal,dataVertical,MainActivity.this);
MainGridRecView.setAdapter(MainGridRecViewAdapter);
this maingridrvadapter
public class MainGridRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final Context mContext;
private ArrayList<ListMainItem> dataHorizontal;
private ArrayList<ListMainItem> dataVertical;
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
public MainGridRvAdapter(ArrayList<ListMainItem> dataHorizontal, ArrayList<ListMainItem> dataVertical, Context context) {
setDataHorizontal(dataHorizontal);
setDataVertical(dataVertical);
mContext = context;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_ITEM) {
//inflate your layout and pass it to view holder
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false);
return new MyViewHolder(v);
} else if (viewType == TYPE_HEADER) {
//inflate your layout and pass it to view holder
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_header, parent, false);
return new MyViewHolderHeader(v);
}
throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof MyViewHolder) {
String name = getDataVertical().get(position-1).getImages();
Log.d("###", "Setting name: " + name);
((MyViewHolder) holder).getmDataImgView().getResources().getIdentifier(name,"drawable","com.example.pkg.pg.Adapters");
// int resourceId = Activity.getResources().getIdentifier("testimage", "drawable", "your.package.name");
} else if (holder instanceof MyViewHolderHeader) {
//cast holder to VHHeader and set data for header.
Log.d("####", "HEADER");
}
}
#Override
public int getItemCount() {
return dataVertical.size() + 1;
}
#Override
public int getItemViewType(int position) {
if (isPositionHeader(position)) {
return TYPE_HEADER;
}
return TYPE_ITEM;
}
private boolean isPositionHeader(int position) {
return position == 0;
}
public void setDataHorizontal(ArrayList<ListMainItem> dataHorizontal) {
this.dataHorizontal = dataHorizontal;
}
public ArrayList<ListMainItem> getDataHorizontal() {
return dataHorizontal;
}
public void setDataVertical(ArrayList<ListMainItem> dataVertical) {
this.dataVertical = dataVertical;
}
public ArrayList<ListMainItem> getDataVertical() {
return dataVertical;
}
private class MyViewHolder extends RecyclerView.ViewHolder {
// private final TextView mDataTextView;
private final ImageView mDataImgView;
public MyViewHolder(View v) {
super(v);
// mDataTextView = (TextView) v.findViewById(R.id.data_vertical);
mDataImgView = (ImageView) v.findViewById(R.id.img_vertical);
}
/*
public TextView getmDataTextView() {
return mDataTextView;
}*/
public ImageView getmDataImgView() {
return mDataImgView;
}
}
private class MyViewHolderHeader extends RecyclerView.ViewHolder {
private final RecyclerView mHorizontalRecyclerView;
public MyViewHolderHeader(View v) {
super(v);
mHorizontalRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view_horizontal);
RecyclerViewAdapterHorizontal mAdapter = new RecyclerViewAdapterHorizontal(getDataHorizontal());
LinearLayoutManager layoutManager
= new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
mHorizontalRecyclerView.setLayoutManager(layoutManager);
mHorizontalRecyclerView.setAdapter(mAdapter);
}
}
}
layout
<TextView
android:id="#+id/data_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text"
/>
<ImageView
android:id="#+id/img_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
class RecyclerViewAdapterHorizontal extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final ArrayList<ListMainItem> mData;
private final Context mContext;
public RecyclerViewAdapterHorizontal(ArrayList<ListMainItem> dataHorizontal,Context context) {
mData = dataHorizontal;
mContext=context;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false);
return new RecyclerViewAdapterHorizontal.MyViewHolderHeader(v);
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
String name = mData.get(position).getImages();
Log.d("###", "Setting name: " + name);
// ((RecyclerViewAdapterHorizontal.MyViewHolderHeader) holder).getmDataTextView().setText(name);
((RecyclerViewAdapterHorizontal.MyViewHolderHeader) holder).getmDataImgView().setImageResource(mContext.getResources().getIdentifier(name, "drawable", "com.example.mohamadmouazen.lebgame.Adapters"));
}
#Override
public int getItemCount() {
return mData.size();
}
private class MyViewHolderHeader extends RecyclerView.ViewHolder {
// private final TextView mDataTextView;
private final ImageView mDataImgView;
public MyViewHolderHeader(View v) {
super(v);
// mDataTextView = (TextView) v.findViewById(R.id.data_vertical);
mDataImgView = (ImageView) v.findViewById(R.id.img_vertical);
}
public ImageView getmDataImgView() {
return mDataImgView;
}
/* public TextView getmDataTextView() {
return mDataTextView;
}*/
}
edit
class RecyclerViewAdapterHorizontal extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final ArrayList<ListMainItem> mData;
private final Context mContext;
public RecyclerViewAdapterHorizontal(ArrayList<ListMainItem> dataHorizontal,Context context) {
mData = dataHorizontal;
mContext=context;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false);
return new RecyclerViewAdapterHorizontal.MyViewHolderHeader(v);
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
String name = mData.get(position).getImages();
Log.d("###", "Setting name: " + name);
// ((RecyclerViewAdapterHorizontal.MyViewHolderHeader) holder).getmDataTextView().setText(name);
((RecyclerViewAdapterHorizontal.MyViewHolderHeader) holder).getmDataImgView().setImageResource(mContext.getResources().getIdentifier(name, "drawable", "com.example.pkg.pg.Adapters"));
}
#Override
public int getItemCount() {
return mData.size();
}
private class MyViewHolderHeader extends RecyclerView.ViewHolder {
// private final TextView mDataTextView;
private final ImageView mDataImgView;
public MyViewHolderHeader(View v) {
super(v);
// mDataTextView = (TextView) v.findViewById(R.id.data_vertical);
mDataImgView = (ImageView) v.findViewById(R.id.img_vertical);
}
public ImageView getmDataImgView() {
return mDataImgView;
}
/* public TextView getmDataTextView() {
return mDataTextView;
}*/
}
}
Change your images array to ints:
int[] images = {
R.drawable.for_honor,
R.drawable.ghost_recon,
R.drawable.horizon_zerod,
R.drawable.mass_effect
};
and set the image something like so:
int resourceId = images[position]; // <-- apply your actual logic here
...getmDataImgView().setImageResource(resourceId);
A method int getIdentifier (String name, String defType, String defPackage) returns resource identifier for the given resource name. In other words it returns the R.drawable.your_name .
When you set the text it works because TextView is expecting the String type.
Plus you actually do not set anything to your ImageView. Your mistake is here:
((MyViewHolder) holder).getmDataImgView().getResources().getIdentifier(name,"drawable","com.example.pkg.pg.Adapters");
You just getting the identifier from the resource. Instead to set the image you should do:
((MyViewHolder) holder).getmDataImgView().setImageResource(mContext.getResources().getIdentifier(name,"drawable","com.example.pkg.pg.Adapters"));
By the way it is not reccomended to hardcode your resources names in the string. Do it in the proper way by setting resource identifier directly.
I have a recycler view in which there are some string like 0,1,2,3 and so on and in the same recyclerview i want to place a view like imageview on every 3rd index. Code mentioned below is working fine but problem is that imageview starts from 0 index like sequence :- Imageview-->0,1,2--->imageview--->. Here i don't want to show image view before index 0.
code:-
mList = (RecyclerView) findViewById(R.id.dealList);
layoutManager = new LinearLayoutManager(mContext);
mList.setLayoutManager(layoutManager);
mList.setHasFixedSize(true);
// set divider between items of list
mList.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));
list = getResources().getStringArray(R.array.list);
int size = list.length;
ArrayList<String> newList = new ArrayList<>();
for (int i = 0; i < size; i++) {
if (i%2==0){
newList.add(null);
}
newList.add(list[i]);
}
adapter = new CMyAdapter(mContext, newList);
mList.setAdapter(adapter);
code for Adapter:-
private Context mContext;
private ArrayList<String> newList;
private static final int CONTENT_TYPE = 0;
private static final int AD_TYPE = 1;
public CMyAdapter(Context context, ArrayList<String> list) {
this.mContext = context;
this.newList = list;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ViewHolder holder = null;
if (viewType == AD_TYPE) {
View adView = LayoutInflater.from(parent.getContext()).inflate(R.layout.view2, parent, false);
holder = new AdViewHolder(adView);
} else {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.view1, parent, false);
holder = new ItemViewHolder(itemView);
}
return holder;
}
#Override
public int getItemViewType(int position) {
if (newList.get(position) == null) {
return AD_TYPE;
} else {
return CONTENT_TYPE;
}
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (newList.get(position) == null) {
AdViewHolder adViewHolder = (AdViewHolder) holder;
adViewHolder.m_dummyImage.setImageResource(R.mipmap.ic_launcher_round);
} else {
ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
itemViewHolder.m_Text.setText(newList.get(position));
}
}
#Override
public int getItemCount() {
return newList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
ViewHolder(View itemView) {
super(itemView);
}
}
private class ItemViewHolder extends ViewHolder {
TextView m_Text;
ItemViewHolder(View itemView) {
super(itemView);
m_Text = (TextView) itemView.findViewById(R.id.textDemo);
}
}
private class AdViewHolder extends ViewHolder {
ImageView m_dummyImage;
AdViewHolder(View v) {
super(v);
m_dummyImage = (ImageView) itemView.findViewById(R.id.adImage);
}
}
}
add (i!=0) in if Condition
if (i%2==0 && i!=0){
newList.add(null);
}
You could do this on CMyAdapter
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (position%3==0) {
//TODO
} else {
//TODO
}
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (position%3==0 && position>0){
AdViewHolder adViewHolder = (AdViewHolder) holder;
adViewHolder.m_dummyImage.setImageResource(R.mipmap.ic_launcher_round);
} else {
ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
itemViewHolder.m_Text.setText(newList.get(position));
}
}
I have the following adapter for a RecyclerView:
public class GreenAdapter extends RecyclerView.Adapter<GreenAdapter.NumberViewHolder> {
private final String TAG = GreenAdapter.class.getSimpleName();
private int mNumberItems;
public GreenAdapter(int numberOfItems) {
mNumberItems = numberOfItems;
}
#Override
public NumberViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
Context context = viewGroup.getContext();
int layoutIdForListItem = R.layout.number_list_item;
LayoutInflater inflater = LayoutInflater.from(context);
boolean shouldAttachToParentImmediately = false;
View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately);
NumberViewHolder viewHolder = new NumberViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(NumberViewHolder holder, int position) {
Log.d(TAG, "#" + position);
holder.bind(position);
}
#Override
public int getItemCount() {
return mNumberItems;
}
class NumberViewHolder extends RecyclerView.ViewHolder {
TextView listItemNumberView;
public NumberViewHolder(View itemView) {
super(itemView);
listItemNumberView = (TextView) itemView.findViewById(R.id.TV1);
}
void bind(int listIndex) {
String[] messages = getResources().getStringArray(R.array.messageArray);
listItemNumberView.setText(messages[listIndex]);
}
}
}
what I want to do is to make the first item of it bigger. My approach would be something like the code below, but I guess there is a better way to do it, right?
public class GreenAdapter extends RecyclerView.Adapter<GreenAdapter.NumberViewHolder> {
boolean isFirst=true;
private final String TAG = GreenAdapter.class.getSimpleName();
private int mNumberItems;
public GreenAdapter(int numberOfItems) {
mNumberItems = numberOfItems;
}
#Override
public NumberViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
Context context = viewGroup.getContext();
int layoutIdForListItem = R.layout.number_list_item;
if(isFirst) {
isFirst=false;
layoutIdForListItem = R.layout.first;
}
LayoutInflater inflater = LayoutInflater.from(context);
boolean shouldAttachToParentImmediately = false;
View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately);
NumberViewHolder viewHolder = new NumberViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(NumberViewHolder holder, int position) {
Log.d(TAG, "#" + position);
holder.bind(position);
}
#Override
public int getItemCount() {
return mNumberItems;
}
class NumberViewHolder extends RecyclerView.ViewHolder {
TextView listItemNumberView;
public NumberViewHolder(View itemView) {
super(itemView);
listItemNumberView = (TextView) itemView.findViewById(R.id.TV1);
}
void bind(int listIndex) {
String[] messages = getResources().getStringArray(R.array.messageArray);
listItemNumberView.setText(messages[listIndex]);
}
}
}
You should override the getItemViewType method.
Sample
public class GreenAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int VIEW_TYPE_LARGE = 1;
private static final int VIEW_TYPE_REGULAR = 2;
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_LARGE) {
// supply large layout
}
if (viewType == VIEW_TYPE_REGULAR) {
// supply regular layout
}
return null;
}
#Override
public int getItemViewType(int position) {
return position == 0 ? VIEW_TYPE_LARGE : VIEW_TYPE_REGULAR;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
}
#Override
public int getItemCount() {
return 0;
}
}
I am trying to implement RecyclerView with custom sectioned header based on list's category. I already have a list that's sorted based on their category, so I want to inflate header if and only if one row's category is different from preceding row's category. I've been figuring out how to do it but something bothering me here. It worked, but does not display the right row. I think it's because of the item count which is increased due to the header. Can anyone help me?
Here is my adapter :
List<Notes> notes;
OnItemClickListener mItemClickListener;
private static final int TYPE_CATEGOORY = 0;
private static final int TYPE_ITEM = 1;
String changing = "";
public OneNoteAdapter(List<Notes> logs) {
this.notes = logs;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == TYPE_CATEGOORY){
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.category_header, parent, false);
return new HeaderViewHolder(itemView);
}else{
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.learn_card, parent, false);
return new ItemViewHolder(itemView);
}
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(holder instanceof HeaderViewHolder){
Notes note = notes.get(position);
((HeaderViewHolder) holder).vCategoryText.setText(note.category);
}else {
ItemViewHolder item = (ItemViewHolder) holder;
Notes note = notes.get(position);
item.vNotesNote.setText(note.notes);
item.vNotesCat.setText(note.category);
}
}
#Override
public int getItemViewType(int position) {
if((!(notes.get(position).category).equals(changing))){
changing = notes.get(position).category;
return TYPE_CATEGOORY;
}else{
return TYPE_ITEM;
}
}
#Override
public int getItemCount() {
return notes.size();
}
public class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
protected TextView vNotesNote, vNotesCat;
private final Context context;
public ItemViewHolder(View itemView) {
super(itemView);
vNotesNote = (TextView) itemView.findViewById(R.id.notes_notes);
vNotesCat = (TextView) itemView.findViewById(R.id.notes_category);
context = itemView.getContext();
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(mItemClickListener != null){
mItemClickListener.onItemClick(v, getPosition());
}
}
}
public class HeaderViewHolder extends RecyclerView.ViewHolder {
protected TextView vCategoryText;
public HeaderViewHolder(View itemView) {
super(itemView);
vCategoryText = (TextView) itemView.findViewById(R.id.category_header_text);
}
}
public interface OnItemClickListener{
public void onItemClick(View v, int poition);
}
public void setOnItemClickListener (final OnItemClickListener mItemClickListener){
this.mItemClickListener = mItemClickListener;
}
}
Thank You
Try to replace your onBindViewHolder and getItemViewType methods with following:
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof HeaderViewHolder) {
((HeaderViewHolder) holder).vCategoryText.setText(notes.get(position + 1).category);
} else {
ItemViewHolder item = (ItemViewHolder) holder;
Notes note = notes.get(position);
item.vNotesNote.setText(note.notes);
item.vNotesCat.setText(note.category);
if (position + 1 < notes.size() &&
!notes.get(position + 1).category.equals(note.category)) {
notes.add(position + 1, null);
}
}
}
#Override
public int getItemViewType(int position) {
if (notes.get(position) == null) {
return TYPE_CATEGOORY;
} else {
return TYPE_ITEM;
}
}
And you can delete String changing = "";.