I have a recyclerView behanving like a list, how do I change the background color of only one view? The item I want to change the color's ID is "1", I thought about getting it in the activity by it's ID and add it a decorator, but I don't know how to, or if it's even possible.
Inside the adapter:
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.setEventNameName(i + "");
holder.settheColor(Color.parseColor("#000000"));
}
static int i;
class ViewHolder extends RecyclerView.ViewHolder{
public TextView eventName;
public RelativeLayout theLayout;
public ViewHolder(final View itemView) {
super(itemView);
eventName = (TextView)itemView.findViewById(R.id.eventName);
theLayout = (RelativeLayout)itemView.findViewById(R.id.backgroundevent);
theLayout.setId(++i);
theLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getAdapterPosition()>0){
removeItem(getAdapterPosition());
}else {
addItem("");
}
}
});
}
public void settheColor(int theColor){
theLayout.setBackgroundColor(Color.parseColor(String.valueOf(theColor)));
}
public void setEventNameName(String TheEventName){
eventName.setText(TheEventName);
}
}
I feel you can do it in this way --
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
currentListItem = List.get(position);
if(position == 0) //this will let you know the first child item
{
holder.textview.setBackgroundColor(Color.RED);
}
}
Hope this helps!
Pretty simple, use the onBindViewHolder method of your adapter, and change the color of the view (which should be referenced in your ViewHolder)
Follow this example if you are new to the Recyclerview concept.
Related
Good day, I want to pass position from RecyclerView's adapter to MainActivity, so I design a interface:
in RecyclerViewAdapter:
publuc interface recyclerViewAdapterListener{
void onDeviceConnect(data, position)
}
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
//here I call onDeviceConnect
holder.button.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
listener.onDeviceConnect(data, position);
}
in MainActicity:
private int pos;
public void onDeviceConnect(data,position){
pos = position;
}
I know the problem is pos = position, that will be always get the last position.
but I need data and position from RecyclerView, because when my device connected,
It will change RecyclerView's item.
ex : (in MainActivity)
case BleService.ACTION_NOTIFY_ON: //here my device is connected.
updateStatus(mac,status); //update status
private void updateStatus(mac,status){
Adapter.updateItem(data, pos); //call adapter's item
}
ex : (in RecyclerViewAdapter)
public void updateItem(data,pos){
if (dataList.size() != 0){
dataList.set(pos, data);
notifyItemChanged(pos);
}
is any good idea can fix it , Thanks.
in MainActicity:
`
public class MainActicity extends AppCompatActivity implements YourRecyleAdaptor.recyclerViewAdapterListener{
private int pos;
#Override
public void onDeviceConnect(data,position){
pos = position;
}
`
Try to replace the position with holder.getAdapterPosition()
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
//here I call onDeviceConnect
holder.button.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
listener.onDeviceConnect(data, holder.getAdapterPosition());
}
Or you can also setOnClickListener in the constructor of the ViewHolder class
I'm new in android and I have just screwed something.
I've used recycleView template and it worked fine but I wanted to change type of my object from string to "TagsManagerObject" which contains
private String tagName;
private String gender;
private String mAgeMin;
private String mAgeMax;
private String mDistance;
since then I'm stuck with this error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tinderapp, PID: 10921
java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.tinderapp.Tags.TagsManagerAdapter$ItemClickListener.onDeleteClick(int)' on a null object reference
at com.example.tinderapp.Tags.TagsManagerAdapter$ViewHolder$1.onClick(TagsManagerAdapter.java:78)
at android.view.View.performClick(View.java:6614)
at android.view.View.performClickInternal(View.java:6587)
at android.view.View.access$3100(View.java:787)
at android.view.View$PerformClick.run(View.java:26122)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6831)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:927)
My adapter looks like:
public class TagsManagerAdapter extends RecyclerView.Adapter<TagsManagerAdapter.ViewHolder>{
private LayoutInflater mInflater;
private ImageView mDeleteImage;
private List<TagsManagerObject> mTagsManagerObject;
private ItemClickListener mItemClickListener;
// data is passed into the constructor
public TagsManagerAdapter(Context context,List<TagsManagerObject> TagsManagerObject) {
this.mInflater = LayoutInflater.from(context);
this.mTagsManagerObject = TagsManagerObject;
}
// inflates the row layout from xml when needed
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.item_tags_manager, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
// String tag = TagsManagerObject.get(position);
holder.tagName.setText("#"+mTagsManagerObject.get(position).getTagName());
holder.gender.setText(mTagsManagerObject.get(position).getGender());
holder.distance.setText(mTagsManagerObject.get(position).getmDistance());
holder.tagAge.setText(mTagsManagerObject.get(position).getmAgeMin() + "-" + mTagsManagerObject.get(position).getmAgeMax());
}
// binds the data to the TextView in each row
// total number of rows
#Override
public int getItemCount() {
return mTagsManagerObject.size();
}
public void setClickListener(ItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView tagName,gender,tagAge,distance;
ViewHolder(View itemView) {
super(itemView);
tagName= itemView.findViewById(R.id.tag);
gender = itemView.findViewById(R.id.tag_gender);
distance = itemView.findViewById(R.id.tag_distance);
tagAge = itemView.findViewById(R.id.tag_age);
mDeleteImage = itemView.findViewById(R.id.tag_delete);
itemView.setOnClickListener(this);
mDeleteImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = getAdapterPosition();
if(position!=RecyclerView.NO_POSITION){
mItemClickListener.onDeleteClick(position); <<--- HERE IS THE ERROR
}
}
});
}
#Override
public void onClick(View view) {
if (mItemClickListener != null) mItemClickListener.onItemClick(view, getAdapterPosition());
}
}
// convenience method for getting data at click position
String getItem(int id) {
return mTagsManagerObject.get(id).toString();
}
// allows clicks events to be caught
// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
void onDeleteClick(int position);
}
}
So when i click delete button it crashes. Does anyone knows why?
Thanks.
You got this exception as you you call onDeleteClick(position) on an object that is null, so you have to set a value to the mItemClickListener before calling this method onDeleteClick(position)
In your code, you have to call setClickListener() of your adapter in your activity/fragment that uses this adapter.
You need to do two things.
First, you need to check mItemClickListener for null. Doing this will prevent from null pointer exception.
mDeleteImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = getAdapterPosition();
if(position!=RecyclerView.NO_POSITION){
if(mItemClickListener !=null){
mItemClickListener.onDeleteClick(position); <<--- HERE IS THE ERROR
}
}
}
}
);
And the second thing is you should set/call the setClickListener for the Activity/Fragment, using the instance of RecyclerviewAdapter.
Doing this will make the Activity/Fragment implement your ItemClickListener and onDeleteClick() method.
adapter.setClickListener(this);
Or, you can also use the Anonymous class here.
Thanks guys for help. The problem was that in my acvitivy I have:
1.Created adapter with empty taglist
2.set onclicklistener
adapter.setOnItemClickListener(new TagsManagerAdapter.OnItemClickListener() {
#Override
public void onItemClick(int position) {
}
#Override
public void onDeleteClick(int position) {
System.out.println("works?");
removeItem(position);
}
});
And then add items.
Instead
adapter.notifyDataSetChanged();
Ive used:
adapter = new TagsManagerAdapter(myTagsList);
and that caused problem, that it coudnt find my listener
in my adapter I have the problem that if I have defined a String before the onClickListener I cannot change it later in the onClickListener. I can set it to invisible which works perfect however I cannot change it.
I want that if a user clicks on the image, a second image should replace the first one, but I cannot either change the image in a imageview, I suggest that it is the same problem as with the String which I cannot change.
Thanks for any help!
private List<cards> listItems;
private Context context;
public arrayAdapter(List<cards> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
return new ViewHolder(v);
}
#SuppressLint("SetTextI18n")
#Override
public void onBindViewHolder(#NonNull final ViewHolder viewHolder, int i) {
final cards currentItem = listItems.get(i);
viewHolder.name.setText(currentItem.getName());
viewHolder.comment.setText(currentItem.getComment());
Picasso.get().load(currentItem.getProfileImageUrl()).into(viewHolder.image);
viewHolder.name.setText("this work"); // here it works
viewHolder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Try this
currentItem.setName("New text");
notifyDataSetChanged();
//viewHolder.name.setText("New text"); // this does not work
// viewHolder.name.setVisibility(View.INVISIBLE); this does work
Picasso.get().load(currentItem.getProfileImageUrl2()).into(viewHolder.image); // does not work
}
});
}
#Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name, comment, howMany;
public ImageView image;
/**
* #param itemView
*/
ViewHolder(#NonNull View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
image = (ImageView) itemView.findViewById(R.id.image);
comment = (TextView) itemView.findViewById(R.id.commentText);
}
}
}
You should call notifyItemChanged with the corresponding position.
#Override
public void onBindViewHolder(final ViewHolder viewHolder, int position) {
final cards currentItem = listItems.get(i);
viewHolder.name.setText(currentItem.getName());
viewHolder.comment.setText(currentItem.getComment());
Picasso.get().load(currentItem.getProfileImageUrl()).into(viewHolder.image);
viewHolder.name.setText("this work"); // here it works
viewHolder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewHolder.name.setText("New text"); // this does not work
// viewHolder.name.setVisibility(View.INVISIBLE); this does work
Picasso.get().load(currentItem.getProfileImageUrl2()).into(viewHolder.image); // does not work
notifyItemChanged(position);
}
});
}
change value in you list at particular position and then notifyItemChanged(position);
in your click event write this:
currentItem.setName("abc");
notifyItemChanged(position)
In the data model you have keep a variable which indicates the current profile image.
for a example
class cards {
String name;
String comment;
....
boolean profileImageChanged;
}
then inside onBindViewHolder
#SuppressLint("SetTextI18n")
#Override
public void onBindViewHolder(#NonNull final ViewHolder viewHolder, int i) {
....
if(currentItem.profileImageChanged){
Picasso.get()
.load(currentItem.getProfileImageUrl()).into(viewHolder.image);
} else {
Picasso.get()
.load(currentItem.getProfileImageUrl2()).into(viewHolder.image);
}
viewHolder.name.setText("this work"); // here it works
viewHolder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewHolder.name.setText("New text"); // this does not work
// viewHolder.name.setVisibility(View.INVISIBLE); this does work
notifyItemChanged(position);
}
});
}
Hi you can go with this approach also.
#Override
public void onBindViewHolder(final ViewHolder viewHolder, int position) {
final cards currentItem = listItems.get(i);
viewHolder.name.setText(currentItem.getName());
viewHolder.comment.setText(currentItem.getComment());
Picasso.get().load(currentItem.getProfileImageUrl()).into(viewHolder.image);
viewHolder.name.setText("this work"); // here it works
viewHolder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ViewHolder viewNewHolder=viewHolder
viewNewHolder.name.setText("New text");
Picasso.get().load(currentItem.getProfileImageUrl2()).into(viewNewHolder.image);
}
});
}
I have used it its working.
*******All The Best*********
Out of the recyclerview item, I need to select only one and make its background white and deselect the previous one.
public class viewHolder extends RecyclerView.ViewHolder {
TextView periodCategoryName;
ImageView periodCategoryPhoto;
View dottedLine;
public viewHolder(View itemView) {
super(itemView);
periodCategoryPhoto=(ImageView) itemView.findViewById(R.id.mood_icon);
periodCategoryPhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("Get Adapeter position ",Integer.toString(getAdapterPosition()));
selectedPosition=getAdapterPosition();
notifyItemRangeChanged(0,periodListPhoto.size()-1);
}
});
}
}
#Override
public void onBindViewHolder(final PeriodListAdaptor.viewHolder holder, final int position) {
holder.periodCategoryPhoto.setImageResource(periodListPhoto.get(position));
//only one period item is highlighted
if(selectedPosition == position){
//already selected item
Log.i("Selected position is ",Integer.toString(selectedPosition));
holder.periodCategoryPhoto.setBackground(ContextCompat.getDrawable(mContext,R.drawable.item_selected));
}else{
//do nothing
}
}
I am getting the strange behavior. Sometimes previously selected item is deselected upon selecting a new item. Sometimes both are selected.
ViewHolders are being reused, which means that you should take care of handling both cases in your onBindViewHolder():
if (selectedPosition == position) {
...
} else {
holder.periodCategoryPhoto.setBackground(defaultBagroundColor);
}
I have use the Switch in the RecyclerView. It have facing the issue of recycling behaviour. When I switch on the 1st position ,it automatically on the switch at 10 postion ... I think it due to reuse of the view. How to fix it. find the screenshot:
https://www.dropbox.com/s/4ms2jf9e28fyc7u/error.png?dl=0
private void setAdapter(ArrayList data) {
ManageCategoryAdapter adapter = new ManageCategoryAdapter(data);
adapter.SetOnItemClickListener(listClick);
mRecyclerView.setAdapter(adapter);
}
public class ManageCategoryAdapter extends RecyclerView.Adapter<ManageCategoryAdapter.ViewHolder> {
private ArrayList<String> catData=new ArrayList<>();
private OnItemClickListener mItemClickListener;
public ManageCategoryAdapter(ArrayList<String> listadap) {
catData=listadap;
System.out.println("$$$$$$$$$"+"adapterclass");
}
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_manage_list, parent, false);
return new ViewHolder(v);
}
public void onBindViewHolder(ViewHolder holder, int position) {
holder.category.setText(catData.get(position));
}
public int getItemCount() {
return catData.size();
}
public void onClick(View view) {
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView category;
public Switch switchClick;
public ViewHolder(View itemView) {
super(itemView);
category=(TextView)itemView.findViewById(R.id.cat_text);
switchClick=(Switch)itemView.findViewById(R.id.switch_btn);
switchClick.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(v, getPosition());
}
}
}
public void myNotifyDataSetChanged(ArrayList list)
{
System.out.println("$$$notify");
catData.addAll(list);
this.notifyDataSetChanged();
}
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
}
This how I set the adapter class
You need to use this in the adapter:
#Override
public int getItemViewType(int position) {
return position;
}
This is a very common problem with RecyclerView and there are lots of answers there in Stackoverflow.
You already have understood your problem i.e. reusing the views. So you might take a look at these answers to get a better idea about how you can overcome it.
Put an else condition everywhere you need to update a view of a list
item dynamically.
Using another list to keep track of the list items in which the
Switch is enabled or disabled. You can see this answer here.
These will do the trick for you I hope.