我目前正在使用一个RecyclerView来显示一个卡片列表。它在ListView和ArrayListAdapter上运行得很好,但是现在发生了一些奇怪的事情。
当第一次加载列表时,将正确显示3张显示的第一张卡片。但是那些没有显示出来的在它们下面都有一个很大的空间。
它与LayoutManager有关,正如to所说,它是一个实体,负责管理要显示什么和如何显示它。
在我的例子中,我使用的是LinearLayoutManager。
以下是我在Fragment中初始化的一些代码
layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
listAdapter = new EventsListAdapter(events);
recyclerView.setAdapter(listAdapter);
recyclerView.setLayoutManager(layoutManager);下面是RecyclerView布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/events_list_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</RelativeLayout>最后我的CardView声明
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/event_row_cardview">
...发布于 2016-03-03 01:20:46
我找到了问题的根源。
我的CardView被包装在一个带有属性android:layout_height="match_parent"的LinearLayout中,所以每次呈现它时,它都占据了屏幕的整个高度。
我通过将属性更改为android:layout_height="wrap_content"来修正它
https://stackoverflow.com/questions/35760242
复制相似问题