【问题标题】:I have a ListView wrapped in a ScrollView to display a list of strings under a header, but not displaying properly我有一个包含在 ScrollView 中的 ListView 以在标题下显示字符串列表,但无法正确显示
【发布时间】:2020-10-20 04:24:32
【问题描述】:

我正在尝试在 android 应用程序上显示一些内容,包括一个标题和一个 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;
    }
}

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

谢谢你, 阿尔伯特

【问题讨论】:

    标签: java android android-layout android-listview


    【解决方案1】:

    最好不要将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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-21
      • 1970-01-01
      • 2013-08-25
      • 2017-08-21
      • 1970-01-01
      相关资源
      最近更新 更多