添加fillViewport = true

时间:2015-05-07 09:56:17

标签: android listview scrollview

我已将fillViewport设置为true以使ListView具有正确的大小,但这会导致ScrollView无法滚动。 如果我对listView使用固定大小,它可以工作,但我不希望listViews可滚动。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.pascal.x"
android:background="#FFFFFF">

<!-- TODO: Update blank fragment layout -->

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="+ add stuff"
        android:id="@+id/button"
        android:background="#00AEEF"
        android:textColor="#FFF"
        android:layout_gravity="center_horizontal|top" />

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:fillViewport="false"
        android:layout_gravity="center_horizontal|bottom"
        android:scrollbars="vertical"
        android:id="@+id/scrollView" >

        <LinearLayout
            android:orientation="vertical"
            android:scrollbars="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="stuff"
                android:id="@+id/textView"
                android:textColor="#00AEEF" />

            <ListView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:id="@+id/listView"
                android:layout_gravity="center_horizontal" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="other stuff"
                android:id="@+id/textView2"
                android:textColor="#00AEEF" />

            <ListView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:id="@+id/listView2" />

        </LinearLayout>
    </ScrollView>
</LinearLayout>

2 个答案:

答案 0 :(得分:2)

使用此视图代替listview

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ListView;

public class ExpandableListView extends ListView {

boolean expanded = true;

public ExpandableListView(Context context) {
    super(context);
}

public ExpandableListView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

    public ExpandableListView(Context context, AttributeSet attrs, int  defStyle)       
{
    super(context, attrs, defStyle);
}

public boolean isExpanded() {
    return expanded;
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // HACK! TAKE THAT ANDROID!
    if (isExpanded()) {
        // Calculate entire height by providing a very large height hint.
        // View.MEASURED_SIZE_MASK represents the largest height possible.
        int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,   MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);

        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    } else {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}




}

在XML中使用它

<yourpackagename(where you put this file).ExpandableListView
 .....
 .....
 ...../>

答案 1 :(得分:0)

我在这里找到了解决问题的方法: https://stackoverflow.com/a/3495908/4743291

    public class Utility {
    public static void setListViewHeightBasedOnChildren(ListView listView) {
          ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
        // pre-condition
              return;
        }

        int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
        for (int i = 0; i < listAdapter.getCount(); i++) {
             View listItem = listAdapter.getView(i, null, listView);
             if (listItem instanceof ViewGroup) {
                listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
             }
             listItem.measure(0, 0);
             totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
                  listView.setLayoutParams(params);
    }
 }