if each item of recycler view has multiple items (Button and textview)
I have interface to handle Recycler's item click , I want upon clicking Recycler view item , be able to update text view and button but from outside of the adapter class ,
how could I achieve this ?
thanks
call adapter.notifyItemChanged(position) on the adapter to change only a single object
EDIT: Based on your comment:
you don't need reference of textview. Just update the state of the item in the list at that position.
For exmaple, take a boolean, isclicked and mark it as true for position = x
Hadle the text and UI inside the onbindviewholder only based on the
isClicked = true or false.
You can use AdapterObject.NotifyDataSetChanged instead of setting the adapter again after the data in list gets updated.
Related
So, I have a recyclerview inside a recyclerview. What I wanna do is click the image inside the first item (at position 0) in the child recyclerview. How am i gonna do that? Nothing works in every answer. Thank you.
So, I have a recyclerview inside a recyclerview. What I wanna do is click the image inside the first item (at position 0) in the child recyclerview.
Consulting the documentation we see there are a few options.
So you want to perform an action on the main recycler. And that action is to perform another recycler view action on a child recycler. And that action it to click an image. In Espresso terms, that translates to something like:
// Starting from the bottom up, you want to click on the image
val clickOnImage = actionOnItem(withId(R.id.image_view), click())
// Next you want to do the above click action on the specific view in the child recycler
val clickOnImageOnChildRecycler = actionOnItemAtPosition(0, clickOnImage)
// Next you want to do the above on the specific child recycler
val clickOnChildReycler = actionOnItemAtPosition(INDEX_OF_CHILD_RECYCLER, clickOnImageOnChildRecycler)
// Finally, you want to do the clicking of a child on the main recycler
onView(withId(R.id.parent_recycler_view)).perform(clickOnChildReycler)
Then you can simplify and inline:
onView(withId(R.id.parent_recycler_view)) // On the parent recycler ...
.perform(actionOnItemAtPosition(INDEX_OF_CHILD_RECYCLER, // On the specific child recycler ...
actionOnItemAtPosition(0, // On the first item ...
actionOnItem(withId(R.id.image_view), click()))) // Click the image ...
Should go without saying that I did not test this, but hopefully it works or at least sets you on the right path.
This is how I did it:
//Scroll to the 2nd Position of Parent Recyclerview
onView(withId(PARENT_RECYCLERVIEW_ID)).perform(actionOnItemAtPosition(1, scrollTo()));
//Added this line Just to make sure I'm already in the Child Recyclerview
onView(withId(PARENT_RECYCLERVIEW_ID)).perform(actionOnItemAtPosition(1, CustomViewAction.clickChildViewWithId(CHILD_RECYCLERVIEW_ID)));
//Click the Image inside the first item in the child recyclerview
onView(allOf(withId(CHILD_RECYCLERVIEW_ID), withParent(withRecyclerView(PARENT_RECYCLERVIEW_ID).atPosition(1)))).perform(actionOnItemAtPosition(0, click()));
How to change specific view in row in RecyclerView. Adapter.notifyDataSetChanged or Adapter.notifyItemChanged updating row. But I need change the specific view in row, without touching other views in this row.
First of all, use Glide (By Google) instead of Picasso. It's more efficient.
Secondly, your device won't download the same image again if it's already present inside your Cache memory.
Now,
Simply change the data inside your List and then update your adapter or recycler view.
Inside your AdapterClass
Create an Interface, let's say
public interface TheUpdater{
public void updateMyAdapter(int position);
}
Then implement this interface in your Activity and override this method.
Now, in order to get the position of the View.
You can override onClick() of a View (Button or even the Whole Parent View i.e. Layout) and perform the following statement.
mTappedPosition = getAdapterPosition();
listener.updateMyAdapter(mTappedPosition); //Update the adapter
Where mTappedPosition is your global variable.
Inside your Activity
#Override
updateMyAdapter(int position){
---- //Do stuff
mAdapter.notifyItemSetChanged(position);
}
you can use following code
MyRecyclerAdapter.ViewHolder viewHolder= (MyRecyclerAdapter.ViewHolder) recycleView.findViewHolderForAdapterPosition(1);
viewHolder.txtView.SetText("Some Text");
I think to avoid the image loading issue you have to use image caching technique. So that already loaded image won't be load again.
or
If the image is already loaded and each and every row is showing same image set then you can keep a boolean flag to your itemModel (The dto which has the adapter item's data ). and change that model and then call
yourListToAdapter(index).setFlag(showImage1); // showImage1 boolean flag
yourAdapter.notifyItemChanged(index);
I'm creating an Android application using Java in Android Studio. I need to create a list view where previously selected elements will be shown as selected when the list view is recreated. Multiple elements from the list view can be selected. The list view will first be created and we will keep track of what is saved in a list of Pair<String, boolean> where the String is the list view element and the boolean indicates whether or not the element was selected.
I handle the change of background color using
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
if(!tmpSelectedSkills.get(position).isSelected()){
tmpSelectedSkills.get(position).setIsSelected(true);
parent.getChildAt(position).setBackgroundColor(
Color.parseColor("#A9BCF5"));}
else{
tmpSelectedSkills.get(position).setIsSelected(false);
parent.getChildAt(position).setBackgroundColor(
Color.parseColor("#ffffff"));}
}
If the user closes the list view and choses to recreate it to change his choices I'd like to change the background color of his previous choices in the new list view before showing it to the user.
To do so I need to access the
View view
from the signature of onItemClick, which corresponds to the View of which I want to change the background color.
You can create a method updateData() in your adapter. In the YourObject of List<YourObject> define a fields boolean for know selected. When you clickOnItem of listview, set this fields to true and notifyDataChanged by method updateData(). In your adapter, check if fields == true, set backgroud for row. Sorry my bad english
I have objects in a list view each with a switch.
how do I identify which switch was pressed meaning which item it was included in. does list view have a method to tell me which object it was in the arraylist of the listview
You can easily know which item is clicked in your OnCheckedChangeListener() by setting a tag with some definite information about the current ListView item using button.setTag() (you can put whatever Object you want in this tag). You will need this in your custom Adapter.
Which you call back in your Listener using view.getTag(). This call will return the same Object. You can then use this method to have a distinct id or string (most often the content of the current item in your arrayList) in each separate ListView.
I'm thoroughly confused about the life cycle of list view. More specifically, what does the list adapter do exactly? Does it just provide data to the given view? And when/where does the getView() function gets called? And what purpose does this getView() function provide? From just looking at the code, it looks like getView() is "assigning" data to the view to be displayed. I'd like to be able to use list views without having to memorize, do this and then this in order for it to work. I'd much rather understand it so I can use it properly. Someone please help me understand all of this.
Also, if someone can explain to me.. what's the difference between BaseAdapter and ArrayAdapter? and any other kind of adapters that comes with Android.
What I have understood is your adapter constructor instantiated by activity and then on activity launch the getView() method is called. the {#param position, view, viewGroup}
position: it refers to the position of the view as given by adapter. Please Note it is different from the position in {OnItemClick(AdapterView adapter, View v, int position,long id)} here position is the list item position. The {position} in {getView()} changes after particular object in the list are displayed again for eg. when you scroll.
view: the view here is the view you want to be presented through getView(). It can be a particular XML layout for each row. So this states clearly that getView is called to plot every row. this view needs to be valid one or another layout (LinearLayout by default) will be selected to maintain uniqueness.
viewgroup: as you might know and as name says will be the container of your #param:view
any other point is appreciated.
getView() fills in the data into the item's view with the given index. The view which is given as a parameter may be a pre-inflated view. If it is not, you have to infalte it yourself.
An ArrayAdapter simply calls setText on the given view with the result of toString() of the object with the respective index from the array. If you override it, you can do more complex stuff, like assigning a picture or filling in more TextViews.
I recommend the following tutorial: http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
Hi list adaper provides view for listview.
when user scrolls listview at that time getview is called.
getview is used to populate your view with data hence the name adapter.
The Adapter does all the "rember to do this" for you. If you change a list view's backing data structure through the adapter's methods (e.g. "add()") it will fire all the datachanged and update events you'll need for the list view to show the new state of the data.