简体   繁体   English

项目没有填满空间

[英]Items doesn't fill space

I have a RecyclerView.我有一个 RecyclerView。 When you look at the picture, I have the Problem, that the Items of the RecyclerView won't fill the full space of the screen.当您查看图片时,我遇到了问题,即 RecyclerView 的项目不会填满屏幕的整个空间。 (I mean the space between the item_note and the screen edge... (我的意思是 item_note 和屏幕边缘之间的空间......

Here is my main_activity.XML:这是我的 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" />

Here is my item_layout.XM:这是我的 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>

Here is how I set the Layout in MainActivity.java:这是我在 MainActivity.java 中设置布局的方法:

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

EDIT: click to see编辑:点击查看

EDIT 2: click to see编辑2:点击查看

EDIT 3:编辑 3:

Adapter:适配器:

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

EDIT 4:编辑 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>

If you don't want gaps as you've described, you probably don't want to be using StaggeredGridLayoutManager , since it will try to add gaps at the end of some rows to create a jagged edge effect.如果你不想要你所描述的间隙,你可能不想使用StaggeredGridLayoutManager ,因为它会尝试在某些行的末尾添加间隙以创建锯齿状边缘效果。 See https://developer.android.com/reference/android/support/v7/widget/StaggeredGridLayoutManager.html for more details.有关更多详细信息,请参阅https://developer.android.com/reference/android/support/v7/widget/StaggeredGridLayoutManager.html

For evenly spaced items, you should use GridLayoutManager .对于均匀分布的项目,您应该使用GridLayoutManager So, try changing所以,尝试改变

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

To

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

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

Alternate Solution 1替代解决方案 1

If you want to stick with the StaggeredGridLayoutManager , you can try setting its gap handling strategy to GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS like so:如果您想坚持使用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);

Alternate Solution 2替代解决方案 2

Try changing the contents of your item_layout.xml to:尝试将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>

Edit编辑

Using Alternate Solution 2 and StaggeredGridLayoutManager , I was able to get the following result (using the same text in your items):使用Alternate Solution 2StaggeredGridLayoutManager ,我能够得到以下结果(在您的项目中使用相同的文本):

在此处输入图片说明

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM