2

My RecyclerView item layout:

<ImageView
    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"
    android:background="#f2f2f2"
    android:scaleType="centerCrop"
    tools:src="@color/black_alpha_60"
    android:padding="10dp"/>

When I changed ListView to RecyclerView, I found that the padding missing on Android 4.1.2 Device, but it works well on Android 7.1, I tried to set padding in Java code (onBildViewHolder) like this:

        ImageView iv = (ImageView) holder.itemView;
        if (iv.getPaddingLeft() == 0) {
            int p = AppUtils.dp2px(iv.getContext(), 10);
            iv.setPadding(p, p, p, p);
        }

And it still not working. Is there any capability solution to resolve this problem for all android platform devices excepting change padding to margin? Thank you.

PS: RecyclerView version: 25.3.1

Update: The iv.setPadding(p,p,p,p) works when I add android:cropToPadding="true" to the ImageView. But prefer to use android:padding="10" to declare the padding than using setPadding API.

Jamling Li
  • 57
  • 1
  • 8

2 Answers2

1

You should consider using ItemDecoration.

class ItemDecoration extends RecyclerView.ItemDecoration {

    private final int mSpace;

    public ItemDecoration(int space) {
        this.mSpace = space;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        // for left and right spacing
        outRect.left = mSpace;
        outRect.right = mSpace;

        outRect.bottom = mSpace;
        // Add top margin only for the first item to avoid double space between items
        if (parent.getChildAdapterPosition(view) == 0) {
            outRect.top = mSpace;
        }
    }
}

And then set it in your recyclerview.

ItemDecoration itemDecoration = new ItemDecoration(20);
recyclerView.addItemDecoration(itemDecoration);

Check out here for more details..

https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ItemDecoration.html

How to add dividers and spaces between items in RecyclerView?

Ritt
  • 3,181
  • 3
  • 22
  • 51
  • An optional solution, but it must hard coding RecyclerView.ItemDecoration for each page. The divider is same in each page, but the item padding is not always same for each page. – Jamling Li Aug 10 '17 at 09:37
  • Right, you don't have to hardcode it for each page. You can have boolean or something in constructor to specify if space and divider both are needed or just on of them. And this is the recommended way to decorate the recycler views. – Ritt Aug 10 '17 at 10:08
  • Well, but how to control the horizontal padding? – Jamling Li Aug 11 '17 at 02:33
  • you can overrride getItemOffset for horizontal spacing. check out the update. – Ritt Aug 11 '17 at 07:32
  • Also, check out the 2nd link though, it explained very well. – Ritt Aug 11 '17 at 07:34
  • I want to control the item padding, not the divider padding. – Jamling Li Aug 12 '17 at 05:45
  • yes, you getItemOffset() is for item padding. Have you tried it yet? – Ritt Aug 12 '17 at 06:21
  • Not yet, I think it's there is no better solution. I hope the support-recycler library will fix the issue in feature, now I just add if (padding>0) {set padding using java code} – Jamling Li Aug 16 '17 at 07:07
  • It has nothing to do with the support library, itemdecoration works very well, for spacing and dividers, post your code for new implementation which u have tried. – Ritt Aug 16 '17 at 07:12
  • Understand how the itemDecorator works and what happens inside getItemOffset(). It is explained very well in the 2nd link. – Ritt Aug 16 '17 at 07:13
  • I prefer to use android:padding="10dp" without any Java hard codes. – Jamling Li Aug 18 '17 at 09:03
-2

Unfortunately many such issues exist across versions. I personally deal with a bunch of headache involving android 6.0 aka apk23 compared with android 7.0 and lower versions.

You need to check version number in code and based on the if statement, do different behavior. See here to check version num of android in code

Edit: link was wrong sorry it's late here... here is correct one. Retrieving Android API version programmatically

Bqin1
  • 467
  • 1
  • 9
  • 19
  • Absolutely misleading link. The link provided gives the version number of one's own application, while the answer provided says that Android Version is to be checked (though that itself is not the correct answer) – Arun Shankar Aug 10 '17 at 04:51
  • Oh my bad I will fix the link. Although I did answer the question and not sure if a downvote was warranted. He ask how to work across all versions of android, my solution does that. I use it everyday in my code – Bqin1 Aug 10 '17 at 04:53
  • I prefer to set padding with "android:padding" in xml layout. – Jamling Li Aug 10 '17 at 05:25
  • Yes me too, I hate adding more code. But so far I haven't found anyway to deal with it with xml only. Good luck. – Bqin1 Aug 10 '17 at 05:32