如果内容的高度大于屏幕,CardView中的ListView不会显示所有内容

时间:2018-07-19 07:30:24

标签: android android-layout listview android-cardview

我正在制作电话簿,它可以工作,但是如果屏幕分辨率小于内容高度,则并非ListView的所有项目都显示出来,它们会被留下: (请参阅最后一项)

enter image description here enter image description here

但是,如果内容环绕到屏幕高度,则显示效果很好:

enter image description here

我该如何解决?

我的代码:

CardView布局

"

我的(自定义)列表视图布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    >
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/phone_cv"
        app:cardElevation="3dp"
        app:contentPadding="10dp"
        app:cardCornerRadius="3dp"
        android:backgroundTint="#ffffff"
        android:layout_marginBottom="10dp"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/phone_cv_person_name"
                android:text="organisation name"
                android:textSize="18sp"
                android:textColor="#000000"
                />
            <View
                android:layout_height="1dp"
                android:layout_width="match_parent"
                android:background="#000000"
                android:layout_marginTop="4dp"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/phone_cv_person_doljnost"
                android:text="Limpus Dda Lorem"
                android:visibility="gone"
                />
            <ListView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/phone_cv_list"
                android:scrollbars="none"
                />
        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

通过下一个代码设置cardview:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/phone_persons_list_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="Name"
        android:textSize="14sp"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        android:paddingTop="1dp"
        android:paddingBottom="0dp"
        android:textColor="#000"
        />
    <TextView
        android:id="@+id/phone_persons_list_otdel"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="Otdel"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        android:paddingTop="1dp"
        android:paddingBottom="0dp"
        android:visibility="gone"
        android:textColor="#CC000000"
        android:textSize="12sp"
        />
    <TextView
        android:id="@+id/phone_persons_list_doljnost"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="Doljnost"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        android:paddingTop="1dp"
        android:paddingBottom="0dp"
        android:textColor="#CC000000"
        android:visibility="gone"
        android:textSize="12sp"
        />
    <View
        android:layout_height="0.5dp"
        android:layout_width="match_parent"
        android:background="#11EEEEEE"
        android:layout_marginTop="4dp"
        />
</LinearLayout>

在RVAdapter内,我正在设置列表视图适配器:

    mRecyclerView = (RecyclerView) findViewById(R.id.phones_rv);
    mRecyclerView.setHasFixedSize(true);

    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);

    mAdapter = new RVAdapter(data);
    mRecyclerView.setAdapter(mAdapter);

然后,使用getTotalHeightOfListView函数动态设置列表视图的高度:

credits to Cristiano

final PhoneNumbersContactListAdapter mAdapter = new PhoneNumbersContactListAdapter(mContext, mDataset.get(position).getPhGosData());
holder.mListView.setAdapter(mAdapter);

getTotalHeightOfListView(holder.mListView);

2 个答案:

答案 0 :(得分:0)

我的(自定义)列表视图布局:

设置computeIfPresent中的ConcurrentHashMap.computeIfAbsent或给出android:layout_height="wrap_content"linear layout之类的固定高度

答案 1 :(得分:0)

为此,您必须根据Viewholder类内部的屏幕分辨率设置列表项的高度。 像这样。

  Resources resources = context.getResources();
  RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.width = AppHelper.getScreenWidth() / 2;
            params.height = (int) AppHelper.convertDpToPixel((float) 200.0,resources);
            // params.setMargins(10,10,10,10);
            params.leftMargin = 10;
            params.rightMargin = 10;
            params.topMargin = 10;
            params.bottomMargin = 10;
            yourlistlayout.setLayoutParams(params);

//将dp转换为像素功能

public static float convertDpToPixel(float dp, Resources resources) {
        DisplayMetrics metrics = resources.getDisplayMetrics();
        return dp * ((float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
    }