0

I am attempting to add a navigation drawer to k9-mail, and in doing so am trying to create a navigation drawer with three listviews, and I want each listview to use the same scrollbar, so they are stationary, except they all scroll together. Basically I want the ListViews to act like one list, scrolling together and stacked on top of each other. However, whenever I try to add the ListViews into a ScrollView via this answer, I get an error. Now how can I use a ScrollView in a DrawerLayout without it throwing an error?

Error it throws

08-13 11:11:33.148: E/AndroidRuntime(17586): FATAL EXCEPTION: main
08-13 11:11:33.148: E/AndroidRuntime(17586): Process: com.bernard.beaconportal.activities, PID: 17586
08-13 11:11:33.148: E/AndroidRuntime(17586): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bernard.beaconportal.activities/com.bernard.beaconportal.activities.activity.MessageList}: java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
08-13 11:11:33.148: E/AndroidRuntime(17586):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
08-13 11:11:33.148: E/AndroidRuntime(17586):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2274)
08-13 11:11:33.148: E/AndroidRuntime(17586):    at android.app.ActivityThread.access$800(ActivityThread.java:145)
08-13 11:11:33.148: E/AndroidRuntime(17586):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
08-13 11:11:33.148: E/AndroidRuntime(17586):    at android.os.Handler.dispatchMessage(Handler.java:102)
08-13 11:11:33.148: E/AndroidRuntime(17586):    at android.os.Looper.loop(Looper.java:136)
08-13 11:11:33.148: E/AndroidRuntime(17586):    at android.app.ActivityThread.main(ActivityThread.java:5144)
08-13 11:11:33.148: E/AndroidRuntime(17586):    at java.lang.reflect.Method.invokeNative(Native Method)
08-13 11:11:33.148: E/AndroidRuntime(17586):    at java.lang.reflect.Method.invoke(Method.java:515)
08-13 11:11:33.148: E/AndroidRuntime(17586):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
08-13 11:11:33.148: E/AndroidRuntime(17586):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
08-13 11:11:33.148: E/AndroidRuntime(17586):    at dalvik.system.NativeStart.main(Native Method)
08-13 11:11:33.148: E/AndroidRuntime(17586): Caused by: java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
08-13 11:11:33.148: E/AndroidRuntime(17586):    at android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:968)
08-13 11:11:33.148: E/AndroidRuntime(17586):    at android.support.v4.widget.DrawerLayout.closeDrawer(DrawerLayout.java:1169)
08-13 11:11:33.148: E/AndroidRuntime(17586):    at com.bernard.beaconportal.activities.activity.MessageList.selectItem_Inbox(MessageList.java:3048)
08-13 11:11:33.148: E/AndroidRuntime(17586):    at com.bernard.beaconportal.activities.activity.MessageList.onCreate(MessageList.java:706)
08-13 11:11:33.148: E/AndroidRuntime(17586):    at android.app.Activity.performCreate(Activity.java:5434)
08-13 11:11:33.148: E/AndroidRuntime(17586):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-13 11:11:33.148: E/AndroidRuntime(17586):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2179)
08-13 11:11:33.148: E/AndroidRuntime(17586):    ... 11 more

xml file for the drawer

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff" >

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" 
         android:background="@color/white"
        >
        <com.bernard.beaconportal.activities.view.ViewSwitcher 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 

        android:background="@color/white">

    <FrameLayout
        android:id="@+id/message_list_container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
       android:clickable="true"
        android:background="@color/white"/>

    <FrameLayout
        android:id="@+id/message_view_container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:clickable="true"
        android:background="@color/white"/>

</com.bernard.beaconportal.activities.view.ViewSwitcher>

    </FrameLayout>


    <com.bernard.beaconportal.activities.activity.VerticalScrollview
        android:layout_height="match_parent"
    android:layout_width="wrap_content"

        >

    <LinearLayout
    android:id="@+id/left_drawer"
    android:layout_height="match_parent"
    android:layout_width="320dp"
    android:orientation="vertical"
    android:layout_gravity="start"
    android:background="#ffffff" 
    android:clickable="true">



    <ListView
        android:id="@+id/listview_inbox"
       android:layout_weight="1"
        android:layout_height="wrap_content" 
        android:layout_width="320dp"
    android:orientation="vertical"
    android:layout_gravity="start"
    android:background="#ffffff"
        android:minHeight="64dip"
       android:layout_alignBottom="@+id/Person"
        android:choiceMode="singleChoice"
        android:divider="#ECF0F1"
        android:dividerHeight="1dp"       
        android:listSelector="@drawable/list_selector"
        />

    <ListView
        android:id="@+id/listview_drawer"
       android:layout_weight="1"
        android:layout_height="wrap_content" 
        android:layout_width="320dp"
    android:orientation="vertical"
    android:layout_gravity="start"
    android:background="#ffffff"
        android:minHeight="64dip"
       android:layout_alignBottom="@+id/Person"
        android:choiceMode="singleChoice"
        android:divider="#ECF0F1"
        android:dividerHeight="1dp"       
        android:listSelector="@drawable/list_selector"
        />

    <ListView
        android:id="@android:id/list"
       android:layout_weight="1"
        android:layout_height="wrap_content" 
        android:layout_width="320dp"
    android:orientation="vertical"
    android:layout_gravity="start"
    android:background="#ffffff"
        android:minHeight="64dip"
       android:layout_alignBottom="@+id/Person"
        android:choiceMode="singleChoice"
        android:divider="#ECF0F1"
        android:dividerHeight="1dp"       
        android:listSelector="@drawable/list_selector"
        />

   </LinearLayout>

  </com.bernard.beaconportal.activities.activity.VerticalScrollview>

</android.support.v4.widget.DrawerLayout>

I also tried setting that to the scrollview (mDrawerLayout.closeDrawer(mScrollView);), and that threw another error.

08-15 17:19:57.286: E/AndroidRuntime(2719): FATAL EXCEPTION: main
08-15 17:19:57.286: E/AndroidRuntime(2719): Process: com.bernard.beaconportal.activities, PID: 2719
08-15 17:19:57.286: E/AndroidRuntime(2719): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bernard.beaconportal.activities/com.bernard.beaconportal.activities.activity.MessageList}: java.lang.NullPointerException
08-15 17:19:57.286: E/AndroidRuntime(2719):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
08-15 17:19:57.286: E/AndroidRuntime(2719):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2274)
08-15 17:19:57.286: E/AndroidRuntime(2719):     at android.app.ActivityThread.access$800(ActivityThread.java:145)
08-15 17:19:57.286: E/AndroidRuntime(2719):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
08-15 17:19:57.286: E/AndroidRuntime(2719):     at android.os.Handler.dispatchMessage(Handler.java:102)
08-15 17:19:57.286: E/AndroidRuntime(2719):     at android.os.Looper.loop(Looper.java:136)
08-15 17:19:57.286: E/AndroidRuntime(2719):     at android.app.ActivityThread.main(ActivityThread.java:5144)
08-15 17:19:57.286: E/AndroidRuntime(2719):     at java.lang.reflect.Method.invokeNative(Native Method)
08-15 17:19:57.286: E/AndroidRuntime(2719):     at java.lang.reflect.Method.invoke(Method.java:515)
08-15 17:19:57.286: E/AndroidRuntime(2719):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
08-15 17:19:57.286: E/AndroidRuntime(2719):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
08-15 17:19:57.286: E/AndroidRuntime(2719):     at dalvik.system.NativeStart.main(Native Method)
08-15 17:19:57.286: E/AndroidRuntime(2719): Caused by: java.lang.NullPointerException
08-15 17:19:57.286: E/AndroidRuntime(2719):     at android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:968)
08-15 17:19:57.286: E/AndroidRuntime(2719):     at android.support.v4.widget.DrawerLayout.closeDrawer(DrawerLayout.java:1169)
08-15 17:19:57.286: E/AndroidRuntime(2719):     at com.bernard.beaconportal.activities.activity.MessageList.selectItem_Inbox(MessageList.java:3151)
08-15 17:19:57.286: E/AndroidRuntime(2719):     at com.bernard.beaconportal.activities.activity.MessageList.onCreate(MessageList.java:718)
08-15 17:19:57.286: E/AndroidRuntime(2719):     at android.app.Activity.performCreate(Activity.java:5434)
08-15 17:19:57.286: E/AndroidRuntime(2719):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-15 17:19:57.286: E/AndroidRuntime(2719):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2179)
08-15 17:19:57.286: E/AndroidRuntime(2719):     ... 11 more
08-15 17:20:01.476: E/k9(2719): Unable to open connection for Lincoln:Drafts/Thread-1312/conn1105736992

Activity that contains the Navigation Drawer is located here: http://pastebin.com/F7ZF3s5M

Community
  • 1
  • 1
I'm_With_Stupid
  • 1,112
  • 4
  • 16
  • 35
  • The problem might comes from this line: `mDrawerLayout.closeDrawer(mDrawerLinear);`. Try to use this instead: `mDrawerLayout.closeDrawer(Gravity.Left);`... Then, why don't you use a `ExpandableListView`? – G.T. Aug 15 '14 at 16:18
  • 7
    You should use a single `ListView`. There's no reason to use three of them if they have the same behavior. – user Aug 15 '14 at 16:21
  • @Luksprog Unfortunately they don't have the same behavior – I'm_With_Stupid Aug 15 '14 at 17:57
  • 1
    How is that? You have the exact `ListView` definition for all of them(along with some useless or wrong attributes). *and I want each listview to use the same scrollbar, so they are stationary, except they all scroll together.* - how exactly do you want to achieve this? If the `ListView` are big enough they need to be scrolled to see the content(and you would need to indicate this with a scrollbar), however you want a single scroll and all the ListView to scroll together. Something is wrong in your explanation. – user Aug 15 '14 at 18:47
  • @Luksprog Some of the ListView are rendered differently, some do have icons, some don't have icons, some use different view holders. Some you can long click and some you can't. They are all very different ListViews. – I'm_With_Stupid Aug 15 '14 at 20:10
  • @G.T. mDrawerLayout.closeDrawer(Gravity.Left); didn't work, it through the error here: http://stackoverflow.com/questions/18547277/how-to-set-navigation-drawer-to-be-opened-from-right-to-left . I also tried setting that to the scrollview (mDrawerLayout.closeDrawer(mScrollView);), and that through another error. I've added the error to the answer – I'm_With_Stupid Aug 15 '14 at 21:21
  • 2
    *are rendered differently* - you can use different row types(you could probably make it easier for you by using Commonsware's MergeAdapter). *Some you can long click and some you can't. * - can't you just set a `OnLongClickListener` for the entire ListView and drop the events for the rows that shouldn't have it? I'm insisting on this because you're nesting scrollable widgets(ListViews) in other scrollable widgets which is rarely a good idea in Android. – user Aug 16 '14 at 05:52
  • 1
    I'm with Luksprog. The problem is your design decision. Just use one listview that has 3 different view types. Problem solved. – Ifrit Aug 16 '14 at 16:25
  • @I'm_With_Stupid i implemented something like: http://i.imgur.com/DifSwz6.png those scroll together in navigation drawer. – mmlooloo Aug 17 '14 at 04:15
  • @mmlooloo how did you do that? – I'm_With_Stupid Aug 20 '14 at 15:58
  • do you accept it if i post complete solution with +50 reputation? – mmlooloo Aug 20 '14 at 16:01
  • @mmlooloo If it works, then of course I'll select your answer. – I'm_With_Stupid Aug 20 '14 at 16:05

2 Answers2

1

We shouldn’t use ListView inside the ScrollView because the list view is already ScrollView, the items in list view are already scrollable..

When we try to add the ListView into the ScrollView, we may face a problem that the list view will show only one item from the adapter.

Solution: Calculate the size of the list items and set the height of the ListView programmatically.

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter(); 
    if (listAdapter == null) {
        // pre-condition
        return;
    }

    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}

Origin: android-listview-inside-scrollview Post from blog helped me to solve this issue. setListViewHeightBasedOnChildren & how-can-i-put-a-listview-into-a-scrollview-without-it-collapsing

Community
  • 1
  • 1
Harsha Vardhan
  • 3,324
  • 2
  • 17
  • 22
  • Using this the problem still persists, how to scroll all the ListViews as one. This solves the problem of making the ListView almost like a Linear Layout, but we can't scroll it, so its almost useless. My question asks for a way to scroll it, not a way to turn it into a LinearLayout type view. – I'm_With_Stupid Aug 20 '14 at 15:17
  • Hi I'm_With_Stupid, Post is abt add multiple listview in scroll view and the below answer giving by u is not linked with scroll view. What is ur question abt? merging of adapter in list view or adding listview in scroll view? – Harsha Vardhan Aug 27 '14 at 10:59
  • the answer was merging the adapters, scrollview would have been easier. But using a ScrollView seems almost "forbidden". – I'm_With_Stupid Aug 27 '14 at 16:08
0

So it turns out that using the merge adapter from CommonsGuy was the way to go, now the problem is just figuring out how to treat each section differently in the listview, like its, its own separate ListView. Oh well, at least my initial problem has been solved.

First download the library, then set it up and import it.

then I declared this before onCreate

private MergeAdapter mergeadapter = null;

Then in onCreate

mergeadapter = new MergeAdapter();

Then when populating the listView which is populated last

public void populateListView(Account[] realAccounts) {
        List<BaseAccount> accounts = new ArrayList<BaseAccount>();

        if (displaySpecialAccounts() && !K9.isHideSpecialAccounts()) {
            BaseAccount unifiedInboxAccount = SearchAccount
                    .createUnifiedInboxAccount(this);
            BaseAccount allMessagesAccount = SearchAccount
                    .createAllMessagesAccount(this);

            accounts.add(unifiedInboxAccount);
            accounts.add(allMessagesAccount);
        }

        accounts.addAll(Arrays.asList(realAccounts));
        AccountsAdapter adapter = new AccountsAdapter(accounts);

        mDrawerList_Inbox = (ListView) findViewById(R.id.listview_inbox);

        mDrawerList_Inbox.setAdapter(adapter);
        mDrawerList_Inbox.invalidate();

        LayoutInflater inflater = getLayoutInflater();

        View header_folders = (View) inflater.inflate(R.layout.header_folders,
                null);

        View header_drawer = (View) inflater.inflate(R.layout.header_drawer,
                null);

        View header_inbox = (View) inflater.inflate(R.layout.header_inbox, null);



                mergeadapter.addView(header_inbox);

                mergeadapter.addAdapter(mAdapter_Accounts);

                mergeadapter.addView(header_drawer);

                 mergeadapter.addAdapter(mMenuAdapter);

                 mergeadapter.addView(header_folders);

                 mergeadapter.addAdapter(mAdapter);

                 mDrawerList.setAdapter(mergeadapter);



    }
I'm_With_Stupid
  • 1,112
  • 4
  • 16
  • 35