3

In my application I have multiple lists of data that I get from api and I have to display them in one activity. Also I have headers for this lists. The problem is that only first listview is being diplayed and scrollable while the second is simply gone. Is it possible to display two listviews so that the second will start after the first listview finishes? Maybe I have to show all lists in a single listview? If yes, how can I set section headers so that lists with data from different api will be separated and will scroll down dynamically one after another? I couldn't find full answer to this question in the internet

Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
Yusuf
  • 145
  • 6
  • 12
  • 1
    Take a look into this http://stackoverflow.com/questions/7943802/how-to-draw-a-section-header-in-android-listview-just-like-the-ioss-uitableview – Jithin Sunny Dec 04 '15 at 08:59
  • Possible duplicate of [multiple listview in one activity](http://stackoverflow.com/questions/24669919/multiple-listview-in-one-activity) – Hemant Ukey Dec 04 '15 at 09:03
  • @hemant1087 this isn't a duplicate of that, as Yusuf is asking about using one ListView and putting multiple lists into it, and that post is about putting actual multiple ListView (fragments) into a single activity. – Alex K Dec 04 '15 at 09:04
  • Can you please try my answer ? http://stackoverflow.com/questions/17693578/android-how-to-display-2-listviews-in-one-activity-one-after-the-other/28713754#28713754 this one – Hiren Patel Dec 04 '15 at 09:20
  • @HirenPatel I have tried it already, scrollview doesn't work when there is listview inside it, as I understood there is some conflict as both scrollview and listview have scrolling functions – Yusuf Dec 04 '15 at 09:33
  • @YusufAbdullaev, Brother please try once copy code and run, if you get any issue please let me know. – Hiren Patel Dec 04 '15 at 09:36

3 Answers3

0

Problem is probably in ur xml file. Try set exact heights of listViews so u can see both in designer. Than it should work. Troubles are when u have more scrollviews included. There are hacks to recount height of list view but is stupid.

I think best for u can be: Prepare xml with only one ListView. Prepare something as listItem - class than will handle if it is title or nor what kind of data and make ArrayAdapter of listItems. Than in adapter solve what view u need to draw for which item.

Tomas Cejka
  • 96
  • 1
  • 8
0

These solutions are ugly but can work in your case: 1. You can use a single listview to display the data sets from the two APIs you are talking about. There should be a way to identify that the item is coming from which API.

So, you can have different drawable defined for the header section and then for the individual items.

<header1 background="header_drawable">
  <item1 background="item_drawable">
  <item2 background="item_drawable">
  <item3 background="item_drawable">
  <item4 background="item_drawable">

<header2 background="header_drawable">  <-- Here you need to identify that this is from 2nd API and adda header section with appropriate drawable.
  <item1 background="item_drawable">
  <item2 background="item_drawable">
  <item3 background="item_drawable">
  <item4 background="item_drawable">

All this kung-fu can be done in your adapter. Let me know if you need any more help with this.

  1. This is another ugly solution. Have a expandable listview with a LinearLayout in the item as a child. So, the layout looks like this:

    <Header1>  <- This contains the first header (a listview item)
      <LinearLayout/> <- This will contain the data from the first API
    <Header1/>
    
    <Header2>  <- This contains the first header (a listview item)
      <LinearLayout/> <- This will contain the data from the first API
    <Header2/>
    

Now you can populate the LinearLayout with the items dynamically. There are a lot of posts for this.

Please note that this method is effective if you have a small number of items coming from your APIs but if you have a large number of items, then I would suggest the first method as with the second approach, you will not get the benefits of a listview or recyclerview. Again, please let me know if you need more clarification.

Another solution is mentioned in this post. Haven't tried it but looks promising: http://www.captechconsulting.com/blogs/android-expandablelistview-magic

avin
  • 459
  • 5
  • 14
0

I have found answer to my question. Answers provided by other users may be are correct too but they were too long for such a lazy person like me:) the most suitable way was to put listviews inside scrollview but they displayed only first item of list. I have just changed ListView in my xml-file into ExpandedListView. More detailed answer here http://joerichard.net/android/android-listview-not-wrap_content-inside-scrollview/ . Credits to author of this blog Joe Richard

Yusuf
  • 145
  • 6
  • 12