【问题标题】:to display two listViews in the same Android Studio page在同一个 Android Studio 页面中显示两个 listView
【发布时间】:2019-03-09 01:26:34
【问题描述】:

其实我有以下问题: 我有两个ListView 自动加载了两个适配器,问题出在XML 中,两个列表都只显示第一个元素。 这是我的 xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.hrizi.onescore.home_links.searshscore">

    <include
        android:id="@+id/toolbar"
        layout="@layout/apptoolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:scaleType="fitCenter" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="66dp"
        android:fillViewport="true">

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

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

                <EditText
                    android:id="@+id/sv_search"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:background="@drawable/edittextbackground"
                    android:hint="Search ..."
                    android:inputType="text"
                    android:textAlignment="center"></EditText>

                <Button
                    android:id="@+id/btn_search"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginRight="20dp"
                    android:layout_weight="1"
                    android:background="@color/coloronscore"
                    android:onClick="btnsearchOnClick"
                    android:text="Search"
                    android:textAllCaps="false"
                    android:textColor="@color/colorwhite" />
            </LinearLayout>
            <!--Result of research-->
            <ListView
                android:id="@+id/listView1"
                android:layout_width="match_parent"
                android:layout_height="248dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:paddingBottom="5dp" />

            <ListView
                android:id="@+id/listView"
                android:layout_width="match_parent"
                android:layout_height="248dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:paddingBottom="5dp" />
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

我认为ScrollView 或其他类似问题的问题。

提前谢谢你。

【问题讨论】:

  • 因为你在ListView里面使用ScrollView
  • 那么,请问有什么解决方案@Yupi
  • stackoverflow.com/questions/6210895/… - 也许这会有所帮助
  • @hatemdagbouj 你用两个ListView做什么?为什么不只使用 1 个ListView

标签: java android xml android-studio listview


【解决方案1】:

您可以使用此功能显示列表视图中的所有项目。

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

            int totalHeight = 0;
            int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
            for (int i = 0; i < listAdapter.getCount(); i++) {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
                totalHeight += listItem.getMeasuredHeight();
            }

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

例子:

setListViewHeightBasedOnChildren(yourlistview);

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 1970-01-01
    相关资源
    最近更新 更多