在同一Android Studio页面中显示两个listViews

时间:2018-10-03 18:19:56

标签: java android xml android-studio listview

实际上我有以下问题: 我有两个带有两个适配器的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或其他与此类似的问题中出现的问题。

先谢谢您。

1 个答案:

答案 0 :(得分:0)

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

@ApplicationScoped
public class MockResources {

private EntityManager entityManager;

@PostConstruct
public void init() {
    entityManager = Persistence.createEntityManagerFactory("db-test").createEntityManager();
}

@Produces
public Logger produceLog(InjectionPoint injectionPoint) {
    return LoggerFactory.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
}


@Default
@Produces
public EntityManager getEntityManager() {
    return entityManager;
}

...

示例:

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();
        }