ViewView里面的ListView问题小显示

时间:2016-07-31 14:15:50

标签: android listview android-fragments android-viewpager android-scrollview

我在Fragment中实现了一个listView,它附加到ViewPager。在片段中,我还必须显示一个按钮,它将向listView添加新元素(由适配器管理)。

在大屏幕(4英寸或更多)上,lisView显示效果很好,但在较小的屏幕上(少于4英寸)用户只能看到一个元素,他们必须滚动一个微观列表视图。

我希望命令listView在变为可滚动之前显示至少3个元素,而是允许用户滚动片段并使listView上方的所有元素消失,包括按钮

The problem, the listView has just space for one element

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin">


<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="@dimen/activity_horizontal_margin"
    ads:adSize="BANNER"
    ads:adUnitId="@string/banner_ad_unit_id" />

<Button
    android:id="@+id/btn_add_custom_timer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="@dimen/activity_horizontal_margin"
    android:background="@color/colorAccent"
    android:drawableLeft="@drawable/ic_timer_black_24dp"
    android:padding="@dimen/activity_vertical_margin"
    android:text="@string/add_custom_timer" />

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.9"
    tools:context=".fragments.ActiveTimersFragment">

    <TextView
        android:id="@+id/fragment_active_timers_emptyView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="@string/no_active_timers" />

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

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/col"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/activity_main_appbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:elevation="8dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/activity_main_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:elevation="0dp"
            app:layout_scrollFlags="scroll|enterAlways"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="?attr/listPreferredItemHeight"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="?attr/actionBarSize"
                android:contentDescription="@string/app_name"
                android:scaleType="center"
                android:src="@drawable/ic_logo" />
        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/activity_main_tabLayout"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="bottom"
            android:background="@color/colorPrimary"
            android:elevation="0dp"
            app:tabGravity="fill"
            app:tabMode="scrollable" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/activity_main_viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:elevation="8dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

1 个答案:

答案 0 :(得分:0)

尝试添加:

android:minHeight="100dp"

到xml中的ListView元素。 玩100值,直到你看到3行。

<强>更新

你还需要将整个布局放在ScrollView中以使整个事物可滚动,并使其高度wrap_content而不是match_parent:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin">


<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="@dimen/activity_horizontal_margin"
    ads:adSize="BANNER"
    ads:adUnitId="@string/banner_ad_unit_id" />

<Button
    android:id="@+id/btn_add_custom_timer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="@dimen/activity_horizontal_margin"
    android:background="@color/colorAccent"
    android:drawableLeft="@drawable/ic_timer_black_24dp"
    android:padding="@dimen/activity_vertical_margin"
    android:text="@string/add_custom_timer" />

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.9"
    tools:context=".fragments.ActiveTimersFragment">

    <TextView
        android:id="@+id/fragment_active_timers_emptyView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="@string/no_active_timers" />

    <ListView
        android:id="@+id/fragment_active_timers_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minHeight="100dp" />
</FrameLayout>

</LinearLayout>

</ScrollView>