I have to set just four data from API in Recyclerview. The data from API are dynamic.If there are more than 4 data then the last one should hide itself and the recent data should appear at the top. I have researched but could not find the applicable solution.
My Adpater
public class EventRecyclerAdapter extends RecyclerView.Adapter<EventRecyclerAdapter.MyViewHolder> {
Context context;
private ArrayList<String> start;
private AdapterView.OnItemClickListener listener;
private ArrayList<String> desc;
private ArrayList<EventData>data;
public EventRecyclerAdapter(Context context, ArrayList<EventData> data) {
this.context = context;
this.data = data;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.content_events, parent, false);
return new EventRecyclerAdapter.MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(EventRecyclerAdapter.MyViewHolder holder, int position) {
holder.start.setText(data.get(position).getStartDate());
holder.des.setText(Html.fromHtml(data.get(position).getName()));
}
#Override
public int getItemCount() {
return data.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView start, des, viewall,name;
private CardView cardView;
public MyViewHolder(View itemView) {
super(itemView);
start = (TextView) itemView.findViewById(R.id.startdate);
des = (TextView) itemView.findViewById(R.id.description);
viewall = (TextView) itemView.findViewById(R.id.viewall);
}
}
}
I have set the data as follows
private void initEventRecycler() {
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
eventR.setLayoutManager(mLayoutManager);
eventR.setItemAnimator(new DefaultItemAnimator());
eventR.setNestedScrollingEnabled(false);
eventRecyclerAdapter = new EventRecyclerAdapter(MainActivity.this, events);
eventR.setAdapter(eventRecyclerAdapter);
}
If you have shorted data in the array list you are getting in adapter then use this code to show only 5 items.
#Override
public int getItemCount() {
if (data!= null && data.size() < 5) {
return data.size();
} else {
return 5;
}
}
if you have a data list in random order then first short it from latest to old and send to adapter.
Hope it will work for you.
EventData[] mData = new EventData[4];
int writeIndex = 0;
public void add(Object o) {
ring[writeIndex % ring.length] = o;
writeIndex++;
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return mData.lenght;
}
You can also Check ArrayCircularQueue.java
Related
I'm desperate.
I have a nested recycler view. Each outer element has an array of inner elements. A different adapter has been created for the inner elements. I am creating an array of external elements of class "KairosWithEvents", each of which contains internal elements of class "Event". Everything is displayed well. When elements are added, everything is also updated. For testing, I created two objects of the "KairosWithEvent" class. In the first object I have placed two objects of the "Event" class, and in the second - three objects. But when I want to change the EditText value, the keyboard appears. And the last element of the second object appears in the first object. How can I fix it? Objects are not moved or duplicated, but showed incorrectly.
This is what a nestled recycler view looks like initially.
And this is what a nested recycler view looks like after the keyboard appears. The "Эвент5" element is duplicated to the first element for some reason.
Here's my code: Outer Adapter:
public class EventAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static List<Item> items;
private static RecyclerViewClickInterface listener;
private RecyclerView.RecycledViewPool sharedPool = new RecyclerView.RecycledViewPool();
private Context context;
public EventAdapter() {
items = new ArrayList<>();
this.context = context;
}
public EventAdapter(List<Item> newItems){
items = newItems;
}
class KairosViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
private TextView title;
private CheckBox iv;
private RecyclerView rv;
public KairosViewHolder(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.tv);
iv = itemView.findViewById(R.id.iv);
rv = itemView.findViewById(R.id.rvSteaks);
itemView.setBackgroundColor(Color.parseColor("#91b3f2"));
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
#RequiresApi(api = Build.VERSION_CODES.N)
void bind(KairosWithEvents kairosWithEvents){
title.setText(kairosWithEvents.kairos.kairosId + " = " + kairosWithEvents.kairos.title);
LinearLayoutManager layoutManager = new LinearLayoutManager(
rv.getContext(),
LinearLayoutManager.VERTICAL,
false);
List<Item> itemsEvents = new ArrayList<>();
kairosWithEvents.events.forEach(i -> itemsEvents.add(new Item(Constants.EVENT_KAIROS, i)));
SubAdapter childAdapter = new SubAdapter(itemsEvents);
rv.setLayoutManager(layoutManager);
rv.setAdapter(childAdapter);
rv.setRecycledViewPool(sharedPool);
}
#Override
public void onClick(View view) {
int position = getAdapterPosition();
if (listener != null && position != RecyclerView.NO_POSITION){
listener.onItemKairosWithEvents( ((KairosWithEvents) items.get(position).object) );
}
}
#Override
public boolean onLongClick(View view) {
return false;
}
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
switch (viewType){
case Constants.KAIROS:
return new KairosViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item_ex, parent, false));
}
return null;
}
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
switch (getItemViewType(position)){
case Constants.KAIROS:
KairosWithEvents kairos = ((KairosWithEvents) items.get(position).object);
((KairosViewHolder) holder).bind(kairos);
break;
}
}
#Override
public int getItemCount() {
return items.size();
}
#Override
public int getItemViewType(int position) {
return items.get(position).type;
}
}
Inner Adapter:
public class SubAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private static List<Item> items;
public SubAdapter(List<Item> newItems) {
items = newItems;
}
static class EventViewHolder extends RecyclerView.ViewHolder {
private TextView title;
public EventViewHolder(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.tv);
itemView.setBackgroundColor(Color.parseColor("#ffc8a8"));
}
void bind(Event event){
title.setText(event.eventId + " = " + event.title);
}
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
switch (viewType) {
case Constants.EVENT_KAIROS:
return new EventViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item_sub, parent, false));
}
return null;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
switch (getItemViewType(position)){
case Constants.EVENT_KAIROS:
Event event = (Event) items.get(position).object;
((EventViewHolder) holder).bind(event);
break;
}
}
#Override
public int getItemCount() {
return items.size();
}
#Override
public int getItemViewType(int position) {
return items.get(position).type;
}
}
In SubAdapter, remove the static keyword from your items field:
private static List<Item> items;
Should be this instead:
private List<Item> items;
You should make the same change to your outer adapter, but since there's only one of them at a time it doesn't wind up causing issues.
public class HashTagSearchAdapter extends RecyclerView.Adapter<HashTagSearchAdapter.HashTagViewHolder> {
Context context;
List<TagSearchObject> tagResponses;
public HashTagSearchAdapter (Context context, List<TagSearchObject> tagResponses){
this.context = context;
this.tagResponses = tagResponses;
for (int i =0;i<tagResponses.size();i++){
Log.e("tagfor",tagResponses.get(i).toString());
}
Log.e("constructor",tagResponses.size()+"");
Log.e("constructor","here");
}
#Override
public HashTagViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.tag_search_list,parent,false);
Log.e("onCreate","here");
return new HashTagViewHolder(view);
}
#Override
public void onBindViewHolder(HashTagViewHolder holder, int position) {
TagSearchObject userTagResponse = tagResponses.get(position);
Log.e("tag",userTagResponse.toString());
holder.name.setText(userTagResponse.getName());
holder.count.setText(userTagResponse.getChallengeCount()+" public posts");
}
#Override
public int getItemCount() {
Log.e("getItemCount",this.tagResponses.size()+"");
return tagResponses.size();
}
class HashTagViewHolder extends RecyclerView.ViewHolder{
ImageView hashtag;
TextView name,count;
public HashTagViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
count = (TextView) itemView.findViewById(R.id.count);
}
}
}
here logcat is :
E/tagfor:TagSearchObject{id='1', name='hahah', challengeCount='1'}
E/constructor: 1
E/constructor: here
Because you have not added a layout manager and/or your recycler view is not visible. Since it's not visible, no attempt to draw is made and itemCount is never called because, well, there is nothing to draw.
set layout manager before setAdapter to the RecyclerView
LinearLayoutManager llm = new LinearLayoutManager(getApplicationContext());
HashTagSearchAdapter hashTagSearchAdapter = new
HashTagSearchAdapter(context,tagSearchObjects);
recyclerView.setLayoutManager(llm);
recyclerView.setAdapter(hashTagSearchAdapter);
Unable to add second child in Recyclerview I am passing two different arrays to RecyclerAdapter to display two child layout with different data and views.Is there any solution to add different child layout using same header layout.I added horizontal Recyclerview in vertical Recyclerview and I want to display details like I attached the image
private void setupRecyclerView(RecyclerView recyclerView) {
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
RecyclerAdapter recyclerAdapter = new RecyclerAdapter();
int[] images = new int[]{
R.drawable.finance,
R.drawable.business,
R.drawable.financejob,
R.drawable.ecomchallenges
};
ArrayList<ChildView> childViews = new ArrayList<>();
childViews.add(new ChildView(images[0], "The \"Best\" Startup Pitch Deck - How To Raise Venture Capital", "$100"));
childViews.add(new ChildView(images[1], "An Entire MBA in 1 Course:Award Winning Business School Prof", "$100"));
childViews.add(new ChildView(images[2], "What Finance Job is for You? Explanation of 14 Finance Roles", "$100"));
childViews.add(new ChildView(images[3], "Learn To Build Beautiful HTML5 And CSS3 Websites In 1 Month", "$100"));
int[] courseImage = new int[] {
R.drawable.php,
R.drawable.development,
R.drawable.web,
R.drawable.java
};
ArrayList<CourseByType> courseByTypes = new ArrayList<>();
courseByTypes.add(new CourseByType("Technology", courseImage[0]));
courseByTypes.add(new CourseByType("Business", courseImage[1]));
courseByTypes.add(new CourseByType("Photography", courseImage[2]));
courseByTypes.add(new CourseByType("Development", courseImage[3]));
Log.d("","Above adapter");
recyclerAdapter.addItem(new GroupView("Business", childViews));
Log.d("","Below Child");
recyclerAdapter.addCourseByType(new CourseByHeader("Technology", courseByTypes));
Log.d("","Below Course");
recyclerView.setAdapter(recyclerAdapter);
}
This is the main fragment where I set the values to two different
arraylist ArrayList<ChildView> childViews = new ArrayList<>()
and
ArrayList<CourseByType> courseByTypes = new ArrayList<>()
Values of child views are passing properly but CourseByType values are not passing.This is the adapter class for this fragment class.
RecyclerAdapter.java
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
ArrayList<PassValues> containerArrayList;
ArrayList<GroupView> groupViews;
ArrayList<CourseByHeader>courseByHeaders;
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
View view = LayoutInflater.from(context).inflate(R.layout.group_title, parent, false);
return new ViewHolder(view);
}
public RecyclerAdapter(){
containerArrayList = new ArrayList<>();
groupViews = new ArrayList<>();
courseByHeaders = new ArrayList<>();
}
public void addContainer(PassValues container){
containerArrayList.add(container);
}
public void addItem(GroupView groupView){
Log.d("","Inside Group method");
groupViews.add(groupView);
}
public void addCourseByType(CourseByHeader courseByHeader){
Log.d("","Inside Course method");
courseByHeaders.add(courseByHeader);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Log.d("", "Pass Values out of IF" + position);
ChildViewAdapter childViewAdapter = new ChildViewAdapter();
if(position == 0){
GroupView groupView = groupViews.get(position);
holder.title.setText(groupView.getTitle());
Log.d("", "Passing Values" + groupView.getTitle());
holder.recyclerView.setLayoutManager(new LinearLayoutManager(holder.recyclerView.getContext(), LinearLayoutManager.HORIZONTAL, false));
holder.recyclerView.setOnFlingListener(null);
childViewAdapter.addChild(groupView.getChildViewList());
holder.recyclerView.setAdapter(childViewAdapter);
}
if (position == 1) {
CourseByHeader courseByHeader = courseByHeaders.get(position);
holder.title.setText(courseByHeader.getTitle());
Log.d("", "Passing Values" + courseByHeader.getTitle());
holder.recyclerView.setLayoutManager(new LinearLayoutManager(holder.recyclerView.getContext(), LinearLayoutManager.HORIZONTAL, false));
holder.recyclerView.setOnFlingListener(null);
childViewAdapter.addCourse(courseByHeader.getCourseByTypes());
holder.recyclerView.setAdapter(childViewAdapter);
}
}
#Override
public int getItemCount() {
if(getItemViewType(0) == TYPE_HEADER)
return groupViews.size() ;
if (getItemViewType(1) == TYPE_ITEM)
return courseByHeaders.size();
else return -1;
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView title;
RecyclerView recyclerView;
public ViewHolder(View itemView) {
super(itemView);
title = (TextView)itemView.findViewById(R.id.course_title);
recyclerView = (RecyclerView)itemView.findViewById(R.id.group_recycler);
}
}
}
This RecyclerAdapter contains one RecyclerView in that first row has one image and 3 textviews and 2nd row has 1 ImageView and 1 TextView. At position first,one image and 3 textviews are shown but it's not going on 2nd view
This is the view I getting after run on emulator.
This are two child for RecyclerViews
ChildView.java
public class ChildView {
int image;
String course, price;
public ChildView(int image, String course, String price) {
this.image = image;
this.course = course;
this.price = price;
}
public int getImage() {
return image;
}
public String getCourse() {
return course;
}
public String getPrice() {
return price;
}
}
CourseByType.java
public class CourseByType {
String courseName;
int courseImage;
public CourseByType(String courseName, int courseImage) {
this.courseName = courseName;
this.courseImage = courseImage;
}
public String getCourseName() {
return courseName;
}
public int getCourseImage() {
return courseImage;
}
}
CourseByHeader.java
public class CourseByHeader {
String title;
ArrayList<CourseByType> courseByTypes;
public CourseByHeader(String title, ArrayList<CourseByType> courseByTypes) {
this.title = title;
this.courseByTypes = courseByTypes;
}
public String getTitle() {
return title;
}
public ArrayList<CourseByType> getCourseByTypes() {
return courseByTypes;
}
}
GroupView.java
public class GroupView {
String title;
ArrayList<ChildView> childViewList;
String courseBy;
ArrayList<CourseByType> courseByTypes;
public GroupView(String title, ArrayList<ChildView> childViewList) {
this.title = title;
this.childViewList = childViewList;
}
public String getTitle() {
return title;
}
public ArrayList<ChildView> getChildViewList() {
return childViewList;
}
}
Groupview and CouseByType class have title and child list for recycleradapter
ChildViewAdapter.java
public class ChildViewAdapter extends RecyclerView.Adapter {
ArrayList<ChildView> childViewList;
ArrayList<CourseByType> courseByTypes;
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
public class ViewHolder extends RecyclerView.ViewHolder{
public ViewHolder(View itemView) {
super(itemView);
}
}
public class GroupHolder extends ViewHolder {
public ImageView iamView;
public TextView course, price;
public GroupHolder(View itemView) {
super(itemView);
iamView = (ImageView) itemView.findViewById(R.id.course_image);
course = (TextView) itemView.findViewById(R.id.course_by);
price = (TextView) itemView.findViewById(R.id.price);
}
}
public void addCourse(ArrayList<CourseByType> courseByType){
courseByTypes = courseByType;
}
public void addChild(ArrayList<ChildView> childView){
childViewList = childView;
}
public class Course extends ViewHolder {
public ImageView courseTypeImage;
public TextView courseType;
public Course(View itemView) {
super(itemView);
courseTypeImage = (ImageView)itemView.findViewById(R.id.course_image);
courseType = (TextView)itemView.findViewById(R.id.course_name_course);
}
}
public ChildViewAdapter() {
childViewList = new ArrayList<>();
courseByTypes = new ArrayList<>();
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
RecyclerView.ViewHolder vh = null;
View v;
if(viewType == TYPE_HEADER){
v = LayoutInflater.from(context).inflate(R.layout.recycler_item, parent, false);
return new GroupHolder(v);
}if(viewType == TYPE_ITEM){
v = LayoutInflater.from(context).inflate(R.layout.type_of_courses, parent, false);
return new Course(v);
}
return vh;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(holder instanceof GroupHolder){
Log.d("","instance of Group Holder");
ChildView childView = childViewList.get(position);
((GroupHolder)holder).iamView.setImageResource(childView.getImage());
((GroupHolder)holder).course.setText(childView.getCourse());
((GroupHolder)holder).price.setText(childView.getPrice());
return;
}
if(holder instanceof Course){
Log.d("","instance of Course ");
CourseByType courseByType = courseByTypes.get(position);
((Course)holder).courseTypeImage.setImageResource(courseByType.getCourseImage());
((Course)holder).courseType.setText(courseByType.getCourseName());
return;
}
}
#Override
public int getItemCount() {
int size;
if(childViewList.size()>0){
return size = childViewList.size();
}else return size = courseByTypes.size();
}
#Override
public int getItemViewType(int position) {
if(childViewList.size() != 0 && childViewList.size()>0){
return TYPE_HEADER;
}else return TYPE_ITEM;
}
}
This childview adapter has two view types first is one image and 3 text and second view type contain one image and one text.When I pass values from fragment only first view type get displayed and second view type not gets value from fragment.
To show multiple different views in a recyclerview, you have to override getItemViewType() in the recyclerview adapter.
//getItemViewType enables dynamic viewholder creation
#Override
public int getItemViewType(int position) {
//you will need to add a integer with variable name viewTypeCode
//for view1 set viewTypeCode = 100 and for view2 set viewTypeCode = 200
viewTypeCode = itemList.get(position).getViewTypeCode();
return viewTypeCode;
}
This is how the onCreateViewHolder will be different for multiple viewtypes. You will have to modify yours like this
#Override
public FeedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 100: return new FeedViewHolder(layoutInflater.inflate(R.layout.v1, parent, false),100);
case 200: return new FeedViewHolder(layoutInflater.inflate(R.layout.v2, parent, false),200);
}
return null;
}
OnBindViewHolder will be similarly modified
#Override
public void onBindViewHolder(final FeedViewHolder holder, int position) {
viewTypeCode = itemList.get(position).getViewTypeCode();
switch ( viewTypeCode) {
case 100:
//your code for v1
case 200:
//your code for v2
}
}
Similarly the ViewHolder class is modified
class FeedViewHolder extends RecyclerView.ViewHolder{
//declare variables here
public FeedViewHolder(View v, int viewType) {
super(v);
switch (viewType) {
//instead of itemView.findViewById you will have to use v.findViewById
case 100:
//your code for v1
case 200:
//your code for v2
}
}
For further reference refer to this SO answer
Don't pass two separate list.Make a custom class like this-
class MyClass {
int viewTypeCode;
CustomClass1 c1;
CustomClass2 c2;
//add the setter getter
}
In your activity while preparing the data.
List<MyClass> itemList = new ArrayList<>();
//put whatever logic you need to make the order of the list
//if CustomClass1 object is put then setViewTypeCode(100), setCustomClass2 = null
//if CustomClass2 object is put then setViewTypeCode(200), setCustomClass1 = null
After data is built, then send this to the adapter.
Prompt, faced with scrollToPosition speed. I RecyclerView 900 positions and need to quickly establish a position as scrollToPosition very slow, c ListView such problems were not setSelection working out perfectly. It is possible to accelerate the scrollToPosition?
public class EventAdapter extends RecyclerView.Adapter<EventAdapter.Holder> {
final static public String TAG = "EventAdapter";
Context context;
List<Event> items;
public EventAdapter(List<Event> items) {
this.items = items;
}
#Override
public EventAdapter.Holder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_event, parent, false);
context = parent.getContext();
return new EventAdapter.Holder(v);
}
#Override
public void onBindViewHolder(EventAdapter.Holder holder, int position) {
holder.setModel(context, items.get(position), position);
}
#Override
public int getItemCount() {
return items.size();
}
public class Holder extends RecyclerView.ViewHolder {
public View block;
Holder(View view) {
super(view);
block = (View) view.findViewById(R.id.cv);
}
public void setModel(Context context, Event model, int position) {
}
}
}
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
rv.setLayoutManager(linearLayoutManager);
rv.setHasFixedSize(true);
linearLayoutManager.scrollToPosition(index);
There are couple of ways to do it:
1) Try smoothScrollToPosition as : [will certainly work]
Toast.makeText(this, "Scroll...", Toast.LENGTH_SHORT).show();
myList.smoothScrollToPosition(0);
2) Also read about scrollToPositionWithOffset as this may help too
I need to do this: a RecycleView with CardView. The first card is static: it show "Details for this order". The other cards after the first are dynamics. So I decided to do this code:
public class DocumentTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
private List<DocumentType> document; //lista di card da riempire
private Context context;
public DocumentTypeAdapter(List<DocumentType>document, Context context)
{
this.document = document;
this.context = context;
}
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View view;
if(viewType == 0)
{
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_info, parent, false);
ViewSimple simpleView = new ViewSimple(view);
return simpleView;
}
else
{
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_info, parent, false);
DocumentTypeViewHolder documentTypeViewHolder = new DocumentTypeViewHolder(view);
return documentTypeViewHolder;
}
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
DocumentType documents = document.get(position);
if(getItemViewType(position)!=0)
{
holder.title.setText(documents.getTitle());
holder.cert.setText(documents.getTypeofCertificate());
holder.lastmod.setText(documents.getLastModified());
}
}
#Override
public int getItemCount()
{
return document.size();
}
#Override
public int getItemViewType(int position)
{
return document.size();
}
private class ViewSimple extends RecyclerView.ViewHolder
{
public ViewSimple(View itemView)
{
super(itemView);
}
}
public class DocumentTypeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
TextView title, lastmod,cert;
public DocumentTypeViewHolder(View itemView)
{
super(itemView);
title = (TextView)itemView.findViewById(R.id.tipo);
lastmod = (TextView)itemView.findViewById(R.id.ultimamodifica);
cert = (TextView)itemView.findViewById(R.id.certificato);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
}
}
}
but it doesn't work.
My question are:
how to make the first card static and other dynamics?
my data are in document list. So how to say to method getItemViewType() that the first card is static and others are generated from the size of the list document?
Edit: this is the code with changes:
public class DocumentTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
private static final int STATIC_CARD = 0;
private static final int DYNAMIC_CARD = 1;
private List<DocumentType> document;
private Context context;
public DocumentTypeAdapter(List<DocumentType>document, Context context)
{
this.document = document;
this.context = context;
}
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View view;
if(viewType == STATIC_CARD)
{
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_info, parent, false);
ViewSimple simpleView = new ViewSimple(view);
return simpleView;
}
else
{
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.document_card_type, parent, false);
DocumentTypeViewHolder documentTypeViewHolder = new DocumentTypeViewHolder(view);
return documentTypeViewHolder;
}
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
DocumentType documents = document.get(position);
if(getItemViewType(position)==DYNAMIC_CARD)
{
DocumentTypeViewHolder mHolder = (DocumentTypeViewHolder)holder;
mHolder.data.setText(documents.getData());
mHolder.status.setText(documents.getStatus());
Picasso.with(context).load(documents.getImage().toString()).into(mHolder.image, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
Log.d("Picasso","success");
}
#Override
public void onError()
{
Log.d("Picasso","error");
}
});
}
}
#Override
public int getItemCount()
{
return document.size();
}
#Override
public int getItemViewType(int position)
{
if(position == 0)
return STATIC_CARD;
else
return DYNAMIC_CARD;
}
private class ViewSimple extends RecyclerView.ViewHolder
{
public ViewSimple(View itemView)
{
super(itemView);
}
}
public class DocumentTypeViewHolder extends RecyclerView.ViewHolder
{
TextView data, status;
ImageView image;
public DocumentTypeViewHolder(View itemView)
{
super(itemView);
data = (TextView)itemView.findViewById(R.id.dateCharging);
status =(TextView)itemView.findViewById(R.id.statusCharging);
image = (ImageView)itemView.findViewById(R.id.documentImage);
}
}
}
Thanks for your answers
You should override getItemViewType() and make it return a different value when it should be a dynamic card or when its a static card. Since you want the first card to be static you should return a different value when position == 0. This will look something like this:
private static final int STATIC_CARD = 0;
private static final int DYNAMIC_CARD = 1;
#Override
public int getItemViewType(int position) {
if(position == 0) {
return STATIC_CARD;
} else {
return DYNAMIC_CARD;
}
}
Then in your onCreateViewHolder() method you should check the viewType and based on the outcome you should inflate a View, which you were already doing OK, but I would replace the hardcoded 0 with the private static final int STATIC_CARD.
Edit: Just came to my mind, if you only need one static card, you might want to consider placing a CardView in your Fragment/Activity xml layout and place the rest of the dynamic cards in a RecyclerView below that static card.
I'll suggest you to use a library that already correctly implement/handles all that for you: https://github.com/eyeem/RecyclerViewTools
Full disclosure: I wrote the library
Just write your adapter for the dynamic items and then:
DocumentTypeAdapter adapter = new // your adapter here
WrapAdapter wrapAdapter = new WrapAdapter(adapter); // that's from the library
wrapAdapter.addHeader( /* add here your static view */ );
recyclerView.setAdapter(wrapAdapter);
and add those to your build.gradle
repositories {
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
dependencies {
compile 'com.eyeem.recyclerviewtools:library:0.0.3-SNAPSHOT#aar'
}