Load all RecyclerView items at once - android

Default RecyclerView feature is to load only those items which are visible and load rest of the items as needed when user scrolls the list.
But my requirement is to load all items at once.
Any idea how to achieve this.
I am loading a Book and each page has multiple images for every line.
I am loading one image per item and I need to implement auto scroll feature for which I need to calculate whole recyclerview height.

I think instead of changing the default behavior of recyclerView if its possible use listView/gridView (until and unless you need StaggardGridView of recyclerView)

Just keep Recyclerview inside NestedScrollView. In simple first all the views regarding recyclerview items, that of cards is set to NestedScrollView and the things goes perfectly.

Related

Recyclerview grid layout: Drag outside incompatible with scrolling?

I am building an android app with two recyclerviews, one with horizontal LinearLayoutManager and the other one with GridLayoutManager.
I want to allow the items recyclerviews to be dragged and dropped over a trash icon outside of the recyclerviews, obviously to delete the dragged item.
What I have done is:
Apply android:clipChildren in the parent of the two recyclerviews
Apply android:clipToPadding in the recyclerviews
This works perfectly in recyclerview with LinearLayoutManager. I can drag an item and drop it over an icon that is outside of the recylcerview.
I also works in the recyclerview with GridLayoutManager, but with a side effect. When I scroll in the grid recyclerview the scrolled items come out of the recyclerview.
At the ende of this GIF you can see the scrolling issue
So, Is there any way to allow dragging a recyclerview item outside of the recyclerview boundaries but preventing items comeout when scrolling?
Lots of thanks for your help and suggestions in advance
I tried putting the recyclerview with LinearLayoutManager on top of the recyclerview with GridLayoutManager, to hide the items which came out of the boundaries, but this make a strange behaivour when I drag an item, because the items are there yet, even they are hidden. I could make a GIF if needed
My last option is putting the trash icon inside of the recyclerview, as a header that appears when dragging an item, but I would prefer not do it that way
Finally what I have learnt is that you have two main options when face a drag and drop problem:
Easy way: Using the Android helper ItemTouchHelper
Hard way: Not using the helper and type all the code, using startDragAndDrop()
A scenario like this only can be solved by the hard way.

Android. RecyclerView track viewed items

I have two RecyclerView inside the NestedScrollView and I need track viewed items of my RecyclerViews. But problem that using nestedScrollView the recycler pattern doesn’t work and all the views will be loaded at once because wrap_content needs the height of complete RecyclerView so it will draw all child Views at once.
Is there any way to track of the viewed items with this approach? Please help me.
never use RecyclerView inside NestedScrollView because your ViewHolders will not be recycled and you will be in danger of OutOfMemoeryExceptions, it may work for lists with small sizes but as the list size increases it becomes worse,
instead of using one ScrollView and 2 RecyclerView as child
you have to completely remove ScrollView and instead of that, use one RecyclerView with multitype ViewHolders, in other words you need to combine those 2 RecyclerView in on RecyclerView .
To achieving this you can combine those adapters manually or you can use ConcatAdapter that depends on what you want, this link may also help you if you want to use ConcatAdapter but that is not necessary.
then you can have different approaches to track the visibility of viewHolders like layoutManager.findFirstVisibleItemPosition() and etc.

how to prevent loading of all item of recyclerview in scrollview?

I have a RecyclerView inside a ScrollView and I want to scroll all of the items in my screen, and I get data from the server. my problem is each item of RecyclerView has a photo, and because of nesting scrolling, recycler assumes that the height of the page is unlimited so it gets all of the items from the server and tries to download all photos, and this makes scrolling not smooth!!
my question is, is there any way to download only shown items photo in RecyclerView? (I know RecyclerView itself does that! but it's not working inside a scrollView)
note :I've also seen similar question like this and this. the first one provides an acceptable solution but requires a lot of change for me! therefor I'm looking for a better solution!
Did you try with Glide? It only load photos when it is visible.

Which Recyclerview or ScrollView should I choose?

I created an activity with a ListView. That is a Friend ListView.
I wanna let it choose to add it to another View.
I don't know which View to choose is the best. Recyclerview or ScrollView?
Like this
Basic difference between RecyclerView and ListView are the below.
ListView is simple to implement. Default Adapters available.
But ViewHolder pattern and finding views using ID's used to give slow performance.
Now in RecyclerView , the above problems are attended using RecyclerView.ViewHolder.
For RecyclerView adapter, this ViewHolder is mandatory. While comparing to list view this is kinda complex, but solves performance issues in ListView.
Difference LayoutManagers are available while using RecyclerView.
LinearLayoutManager
StaggeredGridLayoutManager
GridLayoutManager
Major improvement of RecyclerView performance while comparing to ListView would be this according to developer.android.com
The RecyclerView creates only as many view holders as are needed to
display the on-screen portion of the dynamic content, plus a few
extra. As the user scrolls through the list, the RecyclerView takes
the off-screen views and rebinds them to the data which is scrolling
onto the screen
To sumup, RecyclerView is preferable than ListView (when UI is having same widgets repeating according to your data)
Now when to use ScrollView:
Your UI elements may not show completely in small device screens. But in bigger screen sizes it may!
Elements may not be necessarily only list / grid. It can have combinations of any UI widgets.
Eg:- TextViews vertically , with RadioButton and Button at last for user action.
This cannot be included in ListView/ RecyclerView , now you can add ScrollView which will have a LinearLayout/RelativeLayout . Inside which all other elements can be added.
Now you can
I recommend to user always RecyclerView and never a ListView. Use RecyclerView for element list and scrollview for static views.
Seeing your image Scrollview inside RecyclerView or ListView have problems with drag.
Use a vertical RecyclerView in all page and horizontal RecyclerView each row.
I suggest you to use RecyclerView because you will load lots of images and if you use ScrollView eventually you will use so much memory. Also it is recommended to use RecyclerView when you have dynamic data. You can look the definition of the RecyclerView in the Android Documentation as follow
RecyclerView is a view for efficiently displaying large data sets by
providing a limited window of data items.
You can use nested RecyclerView for creating such hierarchal view.
You can also checkout this example for nested RecyclerView usage.
Also you can further read:
Android Recyclerview vs ListView with Viewholder
RecyclerView vs. ListView
Should we use RecyclerView to replace ListView?
I hope this helps.

RecyclerView vs ViewPager

Currently, I am exploring the option of displaying data from a database by swiping left to right and also allowing users add and remove data from any position in the data array.
I found out that there are 2 possible solutions to do this. One is a RecyclerView with horizontal scroll and the other is a ViewPager with a FragmentStatePagerAdapter .
Which is more efficient? In terms of Memory usage and Ease of implementation?
Thanks.
I would say they are comparable in terms of memory usage and ease of implementation. Where they differ most is in the interaction they provide to the user.
ViewPager is designed to show one item at a time. The visible item takes up full width of the ViewPager. You can only swipe one item at a time and scrolling always snaps to showing one item in the centre – you're never left in an in-between position partially showing two items.
RecyclerView with a horizontal layout manager on the other hand can have items of any width – you could be showing many items at once or you could have items wider that RecyclerView's width or you could match their widths to mimic ViewPager. You can freely scroll – you are not limited to one item width or RecyclerView's width, you can do a fling gesture to scroll big distances. And there's no snapping – when the scroll finishes there's no aligning items to the centre or any of the sides.
As you see there are a few differences. I would recommend you to choose your widget based on the UI you want to achieve. If you want ViewPager's behaviour (one item visible at a time, swipe limited to one item and snapping to show the full item) then go with a ViewPager. It's possible but not trivial to replicate this behaviour using a RecycleView. I would definitely say it is way more difficult to use RecyclerView if you want to make it behave like ViewPager. Conversely it's pretty much impossible to customise ViewPager's behaviour, so if that's not what you want then you definitely should use a RecyclerView.
In term of ease of implementation (this is just my own opinion),
ViewPager is good for displaying the list of data that is not required often add and remove since PagerAdapter can't notify each specific item that it is removed or added it can only call notifyDataSetChanged() which notify that all set of data has been changed. Therefore, it is hard to handle the animation when the item is added or removed.
While in RecyclerView, RecyclerView.Adapter has methods like notifyItemInserted(int position) or notifyItemRemoved(int position) to notify that specific the item is added or removed and, the animation when item is add or remove is already handle when you called those method.
Moreover, right now it is very easy for RecyclerView to mimic the ViewPager behavior by using SnapHelper. There is PagerSnapHelper, and the behavior of ViewPager can be obtained with just a few lines of code. You can contact me if you want the code.
There is no comparison between this two. basically in ViewPager you can scroll only one item at time (either left or right), and in RecyclerView you can scroll to any index. it all depends on your requirements how you want to use it. you need to develop fragments for ViewPages, one for each page. as in RecyclerView you will have a item which will be used by adapter. both of them are easy to implement. there are numerous examples on both of them, you can have a look and get started.

Categories

Resources