How to display multiple TextViews in a RecyclerView list item - java

I'm using a ReyclerView and I want to display 3 TextViews in each list item.
so far I managed to display 3 TextViews but as 3 different list items.
Here's my adapter code:
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
private String[] mDataset;
public static class MyViewHolder extends RecyclerView.ViewHolder {
public TextView textView;
public MyViewHolder(View v) {
super(v);
textView = v.findViewById(R.id.recycler_view_item);
}
}
public CustomAdapter(String[] myDataset) {
mDataset = myDataset;
}
#Override
public CustomAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_item, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
And layout_item.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="15dp">
<TextView
android:id="#+id/recycler_view_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"/>
</LinearLayout>

just add two more textViews in layout_item

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="15dp">
<TextView
android:id="#+id/text_view_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"/>
<TextView
android:id="#+id/text_view_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"/>
<TextView
android:id="#+id/text_view_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"/>
</LinearLayout>
// If you want your textviews next to each other use android:orientation="horizontal" in LinearLayout or if you want your textviews one below the other use android:orientation="vertical" and this will in one view

Use Relative layout instead of Linear layout if you want to set three Textviews .It will be much easier you can drag and drop textview from design Platte where you want to set those Textviews

Related

Android Java Use a cardview to display the items of a gridview

Hi everyone I'm trying to show with a GridView some items that show the contents of an array of strings. This is the CardView I want to set as item:
So when I click on a button, the layout of the choice of the type of film becomes visible and I set an adapter:
cTBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
postaCardVIew.setVisibility(View.GONE);
cT.setVisibility(View.VISIBLE);
GridView simpleGridView = findViewById(R.id.simpleGridView);
String genrelist[] = {"Commedia", "Animazione", "Anime", "Avventura", "Azione", "Biografico", "Documentario", "Drammatico", "Fantascienza","Fantasy",
"Guerra", "Horror", "Musical", "Storico", "Thriller", "Western", "Giallo", "Sentimentale",
};
final ArrayAdapter<String> adapter = new ArrayAdapter<String> (posta.this,android.R.layout.simple_list_item_1, genrelist);
simpleGridView.setAdapter(adapter);
}
But this is what I am getting:
I want to inflate this layout though:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="150dp"
android:layout_height="150dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="30dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type"
android:layout_centerInParent="true"
android:textSize="20dp"
android:textStyle="bold"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
To get such a thing:
Can anyone help me figure out how I could inflate the desired layout?
In order to use a custom item layout, you need to create a custom adapter that extends from the BaseAdapter or ArrayAdapter
Then inflate and build your item layout in the getView() method:
public class CustomAdapter extends BaseAdapter {
String genrelist[] = {"Commedia", "Animazione", "Anime", "Avventura", "Azione", "Biografico", "Documentario", "Drammatico", "Fantascienza", "Fantasy",
"Guerra", "Horror", "Musical", "Storico", "Thriller", "Western", "Giallo", "Sentimentale",
};
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
holder = new ViewHolder();
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.title = convertView.findViewById(R.id.type);
holder.title.setText(genrelist[position]);
return convertView;
}
static class ViewHolder {
TextView title;
}
public int getCount() {
return genrelist.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
}
And use it as the GridView adapter:
cTBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
postaCardVIew.setVisibility(View.GONE);
cT.setVisibility(View.VISIBLE);
GridView simpleGridView = findViewById(R.id.simpleGridView);
simpleGridView.setAdapter(new CustomAdapter());
}
});
I had to manipulate the margin and cared width and wrap the CardView with a FrameLayout in order to force the margin among cards:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="4dp"
android:layout_marginVertical="4dp">
<androidx.cardview.widget.CardView xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="120dp"
android:layout_height="120dp"
app:cardCornerRadius="30dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Type: 1"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</FrameLayout>
Result:

RecyclerView Not Loading all items

I am populating Recycler View with a List of 7 string items but Recycler view only loads two of them
by the way my data is long text and it loads all items when text is short
this is my ContentAdapter.java
public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ViewHolder> {
private LayoutInflater mInflater;
private List<String> mContent;
ContentAdapter(Context context, List<String> Content) {
this.mInflater = LayoutInflater.from(context);
mContent = Content;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.rvcontent_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ContentAdapter.ViewHolder holder, int position) {
//loads only two times !?
holder.txtContentPage.setText(mContent.get(position));
}
#Override
public int getItemCount() {
return mContent.size(); // size is 7
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView txtContent;
ViewHolder(View itemView) {
super(itemView);
txtContent = itemView.findViewById(R.id.txtContent);
}
}
}
rvcontent_item.xml
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txtContent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.cardview.widget.CardView>
and Activity
rvContents.setNestedScrollingEnabled(false);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
rvContents.setLayoutManager(layoutManager);
ContentAdapter contentAdapter = new ContentAdapter(this, Data); // Data has 7 items
rvContents.setAdapter(contentAdapter);
and this is my layout activity xml
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvContentPage"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
Try with
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:nestedScrollingEnabled="false"
android:focusableInTouchMode="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvContentPage"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
The issue is
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvContentPage"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Here android:layout_height="match_parent" is a bug, since you are inside a scroll view your recyclerview should have height wrap_content. So that the parent will allow it to scroll.
Try changing ConstraintLayout height match_parent instead of wrap_content.
and if persists, Remove scrollView from root because RecyclerView itself makes layout scrollable.

color change of status change

I am making an application on leave management and I want to make my leave list layout like shown in the image I want to change the colour of list view on status change from the database. Now i just add the simple text view to show the list but i want to modify it like the image it will really helpful to me if you give me the code.
here is my java code adapter:
#NonNull
#Override
public LeaveViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.leave_list, null);
view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
LeaveViewHolder viewHolder = new LeaveViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull LeaveViewHolder holder, int position) {
LeaveModel leave = leaveList.get(position);
holder.tVFrom_date.setText("From: " + leave.getFrom_date());
holder.tVTo_date.setText("To: " + leave.getTo_date());
holder.tVStatus.setText("Status: " + leave.getLeavestatus());
if (holder.tVStatus == null) {
} else {
}
}
#Override
public int getItemCount() {
return leaveList.size();
}
public class LeaveViewHolder extends RecyclerView.ViewHolder {
protected TextView tVFrom_date, tVTo_date, tVStatus;
public View container;
public LeaveViewHolder(View itemView) {
super(itemView);
tVFrom_date = itemView.findViewById(R.id.tVFrom_date);
tVTo_date = itemView.findViewById(R.id.tVTo_date);
tVStatus = itemView.findViewById(R.id.tVStatus);
}
}
Here is my Xml layout:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="5dp"
card_view:cardPreventCornerOverlap="false"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingEnd="6dp"
android:paddingStart="6dp">
<TextView
android:id="#+id/tVFrom_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textColor="#color/Black"/>
<TextView
android:id="#+id/tVTo_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tVFrom_date"
android:layout_marginTop="5dp"
android:textColor="#color/Black"/>
<TextView
android:id="#+id/tVStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tVTo_date"
android:layout_marginTop="5dp"
android:textColor="#color/Red"
android:textStyle="bold" />
</RelativeLayout>
</android.support.v7.widget.CardView>
I have added image view in your layout so change color of this image view as par the status
<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:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="5dp"
card_view:cardPreventCornerOverlap="true"
card_view:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/img_LeaveAdapter_highlight"
android:layout_width="8dp"
android:layout_height="match_parent"
android:background="#color/colorPrimary" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingEnd="6dp"
android:paddingStart="6dp">
<TextView
android:id="#+id/tVFrom_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textColor="#color/Black" />
<TextView
android:id="#+id/tVTo_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tVFrom_date"
android:layout_marginTop="5dp"
android:textColor="#color/Black"
/>
<TextView
android:id="#+id/tVStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tVTo_date"
android:layout_marginTop="5dp"
android:textColor="#color/Red"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
and i have created adapter just for your understanding
public class LeaveAdapter extends RecyclerView.Adapter<LeaveAdapter.MyViewHolder> {
Activity activity;
ArrayList<LeaveModel> list;
public LeaveAdapter(Activity activity, ArrayList<LeaveModel> list) {
this.activity = activity;
this.list = list;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_leaveadapter, parent, false);
return new MyViewHolder(rootView);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
String leaveStatus = list.get(position).getLeaveStatus();
if (leaveStatus.equals("Panding for approval")) {
holder.img_LeaveAdapter_highlight.setBackgroundColor(ContextCompat.getColor(activity, R.color.Red));
} else if (leaveStatus.equals("Approved")) {
holder.img_LeaveAdapter_highlight.setBackgroundColor(ContextCompat.getColor(activity, R.color.Green));
}
}
#Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
ImageView img_LeaveAdapter_highlight;
public MyViewHolder(View itemView) {
super(itemView);
img_LeaveAdapter_highlight = itemView.findViewById(R.id.img_LeaveAdapter_highlight);
}
}
}
You have 2 options here:
In your RelativeLayout, add FrameLayout empty element and align left parent with full height and ~5dp width. Then in your onBindViewHolder() set FrameLayout background colour as you need. Of course, this option is simple shape (Rectangle)
If the shape you want is specific, also in RelativeLayout, add .PNG image and set backgroundTint programmatically

Recyclerview is not displayed in fullscreen

Recyclerview is not displayed in fullscreen. Below is the layout file and the code I've written. After executing, the contents are displayed as shown in the below picture. The height is only the highlighted part. I want the contents to be fullscreen. I remaining contents are within this highlighted area which is scrollable. I want the contents to be displayed in fullscreen. Any help would be helpful.
Layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/activity_search"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:clickable="true">
<TextView
android:id="#+id/title"
android:textSize="16dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/genre"
android:layout_below="#id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/year"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>
Code:
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class setSearchData extends RecyclerView.Adapter<setSearchData.MyViewHolder> {
private List<SearchDisplayContents> moviesList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, year, genre;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
genre = (TextView) view.findViewById(R.id.genre);
year = (TextView) view.findViewById(R.id.year);
}
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.activity_searchresults, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
SearchDisplayContents movie = moviesList.get(position);
holder.title.setText(movie.getTitle());
holder.genre.setText(movie.getGenre());
holder.year.setText(movie.getYear());
}
#Override
public int getItemCount() {
return moviesList.size();
}
}
Calling function code:
List<SearchDisplayContents> movieList = new ArrayList<>();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
setSearchData mAdapter = new setSearchData(movieList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
SearchDisplayContents movie = new SearchDisplayContents("Mad Max: Fury Road", "Action & Adventure", "2015");
movieList.add(movie);
movie = new SearchDisplayContents("Inside Out", "Animation, Kids & Family", "2015");
movieList.add(movie);
mAdapter.notifyDataSetChanged();
You should use different layout for your viewholder. Take the RelativeLayout you have there, cut it out, put it in different xml file then pass it to onCreateViewHolder.
From look at your code - it looks like what is in the preview screenshot is the same as what is within the RelativeLayout you have staticly defined.
There is an issue with your approach. It is that you are referencing the RelativeLayout (within the same xml hierarchy as your RecylerView) as the CellViewHolder in the RecyclerView.Adapter. My approach personally is to create a seperate xml layout for the cell viewholder.
Follow this guide, it is very detailed:
https://guides.codepath.com/android/using-the-recyclerview
You should use different layout for your viewholder. Take the RelativeLayout you have there, cut it out, put it in different xml file then pass it to onCreateViewHolder.
The above coding has something work use different layout mean one for Recycleview and other for contents in list
main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/uslist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="#dimen/_49sdp"
android:divider="#android:color/transparent" />
</RelativeLayout>
list_content.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:clickable="true">
<TextView
android:id="#+id/title"
android:textSize="16dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/genre"
android:layout_below="#id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/year"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content" />
</RelativeLayout>
Adapter.java
public class yourAdapter extends RecyclerView.Adapter<yourAdapter .SimpleViewHolder> {
ArrayList<UsageRPDetails> mylist;
private Context mContext;
public yourAdapter (Context context, ArrayList<yourArray or model> checklist) {
mContext = context;
mylist = checklist;
}
#Override
public yourAdapter .SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_content, parent, false);
return new yourAdapter .SimpleViewHolder(view);
}
#Override
public void onBindViewHolder(final yourAdapter .SimpleViewHolder holder, final int position) {
holder.title.setText();
holder.year.setText();
holder.genre.setText();
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public int getItemCount() {
return mylist.size();
}
#Override
public int getItemViewType(int i) {
return 0;
}
public static class SimpleViewHolder extends RecyclerView.ViewHolder {
#Bind(R.id.title)
TextView title;
#Bind(R.id.genre)
TextView genre;
#Bind(R.id.year)
TextView year;
public SimpleViewHolder(View itemView) {
super(itemView);
title= (TextView) itemView.findViewById(R.id.title);
genre= (TextView) itemView.findViewById(R.id.genre);
genre= (TextView) itemView.findViewById(R.id.genre);
}
}
}
Also write a java file for Activity that calls this adapter class and call the recycleview in this activity

RecyclerView scrolling makes changes

I have a RecyclerView which has a custom adapter that inflates a rowLayout. Each Row contains a CardView and in it some text and images as buttons. In particular i have a "like" button(ImageView) that as for now only is supposed to change imageResource on click.
I was able to set an onClicklistener in the onBindViewHolder() method and it does indeed register when i click the button, however it not only changes that buttons image but it also changes some of the other like buttons and also when i scroll down and then up again the first like button sometimes get reset.
Here is my adapter code.
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> implements View.OnClickListener {
private ArrayList<WallPost> wallPosts;
#Override
public void onClick(View v) {
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public View view;
public ViewHolder(View v) {
super(v);
view = v;
}
}
public MyAdapter(ArrayList<WallPost> wallPosts) {
this.wallPosts = wallPosts;
}
#Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_layout, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
TextView name = (TextView) holder.view.findViewById(R.id.person_name);
TextView age = (TextView) holder.view.findViewById(R.id.person_age);
ImageView profile = (ImageView) holder.view.findViewById(R.id.person_photo);
TextView description = (TextView) holder.view.findViewById(R.id.description_text);
AppCompatImageView mainImage = (AppCompatImageView) holder.view.findViewById(R.id.main_image);
final ImageView likeButton = (ImageView) holder.view.findViewById(R.id.like_button);
name.setText(wallPosts.get(position).getName());
age.setText(wallPosts.get(position).getAge());
profile.setImageResource(wallPosts.get(position).getPhotoId());
description.setText(wallPosts.get(position).getDescription());
mainImage.setImageResource(wallPosts.get(position).getMainPhotoId());
likeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
likeButton.setImageResource(R.drawable.favourite_red);
}
});
}
#Override
public int getItemCount() {
return wallPosts.size();
}
}
Here is the item layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cv"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="10dp"
card_view:cardCornerRadius="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
>
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:id="#+id/person_photo"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
android:src="#drawable/placeholderprofilepic"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/person_name"
android:text="Name"
android:layout_toRightOf="#+id/person_photo"
android:layout_alignParentTop="true"
android:textSize="30sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/person_age"
android:text="19"
android:layout_toRightOf="#+id/person_photo"
android:layout_below="#+id/person_name"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/description_text"
android:text="This is the description of the image"
android:layout_below="#id/person_age"/>
<android.support.v7.widget.AppCompatImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/main_image"
android:src="#drawable/placeholderfoodimage"
android:layout_below="#+id/description_text"
android:elevation="4dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/like_button"
android:src="#drawable/favoruite_hollow"
android:layout_marginTop="10dp"
android:layout_below="#+id/main_image"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
This is due to recycling of views . You need to modify your getView() method and do these 2 things -
Modify your WallPost class to have a flag to set the like flag. Set this flag in onClickListener and update your wallPosts dataset .
wallPosts.get(position).setLike(true);
Set the like/not like image resource everytime based on the like flag you own in your list objects -
if(wallPosts.get(position).isLiked()){
likeButton.setImageResource(R.drawable.favourite_red);
} else{
likeButton.setImageResource(R.drawable.favoruite_hollow);
}

Categories