0

I am currently working on an application in which I have to taken into account two ListViews one above another and that too with a common scroll. I know this is not the best practice, instead I should have taken just a single ListView but the problem is with the layouts that doesn't seem to be possible with a sinlge ListView. Actually my first ListView will contain list of friend requests that I can accept or ignore and that are not static and can vary with no limit and second list will contain my friends which may vary too i.e not static. If i have to only show these two things, then I should have preferred single ListView and can be easily done but the actual problem arises when i have to add an Alphabetical sorting of my second list i.e my friends list using side bar and that is applicable only for my second list not for first one. This one is the screenshot without side bar

enter image description here

but alphabetical side bar will be there for new connections. So I don't think, this can be possible with a single list since alphabetical side bar will be added to the whole list not the part of a list. so right now I am trying to save myself from using ScrollView for my two ListViews since that will be very expensive. so I need the best way as how to go with this type of structure. Should take two ListView inside ScrollView or it can be done with a single ListView that will be more preferred.

Please pour your suggestions. Thanks in advance.

Mukesh Rana
  • 4,051
  • 3
  • 27
  • 39
  • u can take a look at [android-listview-with-different-layout-for-each-row](http://stackoverflow.com/questions/4777272/android-listview-with-different-layout-for-each-row) – Kaushik Dec 06 '14 at 07:39
  • @kaushik the problem is not with different layouts for each row, problem is that how can i add alphabetical sorting with side bar attached only to the new connections list, since i have to define that side bar in activity layout containing my listview and it will cover the entire list but not only the new connection part – Mukesh Rana Dec 06 '14 at 07:47
  • For your common scrolling, you can use an expanable listview. And for your sorting, you can create a function in your adapter of expandable listview to only sort data that is shown in the second section, which in your case is the friend's list. – Shadab Mirza Dec 06 '14 at 08:34
  • problem is not in sorting, the problem is how can i display that side bar containing Alphabets from A-Z in second section only @ShadabMirza – Mukesh Rana Dec 06 '14 at 08:41
  • @MukeshRana You will have to show the ui of how and where you want your sidebar to be placed. Also, if layout and data sorting is not your issue then you should mention that in your question. – Shadab Mirza Dec 06 '14 at 08:46
  • @ShadabMirza the problem is with the layout actually, i don't need the functionality. I only want to know the best method how my side bar can be placed at new connections list on the right side. – Mukesh Rana Dec 08 '14 at 04:55

1 Answers1

0

What about putting your two ListViews one after the other inside a LinearLayout container and you put that container inside a ScrollView? Then (programatically) you calculate the total height that the ListView needs to be in order to wrap all it's content, so your ListViews will never scroll but your ScrollView will. Here is the idea:

Layout

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <!-- Container -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <!-- Your list views -->
        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
        <ListView
            android:id="@+id/listView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
    </LinearLayout>
</ScrollView>

Code

private void resizeListView(ListView listView) {
    ListAdapter adapter = listView.getAdapter(); 
    int count = adapter.getCount();
    int itemsHeight = 0;
    // Your views have the same layout, so all of them have
    // the same height
    View oneChild = listView.getChildAt(0);
    if( oneChild == null)
        return;
    itemsHeight = oneChild.getHeight();
    // Resize your list view
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)listView.getLayoutParams();
    params.height = itemsHeight * count;
    listView.setLayoutParams(params);
}

You may want to (actually you need to) wait for the ListView to get drawn so getChildAt(int index) can actually return a View and avoid getting a null. You can achieve this adding this to your onCreate:

ViewTreeObserver listVTO = listView.getViewTreeObserver();
listVTO.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        listView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        resizeListView(listView);
    }
});

Just a suggestion, you may want to try the new RecyclerView because it can handle recycling with different types of Views

peguerosdc
  • 946
  • 11
  • 21
  • Thanks for your answer but the problem is I don't want to use ScrollView for this purpose as Romain Guy in this answer http://stackoverflow.com/questions/3495890/how-can-i-put-a-listview-into-a-scrollview-without-it-collapsing/3495908#3495908 said using a ListView to make it not scroll is extremely expensive. So i am looking for better and efficient approach for this :) – Mukesh Rana Dec 06 '14 at 10:49
  • @MukeshRana I couldn't find a comment with an explanation on why it is really expensive (EDIT: sorry, I just had to read a few answers above), but that was my best shot jajaja then I would suggest (again) to try out the RecyclerView as it can handle exactly what you require (unless your requirement is to use a `ListView`) – peguerosdc Dec 06 '14 at 11:01
  • Okay as far as RecyclerView is concerned, i have doubts regarding backward compatibility with lower versions although i know using appcompat library, this can be done but still i am not sure. can you clear my doubt and also help me in finding a perfect sample for using RecyclerView – Mukesh Rana Dec 06 '14 at 11:32
  • Have you tried the [official doc](https://developer.android.com/training/material/lists-cards.html)? Also you can find a good sample in your SDK directory (adt/sdk/samples/android-21/ui/views). If it's not there then just install it with the SDK Manager. And as you are using Support Library v7 then you have backwards compatibility until API 7. – peguerosdc Dec 07 '14 at 06:32
  • okay so you mean to say there is no other way than using two ListViews/RecyclerView inside scrollview?? – Mukesh Rana Dec 08 '14 at 04:57
  • The ways I can think of are: 1. One `RecyclerView` with multiple View types 2. Two resized `ListView` in a `ScrollView` 3. One `LinearLayout` programatically created. If you come up with another way I would be glad to hear it :) also it would be interesting to make performance tests to see which way is better. – peguerosdc Dec 08 '14 at 08:36
  • so far i think out of the three options you have given me, 1 and 3 are of no use. Option One is of no use because if I use only RecyclerView then there is a problem placing that side bar beside my new connections list at the right and creating LinearLayout programatically will just create number of multiple views with no memory management. so i am left with option 2 about which i mentioned in my question that i am looking for an alternative of using two ListViews inside scrollview. So in other words, i am still at the same position where i was earlier before asking this question. – Mukesh Rana Dec 08 '14 at 09:26