1

I have a 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. (I mean the space between the item_note and the screen edge...

Here is my 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:

<?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:

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

EDIT: click to see

EDIT 2: click to see

EDIT 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:

<?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>
Cœur
  • 37,241
  • 25
  • 195
  • 267
marcelo
  • 19
  • 3
  • In your `Adapter`'s `onCreateViewHolder()` method, are you passing the `ViewGroup` parameter in the `inflate()` call? – Mike M. Apr 07 '18 at 22:17
  • @MikeM. Can you have a look at my Adapter(EDIT:3)? Maybe you find an Error. – marcelo Apr 12 '18 at 13:54

1 Answers1

1

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. See https://developer.android.com/reference/android/support/v7/widget/StaggeredGridLayoutManager.html for more details.

For evenly spaced items, you should use 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.

Alternate Solution 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 layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
recyclerView.setLayoutManager(layoutManager);

Alternate Solution 2

Try changing the contents of your item_layout.xml to:

<?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):

enter image description here

enter image description here

Cvarier
  • 161
  • 13
  • That works great. Thanks. But is there a way to make it like the StaggeredGridLayout, because I don't want it so orderly. I want it like in StaggeredGridLayout. Thanks in advance. – marcelo Apr 08 '18 at 21:35
  • No problem, just updated the answer with an alternate solution. Let me know if this achieves what you want. – Cvarier Apr 08 '18 at 22:05
  • Thanks. But maybe you have an other solution. This doesn't work for me. – marcelo Apr 09 '18 at 12:59
  • So, as I understand it, you want the items to fill the entire horizontal space, but be staggered vertically? So something like this: https://i.stack.imgur.com/e8qOC.png. Not sure if this is it, but in `item_layout.xml`, try changing all the lines that are `android:layout_width="fill_parent` to `android:layout_width="match_parent`. Moreover, why do you have a nested `LinearLayout` for such a simple design? Please see my updated answer for a different layout you could try using. – Cvarier Apr 09 '18 at 17:55
  • That's exactly that what I want. The same thing as in the picture. When I use this: `recyclerView.setLayoutManager(new GridLayoutManager(this, 2));`Your other solutions wont work(the space is already there). it works with your new layout. But now I have the problem, that some item_layout gets into the height..See in my question – marcelo Apr 09 '18 at 20:03
  • I see that that the top-right item has a lot of extra space if that's what you mean. What happens if you use my new layout together with `recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));`? – Cvarier Apr 09 '18 at 20:29
  • Yes, exactly that's my Problem. ok..Look at the edit 2 in my question to see the picture. So now the height is fixed but the layout won't fill the space anymore. – marcelo Apr 10 '18 at 11:38
  • To me, it looks like you've set `android:width="wrap_content"` instead of `android:width="match_parent"` for the `LinearLayout` in `item_layout.xml`. Please see my edit for screenshots of what I'm seeing. Make sure that your`item_layout.xml` is exactly the same as what I have in my answer, and that you're using `StaggeredGridLayoutManager`. If you're still having this problem, please update your question with the contents of your adapter. – Cvarier Apr 11 '18 at 05:33
  • I tried it with `recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));` and `StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS); recyclerView.setLayoutManager(layoutManager);`.And i copied the layout. But nothing changed. So here is my adapter. I hope it helps:See edit. – marcelo Apr 11 '18 at 20:05
  • Took a look at your adapter, but couldn't find anything problematic in there. Is the `Note` class something you defined, or is it from some library? If it's something you created, can you post its definition along with the definition of `NoteViewHolder`? Also can you post the contents of the drawable `note_bg`? – Cvarier Apr 11 '18 at 20:27
  • The note_bg looks like the Edit 4 in my Question. In the noteclass there is nothing who can change the layout parameters. The first comment below my Question...I don't know what he wants to ask me? maybe he knows the problem. In NoteViewHolder is nothing special too. – marcelo Apr 12 '18 at 13:51
  • He's asking you if in `onCreateViewHolder()` you are passing in `parent` as an argument to `parent.getContext()).inflate()`, but you are. I don't believe the issue is in your adapter, I'll look into it more carefully. – Cvarier Apr 12 '18 at 17:44
  • It looks like your item information is coming from a Firebase database, since you're using a `FirestoreRecyclerAdapter`. I can't tell exactly what the problem is from the information you've provided, since it could be anything from an API bug in the `FirestoreRecyclerAdapter` implementation to a compatibility issue with `StaggeredGridLayoutManager` on the API you're testing on. Do you absolutely need to use Firebase in your project? Also, what Android API are you testing on? – Cvarier Apr 12 '18 at 18:10
  • Yes Firebase is needed in Project. Android api: 26 – marcelo Apr 21 '18 at 12:43
  • Just tested on API 26, and it seems to be working with the sample project I've built. Unfortunately, I'm not able to pinpoint the issue with the information you've given me. Would you be willing to post the source code for your project, possibly as a GitHub repo, so that I can help you further? – Cvarier Apr 22 '18 at 20:44