I am new to accessibility workarounds in android, please help me fix the issue with losing focus while deleting any item from recycler view. I used the code below for this, but it doesn't seem to be working.
Override method getItemId(), give a stable id to each item:
#Override
public long getItemId(int position) {
return position;
}
Set has StableId:
adapter.setHasStableIds(true);
Now basically you can keep focus in a concrete item when call notifyDatasetChange, if not, disable animator:
mRecyclerView.setItemAnimator(null);
from here
But it's not working. I just need the way around how we can keep the focus after deleting the item from recycler view. Thanks.
Related
I would like to show a custom animation to match the other animations that I have in my RecyclerView. I can’t find a straight forward way of adding custom animations for removal. I know that when RecyclerView replaced ListView one of the talking points was the ability to know when items are added or removed rather than simply saying the dataset has changed and updating everything, so I figured there would be an easy way to add an animation to a View when it is removed that I am missing.
When I add an animation to a view that is removed the animation will not play. This is likely because the View is removed and the animation stops. Is there an easy way to add an animation to a View that doesn’t get cut off? I'm assuming that I could use a second thread, but I want to know if I am missing something.
//code for removal
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//animation to be added here on view
list.remove(holder.getAdapterPosition());
notifyItemRemoved(position);
notifyItemRangeChanged(position,list.size()-position);
}
});
All add/change/delete/move animations in recyclerView are made by ItemAnimator (by default animator is DefaultItemAnimator). So if you need custom delete animation you should provide custom ItemAnimator via recyclerView.setItemAnimator(animator) method.
I suggest to extend from SimpleItemAnimator and override animateRemove method.
I want to catch a vertical swipe in an horizontal RecyclerView. Each item is simply a CircleImageView.
I found many resources on internet (like Drag and swipe with RecyclerView, Android - Swipe to delete RecyclerView) but those solutions ends deleting the item. I don't know if the term swipe requires also that the item is deleted or not.
What I want to achieve is to catch the swipe action on an item in the RecyclerView, but without delete the item itself from the RecyclerView.
I think that a good idea is to override the method onChildDraw() like suggested here: How to detect if Recyclerview item is being swiped?, but I can't understand how to achieve the behaviour I want.
My idea is: while the user swipes an item, the item itself moves in that direction; when the user end the touch event, the item has to come back to the original position (maybe changing the background color).
EDIT 1:
How to have swipe to delete and swipe to archive in two colours in Recyclerview Android probably can help, but it doesn't achieve the behaviour that I need. The item has to come back to the original position.
Your RecyclerView has RecyclerView.Adapter attached to it. The adapter determines what information that the RecyclerView can see and display. So, if item number 10, out of a 100-item backing array (managed by you) is swiped, the adapter can report that the array now contains 99 items and not ever present the swiped item to the RecyclerView. That way the item appears to be deleted but is maintained internally and still accessible programmatically. How you manage that internal state is up to you and dependent upon your implementation.
If, however you want to not remove the item from the screen but just change its appearance, I think that you would need to look at the method onItemDismiss that actually removes the item and notifies the adapter of the data change.
public void onItemDismiss(int position) {
mItems.remove(position);
notifyItemRemoved(position);
}
It is here that you would make the change. The item would stay in the adapter. You would also need to flag that position as "swiped" in case the view holders are recycle so you can maintain the visual "swiped" state.
public void onItemDismiss(int position) {
// Change background color, etc.
}
Take a look at code that has a "swipe to delete" function with "undo" for some ideas. Here is an example with a dialog that is called before deletion actually occurs if "Cancel" is clicked. There are many other examples of "undo" available. What you are trying to do can be considered to be an immediate and implicit "undo" with a visual change to the background.
If you want to have the item move back into position after a swipe, the following should work:
public void onItemDismiss(int position) {
notifyItemChanged(position);
}
I know it's a little bit generic. So I have a recyclerView, and inside it there are multiple items which can expand and collapse (each of them has a webview and expand and collapse to decide if the full content needs to be shown). I need to set images for a BiImageView in the viewAdapter like this
ItemViewHolder viewHolder = (ItemViewHolder) recyclerView
.findViewHolderForLayoutPosition(position);
viewHolder.rightIndicator.setImageResources()
However it only works when every item is collapsed, when the first item expand, it will take up the screen and other items will be hidden or say below the bottom of the screen. Then only the first few items can set the image resource. For other hidden items, the viewHolder is null. It looks like recyclerView removed them from layout, I'm wondering if there is any way to make sure I can get the viewHolder? Like some callback? I tried onCreate and onBind, but when I scroll down or collapse the first item, these two methods didn't get called.
I think, I can help you with some pseudo code, but I dont remember how to use it with view model. First of all, you have to change all your views in onBindViewHolder method in depend on your model, then in your model class you must have field, for example
boolean expanded = false; // initial state
And in your onBindViewHolder:
if (modelsList.get(position).isExpanded()){
//show expanded view
} else {
//show collapsed view
}
After it, you can set onClickListener to some view in your collapsed view, to expand:
view.setOnClickListener(new OnClickListener(){
public void onClick(View v){
modelsList.get(position).setIsExpanded(!modelsList.get(position).getIsExpanded());
MyAdapter.this.notifyItemChanged(position);
}
When you will click on this view, your model will change and RecyclerView will recreate view binded to this model with new data.
Hope it will help you
guys I am developing android TV app so I used recyclerview horizontally and vertically and I used a method to refresh adapter of vertical recyclerview by using adapter.notifyDataSetChanged() but I am getting one problem.
1. It's focus is being gone and on press any D-pad key it is not working only right key is working
2. I used adapter.notifyItemRangeChanged(0, categoryDataList.size()) but I have still one problem that it's focus is going on first element of layout otherwise last of layout.
So Please help me as soon as possible.
Thanks in advance.
Of course item will lose focus. Because no view to get focus when you refresh RecyclerView by calling method notify*Changed.
There is an imperfect way to keep focus in RecyclerView when you call notifyDatasetChanged().
Override method getItemId(), give a stable id to each item:
#Override public long getItemId(int position) { return position; }
Set has StableId:
adapter.setHasStableIds(true);
Now basically you can keep focus in a concrete item when call notifyDatasetChange, if not, disable animator:
mRecyclerView.setItemAnimator(null);
about stable id:Android: How to make an adapter with stable ids?
about disable animator:How to implement ItemAnimator of RecyclerView to disable the animation of notifyItemChanged
about the reason of step3 in google code:
https://code.google.com/p/android/issues/detail?id=204277
Good luck!
Use
notifyItemRangeInserted(position,size);
if you are inserting items.
Im using nhaarman's ListviewAnimation library https://github.com/nhaarman/ListViewAnimations which works great.
Though I have difficulty tweaking one of his options, which is ExpandableListview. I want to tweak it so that only 1 child view (content view)is visible at a time. So when expanding a parent view (title view) item it should close the previous one. I can't seem to update (notify) my adapter when a child view is visible (is expanded). I have a custom adapter which extends ExpandableListItemAdapter.
This is the class here .
Each item is set with the TitleViewOnClickListener, which handles the expanding ad collapsing of the content view.
Now I would like to collapse all visible child views and keep the selected open. Could anyone here help me or guide me in the right direction?
Next to that I can't seem to get an onlistitem click.
Thank you in advance
I've added a setLimit(int) function to the ExpandableListItemAdapter class. When the (limit+1)th item is expanded, the first expanded item will collapse.
In your case, you could call setLimit(1).
You can use:
#Override
public int getChildrenCount(final int groupPosition) {
return 1;
}