在xamarin

时间:2015-07-01 11:04:24

标签: android listview android-listview xamarin scrollview

我是一名Windows开发人员。我对android没有太多的了解。所以我的场景是这样的:我有一个scrollview,其子项为linearlayout。线性布局有3个列表视图。我想显示所有3个列表视图中的所有项目。滚动将由顶级滚动视图处理。

这会有用吗?

<ScrollView
    android:scrollbars="vertical"
    android:scrollbarStyle="insideOverlay"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/scrollView1"
    android:fadeScrollbars="true">
    <LinearLayout
        android:orientation="vertical"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/linearLayout2">
        <RelativeLayout xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:id="@+id/linear_currentSubEmpty"
            android:layout_height="match_parent">

            <LinearLayout
                android:orientation="vertical"
                android:background="@android:color/transparent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/currentListViewLayout"
                android:layout_below="@+id/currentSubsView">
                <com.fortysevendeg.swipelistview.SwipeListView xmlns:swipe="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/currentSubList"
                    android:listSelector="#00000000"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    swipe:swipeFrontView="@+id/front"
                    android:background="@android:color/transparent"
                    swipe:swipeBackView="@+id/back"
                    swipe:swipeDrawableChecked="@drawable/choice_selected"
                    swipe:swipeDrawableUnchecked="@drawable/choice_unselected"
                    swipe:swipeCloseAllItemsWhenMoveList="true"
                    swipe:swipeMode="left"
                    swipe:swipeActionLeft="reveal"
                    swipe:swipeOpenOnLongPress="false"
                    android:layout_weight="1" />
            </LinearLayout>
        </RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:id="@+id/pastViewLayout"
            android:layout_height="match_parent">

            <RelativeLayout xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:id="@+id/linear_pastSubEmpty"
                android:layout_height="wrap_content"
                android:layout_below="@+id/pastBottomView">
                <ListView
                    android:minWidth="25px"
                    android:minHeight="25px"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/pastSubList"
                    android:divider="#ffffffff"
                    android:dividerHeight="1dp"
                    android:layout_weight="1" />
            </RelativeLayout>
        </RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:id="@+id/suspViewLayout"
            android:layout_height="match_parent">

            <RelativeLayout xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:id="@+id/linear_suspendedSubEmpty"
                android:layout_height="wrap_content"
                android:layout_below="@+id/suspBottomView">
                <ListView
                    android:minWidth="25px"
                    android:minHeight="25px"
                    android:id="@+id/suspSubList"
                    android:divider="#ffffffff"
                    android:dividerHeight="1dp"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </RelativeLayout>
        </RelativeLayout>
    </LinearLayout>
</ScrollView>

1 个答案:

答案 0 :(得分:0)

我知道没有直接的方法来实现这一点,我遵循以下方法。

public static void SetListViewHeightBasedOnChildren(ListView listView) {
        IListAdapter listAdapter = listView.Adapter;
        if (listAdapter == null) {
            return;
        }

        int totalHeight = listView.PaddingTop + listView.PaddingBottom;
        for (int i = 0; i < listAdapter.Count; i++) {
            View listItem = listAdapter.GetView(i, null, listView);
            if (listItem is ViewGroup) {
                listItem.LayoutParameters = new global::Android.Views.ViewGroup.LayoutParams (global::Android.Views.ViewGroup.LayoutParams.WrapContent, global::Android.Views.ViewGroup.LayoutParams.WrapContent);
            }
            listItem.Measure(0, 0);
            totalHeight += listItem.MeasuredHeight;
        }

        global::Android.Views.ViewGroup.LayoutParams parameters = listView.LayoutParameters;
        parameters.Height = totalHeight + (listView.DividerHeight * (listAdapter.Count - 1));
        listView.LayoutParameters = parameters;
    }