我有一个 ListView 包裹在 ScrollView 中以显示 header 下的字符串列表,但显示不正确

I have a ListView wrapped in a ScrollView to display a list of strings under a header, but not displaying properly

我正在尝试在 android 应用程序上显示一些内容,包括 header 和 ArrayList URL。现在我正在使用占位符数据,我想使用 ListView 来显示字符串的 ArrayList,但它只显示第一个元素。这是 XML 和 java 代码。

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

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/picture_background" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="48sp"
                android:textStyle="bold"
                android:textColor="@android:color/white"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true"
                tools:text="Home"/>

        </RelativeLayout>

        <ListView
            android:id="@+id/list"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

</ScrollView>

这就是我要显示的内容,这是 java 代码。

package com.zunkufteducation;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import androidx.fragment.app.Fragment;

import java.util.ArrayList;

public class HomeFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.category, container, false);

        ArrayList<String> urls = new ArrayList<>();
        urls.add("wrsgarsg");
        urls.add("arhn");
        urls.add("asrnhnet");
        urls.add("awgetsh4q");
        urls.add("aerfn er");
        urls.add("arehbbsdva");
        urls.add("aerherhgsa");
        urls.add("adreasrfar");
        ListView listView = (ListView) rootView.findViewById(R.id.list);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
                android.R.layout.simple_list_item_1, urls);
        listView.setAdapter(adapter);
        return rootView;
    }
}

我很困惑为什么它不起作用,感谢任何帮助。

谢谢, 艾伯特

最好不要将 ListView 包裹在像 ScrollViewNestedScrollView 这样的可滚动视图中,这会使 ListView 无法滚动;相反,您可以去掉 ScrollView 或改用 RecyclerView

虽然有一个黑客可以通过使用以下方法以编程方式重新调整 ListView 的高度来克服这个问题

private static void setListViewHeightBasedOnChildren
            (ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) return;

        int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
                View.MeasureSpec.UNSPECIFIED);
        int totalHeight = 0;
        View view = null;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            view = listAdapter.getView(i, view, listView);
            if (i == 0) view.setLayoutParams(new
                    ViewGroup.LayoutParams(desiredWidth,
                    ViewGroup.LayoutParams.WRAP_CONTENT));

            view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
            totalHeight += view.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();

        params.height = totalHeight + (listView.getDividerHeight() *
                (listAdapter.getCount() - 1));

        listView.setLayoutParams(params);
        listView.requestLayout();
    } 

并在设置 ListView

后调用它
public class HomeFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.category, container, false);

        // ... rest of your code

        listView.setAdapter(adapter);
        setListViewHeightBasedOnChildren(listView);
        return rootView;
    }
}