How to update RecyclerView item without animation? - java

I have a RecyclerView. When I click a button inside an item in RecyclerView, I want to change the color of a View in that item. The following is my code and it works fine. But, the problem is the item will have an animation which is ugly. I want to update the item without the animation. How should I do that? By the way, I don't want to turn off the animation, only for this click event.
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ImageView imageView;
public Button button;
public ItemViewHolder(View view) {
super(view);
//do something
button.setOnClickListener(this);
}
#Override
public void onClick(View view) {
//change color
notifyItemChanged(getAdapterPosition());
}
}

Try this
notifyItemChanged(position, Object);
This will update the position without animating it as we are passing our Object in it.
Try this and do let me know.
For Kotlin you can use
notifyItemChanged(int position, #Nullable Object payload)

Based on the Rakshit's answer, in Kotlin 1.2 the following code works fine:
notifyItemChanged(position, Unit)

There is a dedicated method to disable just item changed animations:
((SimpleItemAnimator) myRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
Ref.: https://developer.android.com/reference/androidx/recyclerview/widget/SimpleItemAnimator

in kotlin : recyclerView.itemAnimator = null
in java : recyclerView.setItemAnimator(null);
developer said :
A null return value indicates that there is no animator and that item changes will happen without any animations.
https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#getItemAnimator()

recyclerView.getItemAnimator().setChangeDuration(0);
Or this.

Try this
csRecyclerView.getItemAnimator().setChangeDuration(0);
for more information RecyclerView.ItemAnimator

Try this:
public ItemViewHolder(View view) {
super(view);
//do something
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
holder.itemView.setBackgroundColor(Color.parseColor("#000000"));
notifyDataSetChanged();
});;
}
}

Related

Why won't my new activity start on startActivity(intent)? [duplicate]

I'm trying to implement a way to handle item selection on a RecyclerView. I personally don't like the way suggested in some answers on SO of passing through gestures, and I thought that implementing an OnClickListener, as suggested here and here, was waaay cleaner.
The fact is that... this pattern doesn't actually work! I'm really not able to understand why my OnClickListener.onClick is never called. It's kinda like another method intercepts the click before onClick can take care of it.
This is my code:
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView tvName;
ImageView star;
public ViewHolder(View itemView) {
super(itemView);
tvName = (TextView) itemView.findViewById(R.id.CHAT_ITEM_name);
star = (ImageView) itemView.findViewById(R.id.CHAT_ITEM_star);
Fonts.setTypeface(tvName, regular);
}
#Override
public void onClick(View view) {
int position = getLayoutPosition();
select(position);
}
}
Unfortunately it's very important for me to able to access the position of the clicked item in the whole dataset, in order to remove it, so doing something like indexOfChild isn't acceptable too: I tried, but this method gives you the position of the item in the visibile part of the list, thus making list.remove(position) impossible.
Looking at the updated code: you are not setting the onClickListener to any of the views in the ViewHolder. It is an understandable mistake to forget the click listener.
Just use:
tvName.setOnClickListener(this);
star.setOnClickListener(this);
You can set to both or just one of them. You can also simply get the parent layout of these two views, so that the whole item itself in the adapter can be clickable.
itemView.setOnClickListener(this);
You can do it in your onBindViewHolder
#Override
public void onBindViewHolder(ReportViewHolder holder, int position {
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// handle your click here.
} });
}
Simplely Click Handler your ViewHolder. Recycler View don't have special attaching click handlers like ListView which has the method setOnItemClickListener().
** public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
** in public ViewHolder(Context context, View itemView) set public void onClick(View view)
** get position by: int position = getLayoutPosition(); User user = users.get(position);

"Cannot resolve symbol ItemClickListener"

May i know why is it showing me the error of "Cannot resolve symbol ItemClickListener"? Do i need to add library or something in order to solve this issue or i shouldn't declare it in here?
public static class FoodViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private ItemClickListener itemClickListener;
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
View view;
public FoodViewHolder (View v){
super(v);
view= v;
}
public void setName(String title){
TextView post_title = (TextView)view.findViewById(R.id.Menu_Name);
post_title.setText(title);
}
public void setImage(Context ctx, String image){
ImageView menuImage = (ImageView) view.findViewById(R.id.Menu_Image);
Picasso.with(ctx).load(image).into(menuImage);
}
#Override
public void onClick(View v) {
}
}
In General for RecyclerView we create an Interface for handling the click events. Unlike a normal Button Click, the RecyclerView Click events cannot be handled directly. As the RecyclerView is the Adapter(Data Provider to the View), You cannot directly handle the item clicks from here and update the view. For this you need an Separate Interface which is ItemClickListener in your case (Create an Interface file separately in your project). In that Interface you need to declare a method for example something like this
public Interface ItemClickListener{
void onRecyclerViewItemClicked(int position);
}
Create a OnClickListener for your View(which is present over single row. Eg: image, text, etc);#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.myText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
itemClickListener.onRecyclerViewItemClicked(position);
//itemClickListener is the Interface Reference Variable
}
});
}
And in your Activity you need to implement this Interface like
public class YourActivity extends AppCompatActivity implements ItemClickListener {
....
....
protected void onCreate(Bundle savedInstanceState) {
...
...
}
#Override
public void onRecyclerViewItemClicked(int position) {
//You will get the position of the Item Clicked over recycler view
//You can handle as per your requirement
}
}
Upon doing this you will listen the click event of recyclerview to the activity. Then you can handle it accordingly.
If you have further doubt follow the references:
https://stackoverflow.com/a/40584425/8331006
https://stackoverflow.com/a/28304164/8331006
you can set the click listener to any view you want you don't need to declare it as a seperate
yourView.setOnClickListener(this);
and in your onClick(View v) add the code that you want whenever you click the view you want for example :
if(v==imageView){
//write your code here
}

How can I get the text from EditText of a recyclerview item when user clicks on a button of the same item?

I am designing a RecyclerView list. Each item of the Recyclerview contains a LinearLayout. This LinearLayout contains two views, the first one is an EditText and the second one is Button. When user taps on the button, it fires an onclick event. From onClick listener, I need to get the content of the EditText. I don't find a way to access the content of a sibling view when the user taps on another sibling view.
My question is not "how can I set on click listener to a button inside adapter". Most of the people answered how to set onClick listener to a button which is there inside the recyclerview item. My question is bit different, when I am inside onClick method which is fired from button, how will I access the edittext which is a sibling of button. Every item has one edittext, so when I click on a button how will I find the correct edittext?
For example, I have a recylerview of size 10. And each item of recyclerview contains a LinearLayout and inside linearlayout two item, one is an Edittext and the other one is a Button. when I tap on 7th items button, how will I get the text of 7th item's Edittext? I hope I have explained it well
Any help would be appreciated.
First of, you need two references: one to your EditText and one to your Button. You can get those in your ViewHolder. Next, you need an OnClickListener. The ViewHolder can conveniently also implement one but you could also use onBindViewHolder() for that.
Inside that OnClickListener you can filter out your id with a switch statement if you want to and then get the content of the EditText like this:
switch(viewId) {
case R.id.buttonId:
String text = editText.getText().toString();
// do something with that text
return true;
}
In case you implemented an OnClickListener in your ViewHolder you can then do this button.setOnClickListener(this); inside your ViewHolder to make sure onClick() is actually called when you click the button.
EDIT:
Here's some sample code that should work for your case. I'm implementing View.OnClickListener here as mentioned above.
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
EditText editText;
Button button;
public ViewHolder(View itemView) {
super(itemView);
editText = itemView.findViewById(R.id.editText);
button = itemView.findViewById(R.id.button);
button.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.button:
String text = editText.getText().toString();
break;
}
}
}
This is what it would look like if you were to do it in your onBindViewHolder() (in this case you would NOT implement the OnClickListener in your ViewHolder obviously):
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String text = holder.editText.getText().toString();
}
});
}
Step 1 :Make an abstract function in your adapter.
abstract void onButtonClicked(String text);
Step 2: Declare your adapter Abstract.
Step 3: Override the method (onButtonClicked(String text);) in your activity were you have instantiated the adapter.
Step 4: In your adapter inside the onClickListener for your button call the function :
onButtonClicked(editText.getText().toString());
and you'll get the string in your activity where you overrided the method.
You can use holder pattern for RecylcerView adapter. Then you can set click listener on your button and get the text from EditText.
public class SimpleViewHolder extends RecyclerView.ViewHolder {
private EditText simpleEditText;
private Button simpleButton;
public SimpleViewHolder(final View itemView) {
super(itemView);
simpleEditText = (EditText) itemView.findViewById(R.id.simple_edit_text);
simpleButton = (Button) itemView.findViewById(R.id.simple_button);
simpleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = simpleEditText.getText().toString()
}
});
}
}
You can do it inside your adapter which is being set on the recycler view.
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public MyViewHolder(View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.imageView);
}
}
And in the bindview holder you can access the views
public void onBindViewHolder(#NonNull final ImageAdapter.MyViewHolder holder, int position) {
holder.mImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Your Code to access the edit text content
}
});
}
use a custom listener like this in the holder:
public class SimpleViewHolder extends RecyclerView.ViewHolder {
private EditText simpleEditText;
private Button simpleButton;
public SimpleViewHolder(final View itemView, final OnItemSelectedListener listener) {
super(itemView);
simpleEditText = (EditText) itemView.findViewById(R.id.simple_edit_text);
simpleButton = (Button) itemView.findViewById(R.id.simple_button);
simpleButton.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
String text = simpleEditText.getText().toString();
if(listener != null) listener.onItemSelected(text);
}
});
}
public interface OnItemSelectedListener{
void onItemSelected(String value);
}
}
Simply Use Your ViewHolder. It contains all the children you want. Implement the code inside your adapter where each item is inflated. Here is an example.
//inside the onBindViewHolder
viewHolder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = viewHolder.editText.getText().toString();
Log.d("output", text);
}
});
If you are using FirebaseUI Implement this inside the populateViewHolder for older version and onBindViewHolder for the later versions.
People put -1 when they don't know the answer or when they do not understand question. So funny!! In onclick I took parent(getParent()) from the view and accessed the second child of the parent. With that I am able to access the content of the sibling.
` public void onClick(View v) {
for(int i = 0;i<parent.getChildCount();i++){
if(parent.getChildAt(i)instanceof EditText){
passwordView = (EditText)parent.getChildAt(i);
}
}
}`

Recyclerview :- Multiple items get selectied when one item selected after scrolled

#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
final XContacts mContact = visibleObjects.get(position);
holder.Name.setText(mContact.getName());
holder.InviteTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.InviteTextView.setText("INVITED");
}
});
}
holder.Name.setText :- Here i am names to recyclerview
holder.InviteTextView.setOnClickListener :- When I click on one item[invite]. After I scroll down multiple items are get invited without that item being clicked on.
My problem is:
Abninav kashayp invited if I scroll down I get problems
RecyclerView, as the name says, is recycling views, that's why you are seeing 'INVITED' in other views.
In order to fix the issue, in onClickListener you should set a flag in your XContacts object:
mContact.setInvited(true);
Then you should change your onBindViewHolder code to also set the InviteTextView, just after setting the Name:
if (mContact.isInvited()) {
holder.InviteTextView.setText("INVITED");
}
else {
holder.InviteTextView.setText("INVITE");
}

RecyclerView Onclick (call non-static method)

I've got stuck with an issue about setting an OnItemClickListener to my RecyclerView items. I tried to set a listener the way described in the RecyclerView sample of Android Studio. So a listener is set in the ViewHolder class for my RecyclerView.
public class ProgramViewHolder extends RecyclerView.ViewHolder {
protected TextView vName;
protected ImageView vProgramImage;
public ProgramViewHolder(View v) {
super(v);
vName = (TextView) v.findViewById(R.id.programName);
vProgramImage = (ImageView) v.findViewById(R.id.programImage);
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// HERE PROBLEM !!
MainActivity.openSettings(1);
}
});
}
}
Now I want to call a method of my MainActivity openSettings(int ) to load a fragment:
public void openSettings(int layoutId) {
settingsFragment setFrag = new settingsFragment();
Bundle information = new Bundle();
information.putInt("layoutId", layoutId);
setFrag.setArguments(information);
getFragmentManager().beginTransaction()
.replace(R.id.fragmentContainer, setFrag)
.commit();
}
But now the problem. When I try to compile, it says "Non-static method 'openSettings(int )' cannot be referenced from a static context."
I quite not understand this error. Why is it a static context? The class ProgramViewHolder ist not declared static.
And the most important part: How can I fix it? I want to set a OnClickListener to every item of RecyclerView and call a public method of MainActivity.
Thanks a lot to you, for your time spending to help me.
It's not that ProgramViewHolder is static, it's because attempting to call your activity from a static context (you aren't calling a specific instance of the activity).
What you should do is pass the activity into your recyclerViewAdapter so that you have access to it.
For example
MainActivity mainActivity;
public CustomRecyclerViewAdapter(MainActivity mainActivity) {
this.mainActivity = mainActivity;
}
And to create the recyclerViewAdapter from MainActivity
CustomRecyclerView recyclerViewAdapter = new CustomRecyclerViewAdapter(this);
recyclerViewAdapter.setAdapter(recyclerViewAdapter);
You should then be able to access your method like this
mainActivity.openSettings(1);
Let me know if you have any trouble
//Edit
Here's how you would set onClick from bindViewHolder. You want to set up any onClickListeners here due to the way RecyclerView "recycles" data. For example, if each row should perform a different action on click, you need to make sure the click listener is tied to the specific row. Creating this in onBindViewHolder ensures this. If you want an entire row to be clickable, rather than elements inside, just create an outer view that fills the entire row. Then tie the onClickListener to that.
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
ProgramViewHolder programViewHolder = (ProgramViewHolder) holder;
programViewHolder.vName.setOnClicklistener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mainActivity.openSettings(1);
}
});
}
if you have context of the activity containing recyclerView, then you can simply do this:
your_view_holder.v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// HERE SOLUTION!!
((MainActivity)context).openSettings(1);
}
});
You can place this in onBindViewHolder(...)
How to get context:
Create another parameter of context in your Adapter's constructor , and pass the context from your activity once instantiating Adapter .
why pass the context:
i would recommend you to always pass context and assign it to any adapter's variable because this is something you would require every now and then while working with your adapter, so instead of using a workaround every time for context, just save it once .

Categories