【问题标题】:Listview in Cardview enable scrollCardview中的Listview启用滚动
【发布时间】:2015-08-19 23:40:39
【问题描述】:

我正在创建一个待办事项列表。现在我想在 RecylerView 中显示待办事项列表,其中包含卡片以及显示文本视图(标题)和列表视图的卡片。

Listview 中的内容应该是 TodoList 中的任务。

我想在不滚动的情况下修复 Listview,这可能吗?

Card_Layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="4dp"
    card_view:contentPadding="8dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    card_view:cardBackgroundColor="@color/abc_background_cache_hint_selector_material_light"
    android:background="@drawable/big_card"

    android:elevation="@dimen/elevation_high">

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

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:gravity="center_vertical"
            />

        <ListView
            android:id="@+id/cardList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingTop="16dp"
            android:paddingBottom="16dp"
            android:isScrollContainer="false"
            >

        </ListView>
    </LinearLayout>
</android.support.v7.widget.CardView>

RecylerView.xml 显示卡片视图

 <FrameLayout
            android:id="@+id/frame_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/cardList"
                android:layout_width="match_parent"
                android:layout_height="match_parent"


                />

P.s:这种方式 List in List 是一个不好的解决方案吗?有没有更好的解决方案来解决这个问题?

【问题讨论】:

    标签: android listview android-recyclerview android-cardview


    【解决方案1】:

    只需在为列表视图设置适配器后使用此调用,如下所示

    cardlist.setadapter(...);
    justifyListViewHeightBasedOnChildren(cardlist);
    
     public void justifyListViewHeightBasedOnChildren(ListView listView) {
    
            ListAdapter adapter = listView.getAdapter();
    
            if (adapter == null) {
                return;
            }
            ViewGroup vg = listView;
            int totalHeight = 0;
            for (int i = 0; i < adapter.getCount(); i++) {
                View listItem = adapter.getView(i, null, vg);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }
    
            ViewGroup.LayoutParams par = listView.getLayoutParams();
            par.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
            listView.setLayoutParams(par);
            listView.requestLayout();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-22
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      相关资源
      最近更新 更多