I'm trying to use new google's ViewPager2 (https://developer.android.com/jetpack/androidx/releases/viewpager2) class that is implemented using RecyclerView.
One of the main ideas of ViewPager is that it create "neighbor" views for the selected one so the user can instantly see created views when scrolling. But as I see now onBindViewHolder is only called in adapter (I'm using RecyclerView.Adapter) when scrolling is started.
Does anybody know how to fix that to have onBind method called on neighbors before they are actually visible to the user? Thanks.
You can use setOffscreenPageLimit(...) on ViewPager2 to create neighbouring ViewHolders before the user scrolls to them.
Related
I am trying to understand how RecyclerView works.
We know that when RecyclerView needs an ItemView, that RecyclerView's adapter provides the ItemView. To do so, the adapter first looks for that type of view inside some caches and at last inside the RecyclerViewPool (Check this blog for the full list of checking). If the adapter doesn’t get the specified type of view inside the cache's or RecyclerViewPool, then it creates one inside the onCreateViewHolder method.
Now my question is, is there any way to check constantly, while scrolling the RecylerView list, what views are inside the RecyclerViewPool?
Any help will be appreciated. Thank You.
I have used the RececlerView with the ViewHolder pattern for a while now.
Im am implementing a custom Adapter.
Im am not searching for a specific bug help in my code.
I was just wondering, if it's normal, that the onBindViewHolder method is called multiple times (for the same item) while scrolling to the end of the list and scrolling back up. In this case onBindViewHolder is called again for item 0 and 1 (the list contains 7 items in total)
Is there any possibility for this method to get called AGAIN without notifying that the datasat has changed?
Im a bit confused.
Kind Regards,
Palm
Yes it is perfectly normal for a RecyclerView to call onBindViewHolder() multiple times.
A RecyclerView only creates minimum number of Views needed to fill the screen. And it works by reusing the old/created Views. So that when you are scrolling down the View that hid during the scrolling to the top is removed and brought next to the last visible View and added there. But since the View is currently bound with old data onBindViewHolder() is called again to ensure that the View is bound with only the correct data before it is rendered.
Similarly you'll notice that onCreateViewHolder() is only called the exact minimum number of Views it needs.
For a better understanding of how the RecyclerView works I suggest you read up on Recycler, LayoutManager and Recycler.Adapter the three core parts of a RecyclerView.
I want to loop my Viewpager and Im using the PagerAdapter extension. Everything I found in the net was an example with Fragments.
I want to solve this with a normal pageradapter. So When I Swipe from 1st view to left I want to be in the last View , and when I swipe from last view to right I want to change to 1st.
How can I solve this in an easy way (maybe without any libaries) ?
You can use the viewpager's setCurrentItem() method for achieving such functionality. This method takes the position (zero-based) of the view you want to navigate to.
https://github.com/TobiasBuchholz/CircularViewPager
I used this library, but there some problems in it when you try to show many circular views simultaneously
I'm trying to achieve the effect of the footer for ListView but without using the addFooterView method of the ListView. My intention is to treat the last visible item of the list as a pinned footer. In my view I can achieve this by detecting the last visible item on the list and dynamically change it's layout. I did some research and I think I must extend the BaseAdapter class providing two types of items. One for ordinary item on the list indicating that adapter should inflate the item with ordinary layout. And the second one indicating that adapter should inflate the current item with layout of footer. I think i must override the onScroll method to detect the last visible item. And here are my questions. Should i call the getView method from the onScrollmethod ? Is it the proper way to implement such effect? Is it possible at all? I would be grateful for any suggestions.
Thank in advance.
And here are my questions.
Should i call the getView method from the onScrollmethod ?
no, you should never directly call getView(). Only classes that extend AbsListView call getView
Is it the proper way to implement such effect?
No. The proper way of doing it is calling addFooterView to your ListView object.
Is it possible at all? I would be grateful for any suggestions.
No, it's not possible. Using scroll listeners you will never be able to find out when the AbsListView is requesting the "last view". That's because ListView does not guarantee the order it queries for Views. That's specially true when you 1st set the adapter or call notifyDataSetChanged which causes the ListView to get the views in several points to be able to layout and measure stuff it needs.
The suggestion is to use the addFooterView method, that's the correct way of doing, that's why it's there.
I'm working on creating a swipe-to-dismiss list view adapter. My basic methodology is to wrap the list item's view as the second view in a ViewPager and provide the necessary callbacks in the item change listener of the ViewPager. Through much pain I've got the View recycler working as intended, as well as ViewHolder and ViewBinder patterns implemented. I even managed to keep the ListView from taking over the touch events while the ViewPager is being scrolled without having to make a custom subclass of ListView (I can do it all from the Adapter).
Where I'm running into trouble is getting the selector and the OnItemClickListener to work. After looking at ListView's source it seemed that by overriding the ViewPager's hasFocusable() method to always return false (later on I'll pull this value from the child view) these things should have been reenabled. Unfortunately this is not the case. I've tried the setDecendantFocusability() workaround and I'm still stuck.
I'd like to avoid having to extend ListView if possible to provide the greatest amount of modularity. For similar reasons I don't want to add the selector to the ViewPager's background (if the dev changes the ListView's selector this wouldn't be reflected). Essentially I'm looking to make the ViewPager code transparent between the ListView and child View. Any ideas?
You are saying that you are making each list item a view pager, so that you can implement swiping to delete? If so... no no, this is not what view pager is for. First sorry it is just not intended to be used as an item in a list. Second it is for switching between views, not swiping to delete.
Unfortunately we don't have a sample code to show how to do this, but you can look at the platform's implementation of the notification pane or recent apps to get some ideas.