RecyclerView items change size after refresh - java

I have a RecyclerView as the following image presents:
This is the setup code for this RecyclerView:
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 2);
dataRecyclerView.setLayoutManager(layoutManager);
dataRecyclerView.addItemDecoration(new GridSpacingItemDecoration(...);
dataRecyclerView.setItemAnimator(new DefaultItemAnimator());
after starting a new activity where I update the selected item of the RecyclerView, the RecyclerView is re-populated with new data to reflect the new change, but I get the following result:
And when I scroll down, I get this gap:
I tried calling RecyclerView#invalidate(), #invalidateItemDecorations(), #requestLayout() after the refresh but to no avail; & I'm also not calling setHasFixedSize(true) on the RecyclerView.
What is the problem exactly ? How can I resize the child items so as they wrap their content precisely after refreshing?
(P.S: I tried refreshing without making any changes & nothing happened, which proves my theory that the issue arises only when a child item's height changes.)

That's quite late but I have also experienced this problem. This could be helpful for those still having the same bug.
In my case the solution was checking if there are any item decoration for recyclerview:
recyclerView.addItemDecoration(new SpacesItemDecoration(8));
This line was called everytime recyclerview is updated and was make recyclerview items getting smaller. Hope it helps the other.
Happy Coding,
Baki

Related

How to increment a view to Recyclerview item while the item is visible for like 2-3 seconds?

So, I have a recyclerview which consists of images and I want to show how many views the image got via server json.
So as the user scrolls and sees an image for 2+ seconds I want to call the api and increment the view of that particular item which is visible currently. Any help on how to achieve this would be appreciated!
If you are using LinearLayoutManager in RecyclerView you can get an item position by using
int findFirstCompletelyVisibleItemPosition()
int findLastCompletelyVisibleItemPosition()
To detect scroll events in the RecyclerView, you need to add a scroll listener, RecyclerView.OnScrollListener and with every item scroll maintain timestamp.
You can read this article explaining the same functionality Detecting List Items Observed by User

How to get the values from the RecyclerView using Espresso

I want to get the all values from the RecyclerView using Espresso. I am able to get only the visible values from the recyclerView. But not able to get the values which are visible when scrolling.
Ex. In RecyclerView 25 items are there and Only 7 items are visible. Others are visible when we scroll it. I am able to get the values of 7 items. But want to get all the values
Can Anyone please tell me how to get this
Thanks in Advance
You need to remember how RecyclerView really works. It optimizes showing scrollable elements, by displaying only items that are currently within the visible range.
In other words: before you scroll to items below, they are no Views within the RecyclerView to represent those items. Just the data in the Adapter.
So the way you should be probably doing your test is to:
Verify proper state of currently visible items.
Scroll the RecyclerView (using one of the RecyclerViewActions.scrollTo methods)
Verify proper state of currently visible items.
Scroll the RecyclerView.
...

Bug RecyclerView.scrollToPosition(list.size()-1) in Dialog

I have created RecyclerView with list of clients (from 0 till 14). I need maximum down state. For this purpose I am using method RecyclerView.scrollToPosition(14).
When RecyclerView are placing in Activity, I have the good result.
IMAGE: RecyclerView are placing in Activity
And now I am inflating new RecyclerView and put it in Dialog. The Adapter of RecyclerView the same. Method RecyclerView.scrollToPosition(14) give me the bad result (scroll is not set on the 14th position, by the way I can pull it to the down manually):
IMAGE: Bad result in Dialog
Why it happens? I think the problem in parent (Dialog) of RecyclerView. But I don't know what to do.
UPDATED! I think this is a really bug of RecyclerView. Because with ListView is all right (in my case with setSelection(14)).
Because your interaction panel hide the last element. Position your recycler view above the panel.

Checkbox in ListView with odd checking behaviour

I am new to Java and Andriod development.
I have a view within my app which contains a ListView
I then have a custom Adapter which inherits from an ArrayAdapter
within the view in the getView method I return a row which contains some text and a checkbox
now so far this all works great my list is populated and I can check my items, and all the events seem to fire as expected.
My problem that I am having is that I check the first 3 items and then I notice that the 11th, 12th and 13th items are checked and as I scroll I see that at regular intervals other checkboxes also seem to be checked in the same pattern.
If I check about 10 checkboxes then it will end up checking all the items in the list of about 80...
can anyone explain what I have done wrong?
I dont think any code will help explain this as I do not set the checkstate of the checkbox anywhere, this is all handled itself, so the fact items are being checked is puzzling me.
Thanks in advance
This is happening because Android recycles list items for performance purposes, here you have the solution of this problem:
ListView reusing views when ... I don't want it to
Hope to help :)
This is expected behavior. ListView reuses views in the adapter.
So what you want is to keep a Set of "selected items" and add an item or remove it when the CheckBox is clicked and then in your getView add the following code:
if(mSelected.contains(getItem(position)) {
checkBox.setChecked(true);
} else {
checkBox.setChecked(false);
}
This way when the row is reused the CheckBox will be the proper status.

Problems with android-pulltorefresh widget

I am having a bit of difficulty implementing the android-pulltorefresh widget by Johan Nilsson found at https://github.com/johannilsson/android-pulltorefresh
The problem I am having is after putting the custom listview into my application everything is fine it, but it asks to Tap to Refresh the list view but I need it to be set to pull down to refresh.
The code I am using below is pretty much from the github page and a screenshot of the app can be found below do demonstrate my issue:
PullToRefreshListView lv = (PullToRefreshListView)findViewById(R.id.listView);
lv.setOnRefreshListener(new OnRefreshListener() {
public void onRefresh() {
// Do work to refresh the list here.
GetData getData = new GetData();
getData.execute();
}
I need the Tap to refresh header gone and only to be shown once the listview has been dragged down. I get the feeling I just need to change some sort of flag but I can't find where this would be.
Unfortunately there is no way to work around this. The entire control is built around the idea that the "Pull to Refresh" header is a normal listview item that gets hidden by scrolling the list upward. Unfortunately, when you have a very short list, the list cannot be scrolled upward to hide the first item cause there are not enough items in the list -- so the fallback is to show the first item (the header) as well and have it display "Tap to Refresh".
EDIT: One kludge you may be able to do is insert dummy blank items so the list has enough items to hide the top header list item.
use this code
Hope it works for you.

Categories