【问题标题】:How to make XML scrollable with multiple listview and increase the size of listview as per entry?如何使用多个列表视图使 XML 可滚动并根据条目增加列表视图的大小?
【发布时间】:2014-11-28 09:56:00
【问题描述】:

我的布局看起来像

我希望页面可以滚动并动态添加列表视图。 现在当数据超过页面高度时,我无法向上滚动页面。

xml在这里

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/buttonlayout"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:background="#1274B9"
        android:gravity="left|top"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/settings"
            android:layout_width="54dp"
            android:layout_height="match_parent"
            android:background="@drawable/backarrow" />

        <ImageView
            android:id="@+id/prevMonth"
            android:layout_width="20dp"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:clickable="true"
            android:onClick="prevDate"
            android:src="@drawable/calendar_left_arrow_selector" >
        </ImageView>

        <TextView
            android:id="@+id/currentMonth"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_weight="0.6"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#FFFFFF" >
        </TextView>

        <ImageView
            android:id="@+id/nextMonth"
            android:layout_width="20sp"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:layout_marginRight="10sp"
            android:clickable="true"
            android:onClick="nextDate"
            android:src="@drawable/calendar_right_arrow_selector" >
        </ImageView>

        <Button
            android:id="@+id/addEvent"
            android:layout_width="54sp"
            android:layout_height="match_parent"
            android:background="@drawable/check"
            android:onClick="gotomealsactivity" />
    </LinearLayout>

    <!-- Breakfast portion -->

    <RelativeLayout
        android:id="@+id/relativebreakfastheading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/buttonlayout"
        android:background="#1274B9" >

        <TextView
            android:id="@+id/breakfastheading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Breakfast"
            android:textColor="#ffffff"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/totalcalorieheading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="10dp"
            android:gravity="right"

            android:textColor="#ffffff"
            android:textSize="20sp" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativebreakfastlist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/relativebreakfastheading" >

        <ListView
            android:id="@+id/listbreakfast"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>

        <TextView
            android:id="@+id/addbreakfast"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/listbreakfast"
            android:layout_centerHorizontal="true"
            android:clickable="true"
            android:gravity="center"
            android:onClick="gotobreakfastactivity"
            android:text="Add Food +"
            android:textStyle="bold" />

        <View
            android:id="@+id/view1"
            android:layout_width="fill_parent"
            android:layout_height="1dip"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/addbreakfast"
            android:background="#ff106510" />
    </RelativeLayout>

    <!-- Lunch portion -->

    <RelativeLayout
        android:id="@+id/relativelunchheading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/relativebreakfastlist"
        android:background="#1274B9" >

        <TextView
            android:id="@+id/lunchheading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Lunch"
            android:textColor="#ffffff"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/totalcalorielunchheading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="10dp"
            android:gravity="right"

            android:textColor="#ffffff"
            android:textSize="20sp" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativelunchlist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/relativelunchheading" >

        <ListView
            android:id="@+id/listlunch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>

        <TextView
            android:id="@+id/addlunch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/listlunch"
            android:layout_centerHorizontal="true"
            android:clickable="true"
            android:gravity="center"
            android:onClick="gotolunchactivity"
            android:text="Add Food +"
            android:textStyle="bold" />

        <View
            android:id="@+id/view2"
            android:layout_width="fill_parent"
            android:layout_height="1dip"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/addlunch"
            android:background="#ff106510" />
    </RelativeLayout>

    <!-- Dinner portion -->

    <RelativeLayout
        android:id="@+id/relativedinnerheading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/relativelunchlist"
        android:background="#1274B9" >

        <TextView
            android:id="@+id/dinnerheading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Dinner"
            android:textColor="#ffffff"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/totalcaloriedinnerheading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="10dp"
            android:gravity="right"

            android:textColor="#ffffff"
            android:textSize="20sp" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativedinnerlist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/relativedinnerheading" >

        <ListView
            android:id="@+id/listdinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>

        <TextView
            android:id="@+id/adddinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/listdinner"
            android:layout_centerHorizontal="true"
            android:clickable="true"
            android:gravity="center"
            android:onClick="gotodinneractivity"
            android:text="Add Food +"
            android:textStyle="bold" />

        <View
            android:id="@+id/view3"
            android:layout_width="fill_parent"
            android:layout_height="1dip"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/adddinner"
            android:background="#ff106510" />
    </RelativeLayout>

    <!-- Snacks portion -->

    <RelativeLayout
        android:id="@+id/relativesnacksheading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/relativedinnerlist"
        android:background="#1274B9" >

        <TextView
            android:id="@+id/snacksheading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Snacks"
            android:textColor="#ffffff"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/totalcaloriesnacksheading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="10dp"
            android:gravity="right"

            android:textColor="#ffffff"
            android:textSize="20sp" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativesnackslist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/relativesnacksheading" >

        <ListView
            android:id="@+id/listsnacks"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>

        <TextView
            android:id="@+id/addsnacks"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/listsnacks"
            android:layout_centerHorizontal="true"
            android:clickable="true"
            android:gravity="center"
            android:onClick="gotosnacksactivity"
            android:text="Add Food +"
            android:textStyle="bold" />

        <View
            android:id="@+id/view4"
            android:layout_width="fill_parent"
            android:layout_height="1dip"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/addsnacks"
            android:background="#ff106510" />
    </RelativeLayout>

</RelativeLayout>

【问题讨论】:

  • 当前滚动不起作用,页面也没有滚动。但是以防万一,我应该怎么做才能使它无法滚动?
  • 您是否要将ListView 放入ScrollView 中?
  • 是的..我正在这样做。
  • 好吧,不建议您这样做。最好改用ExpandableListView

标签: android xml listview android-listview


【解决方案1】:

您可以找到屏幕高度并将其与 listBottom 进行比较 - 一些 ListView 底部的坐标,并在需要时添加新的 ListView。

int[] coords = {0,0};
list.getLocationOnScreen(coords);
int listBottom = coords[1] + list.getHeight();

或者问题是你的布局中没有 ScrollView?

【讨论】:

  • 如果数据更多,它会让我的页面可以滚动吗?
  • 我认为你可以在布局中制作全局滚动视图并将 ListViews 设置为根本不滚动。
  • 它的全球性!但是我应该在列表视图中添加什么......?
  • 尝试添加一个ScrollView。
  • 这取决于您处理手势的位置。我正在使用这个 int onTouch(View v, MotionEvent event) :if (v == sc) openText.getParent().requestDisallowInterceptTouchEvent(false); if (v == openText) openText.getParent().requestDisallowInterceptTouchEvent(true);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-21
  • 1970-01-01
  • 2013-08-17
相关资源
最近更新 更多