Office Hours — Today, January 11

Yesterday, January 10

Jan 11
3:55 PM
Mark M.
has entered the room
Mark M.
turned off guest access
Mark M.
turned on guest access
Aman
has entered the room
Aman
Hello Mark
Mark M.
hello, Aman!
how can I help you today?
Aman
First time here, nice to meet you!
Mark M.
nice to meet you too!
Aman
Here's my question:
View paste (9 more lines)
I need to show 6 items that the user can scroll through horizontally.  Of these 6 items, the first 2 should be showing first.  Then, as the user scrolls, the first 2 scroll left and the next 2 scroll into view.  For the next scroll, only the 5th item scrolls into view.  And then the 6th item on the next scroll.  

The items also have titles.  The first 2 items have a single title, the next 2 have a single title, and each of 5th and 6th have their own titles as well.  So, something like this:

    screen1title
item1          item2


— now the user scrolls left and the next screen snaps into place:

    screen2title
item3          item4


— scroll again, and we see:
...
Mark M.
OK
Aman
View paste
I tried using a RecyclerView with a horizontal LinearLayoutManager and a LinearSnapHelper which snaps the items into place.  I kept only the items in the RecyclerView and the titles in a separate textview that I can scroll manually in a RecyclerView.OnScrollListener.  The problem I have is getting the RecyclerView to show 2 items on the first 2 screen and only 1 item on each of the next 2 screens.  How can I solve this problem?
View paste
I’ve tried setting the width for the first 4 items at runtime like this:
   view.getLayoutParams().width = mDisplayMetrics.widthPixels / 2;

This works but now the LinearSnapHelper is still snapping 1 item into the middle of the screen.  For example, it snaps item2 into the middle and you see half of item1 on the left and half of item3 on the right.
4:00 PM
Mark M.
have two different layouts for the "pages" in the RecyclerView
one layout has two items
the other layout has one item
in other words, the ViewHolder and Adapter are adapting pages, not items
you use the two-item layout for the first two pages, and the one-item layout for the other two pages
Aman
i thought about that but I need each item to really be separate because each will have it's own onclicklisteners
Mark M.
have two different ViewHolder classes, one for one-item pages and one for two-item pages
where they handle the correct number of event listeners per page
if you ignore the items for the moment, your description is for a four-page UI
and so the simplest way to implement that is to have a four-page UI, whether you use RecyclerView or ViewPager
it just so happens that on two of those pages, you want to show two items apiece
but that's an implementation detail of the *page*, not the overall structure for showing the pages
Aman
i see
Mark M.
it is fairly common to have pages with significantly different results
4:05 PM
Mark M.
for example, the stock dialer on a Nexus 5X has four pages (tabs), with favorites in a grid, call history in a list with headers, contacts as a list, and voicemail (I think as a list)
that activity will have either four separate ViewHolder classes or four separate fragments, depending on whether it is using RecyclerView or ViewPager
Aman
yes, i'm looking at it now
And can you give me your thoughts on whether a RecyclerView is a good choice here? Is there something else that would fit better like the ViewPager u mention.
Mark M.
on the whole, I am tending to use RecyclerView more for this scenario
there is nothing wrong with ViewPager, and if it simplifies your life if your pages are fragments, ViewPager is the better solution
Aman
ok
i've looked up using a listview horizontally but found different solutions with their own problems, do you recommend that?
would you use a listview for horizontal scrolling?
Mark M.
no
Aman
ok
Mark M.
in fact, AFAIK, that is not strictly possible
there are horizontal list libraries, but AFAIK they are not using ListView as a base
Aman
i see
Mark M.
but, I haven't paid them much attention since RecyclerView was released
4:10 PM
Aman
right ok
what about using a gridview for horizontal scrolling?
Mark M.
I do not think that GridView offers horizontal scrolling
the only horizontal scrolling option we had in the early days was Gallery, which is long since deprecated
then we had ViewPager
Aman
i see
Mark M.
and HorizontalScrollView was added around the same time
now, we have RecyclerView
Aman
ok
Mark M.
there are third party libraries with other options, and you are welcome to explore them
for me, RecyclerView is sufficient for most of my needs
Aman
ok, going back to my use case, i want to give the user a hint that there are more items to the right to scroll to, i was thinking to show a little bit of the title
would you say the title should be in the viewholder?
Mark M.
I cannot really answer that
Aman
the title should scroll along with the items and come to the middle
4:15 PM
Mark M.
then the title is part of the layout that you are using for the pages
however, that does not really fit your "show a little bit of the title" approach
as the that extra title is in the wrong place, presumably
Aman
would i be able to have it scroll at a different rate?
right
Mark M.
possibly, but I have no idea how
Aman
i think that's why i need it outside the recyclerview
Mark M.
I would consider adapting one of Jake Wharton's old ViewPagerIndicators for use with RecyclerView
such as the one that shows dots
Aman
is that only for ViewPagers?
Mark M.
yes, though presumably it could be adapted, if nobody else has adapted it already
that's a standalone dot indicator library
4:20 PM
Aman
yes, nice
what do u think about this:
Mark M.
that seems to be a similar concept, more closely coupled with RecyclerView
I'm impressed that item decorators can pull that off
Aman
OK, I really appreciate all your help, thanks much!
4:25 PM
Mark M.
you're welcome!
5:00 PM
Mark M.
that's a wrap for today's chat
the transcript will be posted to https://commonsware.com/office-hours/ shortly
the next chat is Tuesday in the same time slot (4pm US Eastern)
have a pleasant day!
Aman
has left the room
Mark M.
turned off guest access

Yesterday, January 10

 

Office Hours

People in this transcript

  • Aman
  • Mark Murphy