0

I am trying to use ItemDecorator to achieve spacing between CardViews inside a RecyclerView. For that I am using as reference this answer but the spacing isn't applied. I Also tried with this answer but no avail.

Here is the XML for the RecyclerView:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             tools:context="com.company.name.MyFragment">

    <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerviewId"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            />
</FrameLayout>

Here is the XML for the CardView:

<?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="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cardView"
        card_view:cardElevation="2dp"
        >

    <android.support.constraint.ConstraintLayout
            android:id="@+id/constraintLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <ImageView
                android:id="@+id/imageView"
                android:layout_width="100dp"
                android:layout_height="98dp"
                card_view:srcCompat="@mipmap/ic_launcher"
                card_view:layout_constraintTop_toTopOf="parent"
                card_view:layout_constraintLeft_toLeftOf="parent"/>
    </android.support.constraint.ConstraintLayout>

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

The code for the item decorators are the ones presented in the consulted answers.

What could be the reason for the spacing not to be applied?

EDIT 1:

Here's the code to assign the item decorator:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    ...

    myRecyclerView.setLayoutManager(new GridLayoutManager(activity, 2));
    myRecyclerView.addItemDecoration(new SpacingGridView(activity, R.dimen.my_spacing), 0);

    ... 
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState)

    MyAdapter adapter = MyAdapter(getData());
    myRecyclerView.setAdapter(adapter);
}
Community
  • 1
  • 1
OneEyeQuestion
  • 742
  • 7
  • 22
  • Can you post the part of your code where you assign the decorator and the adapter? – zon7 Oct 12 '16 at 16:08
  • @zon7 I updated the question – OneEyeQuestion Oct 12 '16 at 16:16
  • I've just tried your code and it works for me. Only difference is I have the adapter created and assigned in the onCreateView, so I have MyAdapter adapter = new MyAdapter(); //I have my own code myRecyclerView.setLayoutManager(new GridLayoutManager(activity, 2)); myRecyclerView.addItemDecoration(new SpacingGridView(activity, R.dimen.my_spacing), 0); Only difference, could be that I have code calculating the space for the decorator – zon7 Oct 12 '16 at 16:34
  • @zon7 Thank you. Which link of the two I mentioned are you using for the item decorator? – OneEyeQuestion Oct 12 '16 at 16:36
  • The first one. I have my own Grid code working, and just added the decorator. As I'm saying, maybe the problem is with the spacing – zon7 Oct 12 '16 at 16:40
  • @zon7 What do you mean with the spacing? The code used for the item decorator? I also tried assigning the adapter in the onCreateView but to no avail. – OneEyeQuestion Oct 12 '16 at 16:47
  • Yes, you are using a constant in new SpacingGridView(activity, R.dimen.my_spacing) You should calculate the value of my_spacing, so it works in any screen resolution. Or at least you should do it – zon7 Oct 12 '16 at 16:52
  • @zon7 The thing is that no matter what value I assign to `my_spacing` (1dp, 10dp, 5dp, etc.) it is never applied :( – OneEyeQuestion Oct 12 '16 at 17:41

0 Answers0