在导航抽屉中使用ScrollView

时间:2014-01-09 18:55:33

标签: android navigation-drawer

我的导航抽屉里有一个ListView和两个ImageButtons。我的问题是,如果我的Listview扩展到屏幕以外,它会滚动显示所有listview项目,而不是下面的两个图像按钮。

为了解决这个问题,我尝试添加ScrollView来包装LinearLayout(@ + id /抽屉),但是它给了我以下错误。

是否可以使用带有额外项目的NavDrawer(除了listview)并拥有整体滚动条?

<FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ScrollView
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@drawable/texturebg2"
        android:layout_gravity="start">

    <LinearLayout
        android:id="@+id/drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@drawable/texturebg2"
        android:layout_gravity="start">

        <ListView
            android:id="@+id/list_slidermenu"
            android:layout_width="240dp"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@color/col3"
            android:dividerHeight="1dp"
            android:listSelector="@drawable/menu_selector" />

        <LinearLayout
            android:id="@+id/extrabuttons"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            android:gravity="center_horizontal|bottom"
            android:layout_marginTop="15dp"
            android:layout_marginBottom="15dp" >
            <ImageButton
                android:id="@+id/credit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription = "@string/credit"
                android:onClick="openCredit"
                android:src="@drawable/information"
                android:background="@drawable/mybutton2"
                android:padding="8dip"
                android:text="@string/credit" />

            <ImageButton
                android:id="@+id/exit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription = "@string/exit"
                android:onClick="closeApp"
                android:src="@drawable/ic_exit"
                android:background="@drawable/mybutton2"
                android:padding="8dip"
                android:layout_marginLeft="10dp"
                android:text="@string/exit" />
        </LinearLayout>
    </LinearLayout>
    </ScrollView>

logcat的

01-09 14:04:02.446: E/AndroidRuntime(1072): FATAL EXCEPTION: main
01-09 14:04:02.446: E/AndroidRuntime(1072): Process: com.migrationdesk.mylibman, PID: 1072
01-09 14:04:02.446: E/AndroidRuntime(1072): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.migrationdesk.mylibman/com.migrationdesk.mylibman.NavContainer}: java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
01-09 14:04:02.446: E/AndroidRuntime(1072):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
01-09 14:04:02.446: E/AndroidRuntime(1072):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
01-09 14:04:02.446: E/AndroidRuntime(1072):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
01-09 14:04:02.446: E/AndroidRuntime(1072):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
01-09 14:04:02.446: E/AndroidRuntime(1072):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-09 14:04:02.446: E/AndroidRuntime(1072):     at android.os.Looper.loop(Looper.java:137)
01-09 14:04:02.446: E/AndroidRuntime(1072):     at android.app.ActivityThread.main(ActivityThread.java:4998)
01-09 14:04:02.446: E/AndroidRuntime(1072):     at java.lang.reflect.Method.invokeNative(Native Method)
01-09 14:04:02.446: E/AndroidRuntime(1072):     at java.lang.reflect.Method.invoke(Method.java:515)
01-09 14:04:02.446: E/AndroidRuntime(1072):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-09 14:04:02.446: E/AndroidRuntime(1072):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-09 14:04:02.446: E/AndroidRuntime(1072):     at dalvik.system.NativeStart.main(Native Method)
01-09 14:04:02.446: E/AndroidRuntime(1072): Caused by: java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
01-09 14:04:02.446: E/AndroidRuntime(1072):     at android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:857)
01-09 14:04:02.446: E/AndroidRuntime(1072):     at android.support.v4.widget.DrawerLayout.closeDrawer(DrawerLayout.java:1058)
01-09 14:04:02.446: E/AndroidRuntime(1072):     at com.migrationdesk.mylibman.NavContainer.displayView(NavContainer.java:202)
01-09 14:04:02.446: E/AndroidRuntime(1072):     at com.migrationdesk.mylibman.NavContainer.onCreate(NavContainer.java:116)
01-09 14:04:02.446: E/AndroidRuntime(1072):     at android.app.Activity.performCreate(Activity.java:5243)
01-09 14:04:02.446: E/AndroidRuntime(1072):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-09 14:04:02.446: E/AndroidRuntime(1072):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
01-09 14:04:02.446: E/AndroidRuntime(1072):     ... 11 more

3 个答案:

答案 0 :(得分:1)

<LinearLayout
            android:id="@+id/extrabuttons"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            android:gravity="center_horizontal|bottom"
            android:layout_marginTop="15dp"
            android:layout_marginBottom="15dp" >
            <ImageButton
                android:id="@+id/credit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription = "@string/credit"
                android:onClick="openCredit"
                android:src="@drawable/information"
                android:background="@drawable/mybutton2"
                android:padding="8dip"
                android:text="@string/credit" />

            <ImageButton
                android:id="@+id/exit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription = "@string/exit"
                android:onClick="closeApp"
                android:src="@drawable/ic_exit"
                android:background="@drawable/mybutton2"
                android:padding="8dip"
                android:layout_marginLeft="10dp"
                android:text="@string/exit" />
        </LinearLayout>

将上面的布局放在不同的xml文件中,并将其命名为footer.xml

现在在您设置适配器的java代码中:

LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.footer, null, true);

listView.addFooterView(View);

只需将ListView保留在抽屉中,如:

<ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@color/col3"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/menu_selector" />

现在您的列表视图将滚动到此视图。

答案 1 :(得分:0)

这对我来说非常熟悉,因为我在不久前遇到了同样的问题。首先,将ListView包装到ScrollView通常不是一个好主意(并且可能,如果您使用Eclipse的Android SDK集成版本,则会收到警告),因为ListView已经一个卷轴本身。

其次,问题是由于您的布局定义,ListView正在扩展到屏幕的底部,这会使您的两张图片无法访问。

我解决了这个问题,包含了我希望在LinearLayout vertical方向内显示的内容,如下所示:

<LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:layout_marginTop="20dp" >

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#FFFFFF"
    android:textStyle="italic"
    android:textAppearance="?android:attr/textAppearanceMedium" />

  <ListView
    android:id="@+id/chanlist_list"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1" 
    android:scrollbars="vertical" >
  </ListView>

  <Button
    android:id="@+id/btn_chanlist_close"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:layout_marginTop="10dp"
    android:layout_gravity="center"
    android:onClick="btn_close"
    android:background="@drawable/botones"
    android:textColor="#FFFFFF"
    android:text="@string/btn_close" />          
</LinearLayout>

“问题”是布局会尝试使屏幕适合整个布局定义,即如果布局中有太多元素,则ListView将在短空间内压缩,它会让你难以滚动。尽量简化您的布局。

答案 2 :(得分:0)

您可以使用background图片,因为它会扩展wrap_content,或者您可以使用空图片并使用ImageView.getLayoutParams().width = width;ImageView.getLayoutParams().height = height;