【问题标题】:Items doesn't fill space项目没有填满空间
【发布时间】:2018-04-07 20:58:23
【问题描述】:

我有一个 RecyclerView。当您查看图片时,我遇到了问题,即 RecyclerView 的项目不会填满屏幕的整个空间。 (我的意思是 item_note 和屏幕边缘之间的空间......

这是我的 main_activity.XML:

<android.support.v7.widget.RecyclerView
    android:id="@+id/rvNoteList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical" />

这是我的 item_layout.XM:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:background="@drawable/note_bg">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="9"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="Title"
        android:textStyle="bold"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/tvContent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="Text for Content" />
</LinearLayout>

这是我在 MainActivity.java 中设置布局的方法:

recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
recyclerView.setItemAnimator(new DefaultItemAnimator());

编辑: click to see

编辑 2: click to see

编辑 3:

适配器:

adapter = new FirestoreRecyclerAdapter<Note, NoteViewHolder>(response) {
        @Override
        protected void onBindViewHolder(NoteViewHolder holder, int position, Note model) {
            final Note note = notesList.get(position);

            holder.title.setText(note.getTitle());
            holder.content.setText(note.getContent());
        }

        @Override
        public NoteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_note, parent, false);
            return new NoteViewHolder(view);
        }

        @Override
        public void onError(FirebaseFirestoreException e) {
            Log.e("error", e.getMessage());
        }
    };

编辑 4:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <solid android:color="#CABBBBBB"/>
        <corners android:radius="2dp" />
    </shape>
</item>

<item
    android:left="0dp"
    android:right="0dp"
    android:top="0dp"
    android:bottom="2dp">
    <shape android:shape="rectangle">
        <solid android:color="@android:color/white"/>
        <corners android:radius="2dp" />
    </shape>
</item>
</layer-list>

【问题讨论】:

  • 在您的AdapteronCreateViewHolder() 方法中,您是否在inflate() 调用中传递了ViewGroup 参数?
  • @MikeM。你能看看我的适配器(编辑:3)吗?也许你发现了一个错误。

标签: java android items staggeredgridlayout


【解决方案1】:

如果您不想要上述的间隙,您可能不想使用StaggeredGridLayoutManager,因为它会尝试在某些行的末尾添加间隙以创建锯齿状边缘效果。详情请见https://developer.android.com/reference/android/support/v7/widget/StaggeredGridLayoutManager.html

对于均匀间隔的项目,您应该使用GridLayoutManager。所以,尝试改变

recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));

recyclerView.setLayoutManager(new GridLayoutManager(this, 2));

有关如何使用GridLayoutManager 的更多详细信息,请参阅https://developer.android.com/reference/android/support/v7/widget/GridLayoutManager.html#GridLayoutManager(android.content.Context,%20int)

替代解决方案 1

如果您想坚持使用StaggeredGridLayoutManager,可以尝试将其间隙处理策略设置为GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS,如下所示:

StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
recyclerView.setLayoutManager(layoutManager);

替代解决方案 2

尝试将item_layout.xml 的内容更改为:

<?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"
    android:orientation="vertical"
    android:padding="10dp"
    android:layout_marginTop="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginLeft="5dp"
    android:background="@drawable/note_bg">
        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="Title"
            android:textStyle="bold"
            android:textSize="18sp" />
        <TextView
            android:id="@+id/tvContent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="Text for Content" />
</LinearLayout>

编辑

使用 Alternate Solution 2StaggeredGridLayoutManager,我能够得到以下结果(在您的项目中使用相同的文本):

【讨论】:

  • 效果很好。谢谢。但是有没有办法让它像 StaggeredGridLayout 一样,因为我不希望它那么有序。我希望它像在 StaggeredGridLayout 中一样。提前致谢。
  • 没问题,只是用替代解决方案更新了答案。让我知道这是否达到了您的要求。
  • 谢谢。但也许你有其他解决方案。这对我不起作用。
  • 所以,据我了解,您希望项目填满整个水平空间,但垂直交错?所以是这样的:i.stack.imgur.com/e8qOC.png。不确定是不是这样,但在item_layout.xml 中,尝试将android:layout_width="fill_parent 的所有行更改为android:layout_width="match_parent。而且,这么简单的设计为什么还要嵌套LinearLayout呢?请查看我的更新答案,了解您可以尝试使用的不同布局。
  • 这正是我想要的。和图片中的一样。当我使用这个时:recyclerView.setLayoutManager(new GridLayoutManager(this, 2));您的其他解决方案将不起作用(空间已经存在)。它适用于您的新布局。但是现在我遇到了问题,有些 item_layout 进入了高度。请参阅我的问题