Android ScrollView中的Listview Height问题

Jee*_*mar 3 android listview android-listview

我在Android ListView内部使用ScrollView。因此,众所周知,它将发生高度问题。为此,我ListView使用以下代码设置大小:

public static void getListViewSize(ListView myListView, Context context) {
        ListAdapter myListAdapter = myListView.getAdapter();

        if (myListAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int size = 0; size < myListAdapter.getCount(); size++) {

            View listItem = myListAdapter.getView(size, null, myListView);
            if (listItem instanceof ViewGroup)
                listItem.setLayoutParams(new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT));

            WindowManager wm = (WindowManager) context
                    .getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            @SuppressWarnings("deprecation")
            int screenWidth = display.getWidth();
            int listViewWidth = screenWidth - 65;
            int widthSpec = MeasureSpec.makeMeasureSpec(listViewWidth,
                    MeasureSpec.AT_MOST);
            listItem.measure(widthSpec, 0);

            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = myListView.getLayoutParams();
        params.height = totalHeight
                + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
        myListView.setLayoutParams(params);
        myListView.requestLayout();
    }
Run Code Online (Sandbox Code Playgroud)

Xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white" >

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

        <ListView
            android:id="@+id/lvHomeStream"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:divider="#b5b5b5"
            android:dividerHeight="1dp" >
        </ListView>

        <TextView
            android:id="@+id/tvMoreRecords"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="15dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/list"
            android:gravity="center"
            android:paddingBottom="5dp"
            android:paddingTop="5dp"
            android:text="@string/viewmore"
            android:textColor="@color/blue"
            android:textSize="16sp" />
    </LinearLayout>

</ScrollView>
Run Code Online (Sandbox Code Playgroud)

如果每个ListView项的高度相同,则效果很好。但是,如果每个ListView项目的高度都不正常,则它在ListView端边界和之间会留出额外的大空间TextView

请帮我解决这个问题。

Kai*_*bhi 7

好吧,我在这里分享我的项目代码可能对您有帮助。

此类计算高度并自动调整layoutparams。

解:-

 package com.kview;

 import android.content.Context;
 import android.util.AttributeSet;
 import android.widget.GridView;
 import android.widget.ListView;

 public class FullLengthListView extends ListView {

boolean expanded = true;

public FullLengthListView(Context context, AttributeSet attrs,
        int defaultStyle) {
    super(context, attrs, defaultStyle);
}

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

public boolean isExpanded() {
    return expanded;
}

public void setExpanded(boolean expanded) {
    this.expanded = 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);

        android.view.ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    } else {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
  }
Run Code Online (Sandbox Code Playgroud)

  • 黑客!拿那个安卓吧!=0 (2认同)