Anton111111
Anton111111

Reputation: 686

How to add space between items only on scrolling

I have horizontal RecyclerView that shown on full screen. This my activity layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</RelativeLayout>

And ViewHolder show on fullscreen too.

Item layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true">

    <ImageView
        android:id="@+id/item_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</FrameLayout>

In result i have one item of RecyclerView on screen. It's what i need. But i don't understand how add spacing between items on scroll (how on all image gallery apps). But i need do not see this space on scroll end.

How i init RecyclerView:

RecyclerView photoList = findViewById(R.id.photos_list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false);
        photoList.setLayoutManager(linearLayoutManager);


        PagerSnapHelper pagerSnapHelper = new PagerSnapHelper();
        pagerSnapHelper.attachToRecyclerView(photoList);

        PhotoListAdapter photoListAdapter = new PhotoListAdapter(this, photoResults);
        photoList.setAdapter(photoListAdapter);

On screenshot i show space what i want get:

enter image description here

Upvotes: 0

Views: 327

Answers (1)

Rainmaker
Rainmaker

Reputation: 11100

To get this space you should use DividerItemDecoration. You can set width and color of it. For example,

drawable:

 <shape xmlns:android="http://schemas.android.com/apk/res/android"
           android:shape="rectangle">
        <size
            android:width="1dp"
            android:height="1dp" />
        <solid android:color="@color/primary" />
    </shape>

And then set it when recycler is not idle like so

DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getContext(),
                    LinearLayoutManager.VERTICAL);//or HORIZONTAL
dividerItemDecoration.setDrawable(getContext().getResources().getDrawable(R.drawable.sk_line_divider));
recyclerView.addItemDecoration(dividerItemDecoration);

or remove like so , when recycler is idle

recyclerView.removeItemDecoration(dividerItemDecoration);

To detect if the recycler is scrolling, implement RecyclerView.OnScrollListener docs

Upvotes: 1

Related Questions