1

I am using 3 ListViews one below the other in a same Activity, But I can't see my third list view, and my second listview scrolls within itself, I need all the three list view to be scrolled in a single page one after the other. If I use the android:layout_weight="1" attribute, it shows all the list view by dividing the page, but I don't want to divide the activity screen, but to show all the list view one after the other.

Could anyone would guide me for the same, belopw is my xml layout which includes those three listviews:-

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ListView
        android:id="@+id/listTopTenBatsmans"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ListView>

    <ListView
        android:id="@+id/listTopTenBowlers"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ListView>

    <TextView
        android:id="@+id/tSometextField"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Fall of Wickets"
        android:textColor="@color/red"
        android:textSize="18sp"
        android:typeface="sans"
        android:textStyle="bold"
        android:background="#ffdfe5"
        android:paddingLeft="5dp" />

    <ListView
        android:id="@+id/listTopTenFielders"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ListView>

</LinearLayout>

In the above XML the first list is appers with some 10 fields in it populated as per my adapter view and the seconds is also appears with some 10 list field as per populated by adapter, but it scrolls in itself and where as the textview and list view is not appered, and if I use the android:layout_weight="1" attribute then It shows all the 3 listvies and the textview by dividing the activity screen, But I need one after the other.

Scenerio:-

ListView1
Element1    Ele1    E1
Element2    Ele2    E2
Element3    Ele2    E3
Element4    Ele4    E4
Element5    Ele5    E5
Element6    Ele6    E6
Element7    Ele7    E7
Element8    Ele8    E8
Element9    Ele9    E9
Element10   Ele10   E10

ListView2
Item1   It1 I1
Item2   It2     I2
Item3   It2     I3
Item4   It4     I4
Item5   It5     I5
Item6   It6     I6
Item7   It7 I7
Item8   It8 I8
Item9   It9     I9
Item10  It10    I10

SomeTextView
Listview3
Source1     Sou1    S1
Source2     Sou2    S2
Source3     Sou2    S3
Source4     Sou4    S4
Source5     Sou5    S5
Source6     Sou6    S6
Source7     Sou7    S7
Source8     Sou8    S8
Source9     Sou9    S9
Source10    Sou10   S10
Pravinsingh Waghela
  • 2,516
  • 4
  • 32
  • 50
  • 2
    Several options: use [different view types](http://stackoverflow.com/questions/4777272/android-listview-with-different-layout-for-each-row) in your adapter, or take a look at [`MergeAdapter`](https://github.com/commonsguy/cwac-merge), or [`RecyclerView`](http://developer.android.com/reference/android/support/v7/widget/RecyclerView.html). – MH. Mar 09 '15 at 08:21
  • @MH. Thanks the MergeAdapter worked for me, please write it in the answer so that I can accept your answer. Thanks mate once again. – Pravinsingh Waghela Mar 11 '15 at 13:33

3 Answers3

2

As per earlier comment, you have several options:

If you already have three ListViews and adapters set up, the quickest solution is to go with MergeAdapter. The more modern approach would be to trade in your current implementation for a RecyclerView, but that will probably mean some more serious code changes.

Community
  • 1
  • 1
MH.
  • 45,303
  • 10
  • 103
  • 116
  • Can you help me, as I am using MergeAdapter. I am able to merge the adapters and then set that adapter to the list view. But When I try to add some view using the mergeAdapter.addView(View view); method. I am getting java.lang.NoClassDefFoundError on addView(); method line. – Pravinsingh Waghela Mar 11 '15 at 14:32
  • 1
    @PravinsinghWaghela: What class cannot be found? If you're not using Gradle/Maven to include `MergeAdapter` as a dependency, you may have missed the fact that it also relies on [`SackOfViewsAdapter`](https://github.com/commonsguy/cwac-sacklist). So, if you added `MergeAdapter` as a `jar`, make sure you do the same for `SackOfViewsAdapter`. The latter comes into play when adding views directly to the `MergeAdapter` (internally the views are then wrapped into a `SackOfViewsAdapter` instance). – MH. Mar 11 '15 at 14:38
  • Thanks mate once again, you are a true life saver. Is there any way so that I can follow you on any Social Network site?? – Pravinsingh Waghela Mar 12 '15 at 06:48
0

you should use methods getViewTypeCount() and getItemViewType(int position) of your adapter and only one ListView for all items

more HERE

Community
  • 1
  • 1
snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • Well all my three list view contains different columns and rows in each of there adapter, hence I can can't use one list view for all three type of different contents or items in it. – Pravinsingh Waghela Mar 09 '15 at 09:21
  • you don't have any other option, rewrite code to one adapter, that way it should be done... there is no other way due to recycling methods of single `ListView`. one other option is to set fixed height to every `ListView` (adapterCount * getView.getHeight) and put them all inside e.g. `LinearLayout` in other `ScrollView`, but note that recycling will be disabled and all items in all lists will be drawn at start (bad for performance) – snachmsm Mar 09 '15 at 09:59
  • 1
    @PravinsinghWaghela: you're argument doesn't really hold, since that is exactly what item view types are for... Anyways, if you already have three ListViews and adapters set up, the quickest solution is to go with `MergeAdapter` (see my earlier comment on your question). The more modern approach would be to trade in your current implementation for a `RecyclerView`, but that will mean some serious code changes. – MH. Mar 10 '15 at 07:42
  • @MH. could you please provide me any link for the guidance to how to do that? any demo? – Pravinsingh Waghela Mar 10 '15 at 10:38
  • @PravinsinghWaghela: Do wat exactly? They're all quite different. Besides the links given before (i.e. `MergeAdapter` has a [demo project](https://github.com/commonsguy/cwac-merge/tree/master/demo)), Google is your best friend to find examples. – MH. Mar 10 '15 at 10:48
0

Set your lists heights to a concrete value instead of match_parent or wrap_content.

Juanjo Vega
  • 1,410
  • 1
  • 12
  • 20
  • well that will divide the Activity Screen in that case. I need once the first List view is scrolled then to its bottom then next listview should apper and once that is scrolled, then the third should be appered. – Pravinsingh Waghela Mar 09 '15 at 09:20
  • Ok. I misunderstood you, sorry. Then, what you need is a listview with headers. Have a look to this library: http://nhaarman.github.io/ListViewAnimations/#getting-started I'm not using headers for now, just drag&drop, swipe deletions, undo, etc. but it provides that as well. Check the sample app at play store. – Juanjo Vega Mar 09 '15 at 09:39