我正在尝试将导航抽屉添加到k9邮件中,并且这样做是尝试创建具有三个列表视图的导航抽屉,并且我希望每个列表视图使用相同的滚动条,因此它们是静止的,除了它们全部滚动在一起。基本上我希望ListViews像一个列表一样,一起滚动并堆叠在一起。但是,每当我尝试通过this answer将ListViews添加到ScrollView时,我都会收到错误消息。现在,如何在没有抛出错误的情况下在DrawerLayout中使用ScrollView?
抛出错误
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文件
<?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>
我也尝试将其设置为scrollview(mDrawerLayout.closeDrawer(mScrollView);),并引发了另一个错误。
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
包含导航抽屉的活动位于此处:http://pastebin.com/F7ZF3s5M
答案 0 :(得分:1)
我们不应该在ScrollView中使用ListView,因为列表视图已经是ScrollView,列表视图中的项目已经可以滚动了。
当我们尝试将ListView添加到ScrollView时,我们可能会遇到一个问题,即列表视图只会显示适配器中的一个项目。
解决方案: 计算列表项的大小并以编程方式设置ListView的高度。
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();
}
来源:android-listview-inside-scrollview来自博客的帖子帮助我解决了这个问题。 setListViewHeightBasedOnChildren&amp; how-can-i-put-a-listview-into-a-scrollview-without-it-collapsing
答案 1 :(得分:0)
所以事实证明使用merge adapter from CommonsGuy是可行的方法,现在问题只是弄清楚如何在列表视图中区别对待每个部分,就像它自己独立的ListView一样。哦,至少我最初的问题已经解决了。
首先下载该库,然后进行设置并导入它。
然后我在onCreate
之前宣布了这个private MergeAdapter mergeadapter = null;
然后在onCreate
mergeadapter = new MergeAdapter();
然后填充最后填充的listView
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);
}