0

enter image description here

Hi, I have to make a listview like this image.

These letter A,B are auto scrolled by the listview. The Letter A is appear until there is no Item start with A in the list view.

I dont have any problem with the list view in the right. My difficulty is showing letter abc along with the listview.

Can anyone help me about this? Thanks.

Thanh Le
  • 1,370
  • 2
  • 19
  • 30
  • 2
    please specify more regarding the behaviour of this list. Do the "A", "B", etc letters stick to the top, or scroll normally? Also, what approaches have you already tried? – Gil Moshayof Nov 16 '14 at 13:17
  • HI, I have add some descriptions. Please ask if you need more. – Thanh Le Nov 16 '14 at 13:20

4 Answers4

2

I'm assuming that you already know how to make a list view to display data and/or images as the image above illustrates, and that your primary difficulty is to show the "A", "B", "C", etc.

If that is the case, then a good approach would be to specify a list of models (data), each containing a field of "name", "imageUrl" & an optional "firstLetter" which can be null.

When you sort these models by the "name" field, you go over the list once, and on each iteration you check if the 1st letter of the "name" field is different than the previous one (always true for the 1st element). If this is the case, the "firstLetter" field is set to the upper-case first letter of the "name" field. Otherwise, it will be null.

Then in your adapter, if the "firstLetter" field is not null, then make the TextView which displays this letter visible (otherwise, make it GONE), and assign the text to the value of "firstLetter".

EDIT:

The above was written before you asked for the "A", "B", etc to be "sticky" and remain until there are no more items beginning with that letter.

To implement the "sticky" behaviour, similar to the WhatsApp app which makes the dates of the texts sticky, you can create an additional view which floats over the list (you'll need to make sure that the paddings, styles and fonts match so that everything looks good).

Then, in your adapter, you'll need to check the following:

  1. Am I already displaying this "hovering" letter for the letter that my cell wants to show? if that's the case, don't show it. This will prevent showing two "A"'s when the "A" is partially outside the screen.
  2. Is the position of my cell which is showing the "A" or "B" slightly outside of the screen? If so, make sure to show the "hovering" letter and give it the correct text.
Gil Moshayof
  • 16,633
  • 4
  • 47
  • 58
  • Thanks for your answer. It is very awesome. I have one concern that How to make these letter looks like they are fixed. I think in this approach the letter will move a little bit when swapping (between the same letter.) – Thanh Le Nov 16 '14 at 13:37
  • 1
    you need to position the "hovering" letter at the exact position the letter would appear if the top cell showing it was at y=0 relative to the list. Then, you need to make sure to update the visibility of the letter of every "onScroll" event of the list. If you do this correctly, there is no way the letters will jitter since every frame show display correctly – Gil Moshayof Nov 16 '14 at 13:40
  • Thanks, My concern is how to make the letter fixed at the top of the screen when the cell is scrolled over the screen (just a half of the cell is over, and the other is in the screen). – Thanh Le Nov 16 '14 at 13:47
  • this link (http://stackoverflow.com/questions/2139700/how-to-get-location-on-screen-of-row-in-listview) shows a good way to detect the y-coordinate of a view in a list view. use this in conjunction with onScroll events to decide when to show or hide to set the hovering letter or the cell's letter. – Gil Moshayof Nov 16 '14 at 14:30
  • Hi, I have found that the listview above looks like Android Lollipop contact list. Do you have any ideal about this? It may depend on something new in lollipop. Could you help me find how to do it in lollipop? I have searched but find nothing. Thanks – Thanh Le Nov 20 '14 at 11:34
  • perhaps look into RecyclerView: https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html – Gil Moshayof Nov 20 '14 at 11:40
1

I think you have to use 2 listviews on a relativeLayout. First with the Capitals of the names and second, the list you want to show. For make your listview you must create your itemList.xml, with name and photo like you want for create your listview.xml. You will need a "people.java" class with atributes, for example, name and photo. Also, you need one adapter for create the relation between your list.xml and your mainclass.java. This page is in spanish but the code could be usable for you: http://www.oneoctopus.es/desarrollo-android/listviews-personalizadas-en-android/

Asier
  • 461
  • 9
  • 19
0

When you are getting the data in adapter class then check for each object and its first word extract their first word in a char and compare that first word with other objects if they are same ignore and if different then save the previous char to temp char and assign the new one to previous char and so on.

-1

For the sticky letters index, you can do the following:

  1. On the leftmost side of your RecyclerView, create a TextView that will hold the letter index;
  2. On the top of the Recycler view (in the layout that wrappes it) place a TextView in order to cover the one you created in step 1, this will be the sticky one;
  3. Add a OnScrollListener in your RecyclerView. On method onScrolled (), set the TextView created in step 2 for the reference text taken from firstVisibleRow. Until here you shall have a stiky index, without the effects of transition;
  4. To add the fade in/out transition effect, develop a logic that checks if the item previous of the currentFirstVisibleItem is the last of the previous letter list, or if the secondVisibleItem is the first one of the new letter. Based on these information make the sticky index visible/invisible and the row index the opposed, adding in this last the alpha effect.

I've answered about the sticky-index in this question and also provided a library that does this behaviour. Hope someone still needs it: https://stackoverflow.com/a/30679418/2068693

Community
  • 1
  • 1
E. Fernandes
  • 3,889
  • 4
  • 30
  • 48
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – TimoStaudinger Jun 07 '15 at 01:08
  • I already fixed the answer, can you please remove the downvote? – E. Fernandes Jun 14 '15 at 00:49