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.
Related
Being objective, my goal is simple (or not), drag a recycler's item to other one in my activity.
The scenario is the following.
I have a list of items, let's call them sections, and inside a section another list of items, of another type, the elements.
I want exchange the elements between the sections. The list of sections isn't fixed, can be changed adding and removing a section, but hasn't any movement behavior. The elements also can be added and removed from a section, but have this reorder approach.
I've found some strategies with fixed "section". Here is an example. This one uses drag and drop to make the exchange. But it's too simple. For example, don't consider the need to scroll recycle when the dragging reaches bottom or top of visible elements. That's because it has only two lists. Does Drag and Drop have this behavior, forcing the scroll?
Is there any way to do this using the ItemTouchHelper? If so, how would it be? ItemTouchHelper seems better, 'cause considers the scroll problem and has a more intuitive reorder animation.
If you think of a better approach than using recyclers, feel free to indicate such strategy to me.
You use drag and drop for the dragging and dropping, the scrolling and such you'd have to make yourself using the appropriate events in the OnDragListener.
I will need to create cards layout mimicking cards found in Google Now. To do so, I've used a RecyclerView and each row as both the header and item. Pretty skeptical with this approach however.
The problem I'm facing is to mimic the swiping behaviour. Swiping at the header will also move the entire card (header + items) together.
To do so, I managed to edit the SwipeDismissRecyclerViewTouchListener and the adapter to remove the affected items. But after that, the visual would get clunky whereby remaining items would have random empty spaces in between or height that does not match.
How and what is the correct way to approach this? Should I instead use another way to populate the cards?
I am new the Android development (3 weeks).
I'd like to create an Activity that allows the user to scroll through a list of items. I'd like only one item to occupy the width/height of the screen at any given time. At the same time, I'd like for the items to be able to scroll smoothly (up/down), similarly to facebook/instagram. During run-time, items will be pushed on top of the stack/list (like a news feed in FB).
What is the best way to accomplish this? What are the pros/cons between using a ListView, LinearLayout (Vertical) items, a List of Buttons added on top of one another? Or should I use Fragments that display on top of one another? How would I implement the ability to display only one item at a given moment?
The answer is to use a ViewPager: http://developer.android.com/reference/android/support/v4/view/ViewPager.html
By default, the motion is horizontal, but there are examples that show how to make it vertical.
how can I set ListFragment to show each of its items on whole screen? And of course, when you swipe up and down, whole item should be available at a time, the list shouldn't stop scrolling in the middle of two items. Thanks for any suggestions.
You have two choices:
Add a OnScorllListener and implement logic to "settle" the list
after a scroll.
Make a vertical ViewPager. You could copy the source code and flip everything from using X to using Y.
so I'm considering making a UI that consists of a list with expandable sections. I've read that the ExpandableList design is being phased out, so as an alternative, I am thinking about using a ViewPager that can partially swipe. What I mean by partially swipe is that the UI will consist of 3 columns, each column expanding relevant items in the column to its immediate right. When an item is selected in the very right most column, all the fragments will shift to the left and a new column on the right will display the next level of relevant items. All the while, a PagerTitleStrip will give the title of right most column.
In my research, I didn't even really know what to search to find information, so if anyone has any idea how to go about this, it would be greatly appreciated!
I realize this is a very old question, but here's my answer anyway.
Check out ViewPager.PageTransformer.
You can handle complicated behaviors like partial scrolling, position of each view, etc. on transformPage(..). This can be set to the ViewPager using viewPager.setPageTransformer(..)
An alternative could be using a RecyclerView with the total width of the three columns instead of a ViewPager and use smoothScrollToPosition(..) to the correct column when the next one expands. (With this you might have to disable manual scrolling on the RecyclerView like this