recyclerview items outside view bound invisible - android

I have the following situation: I have a vertical RecyclerView with padding and the parent layouts clipChildren and clipToPadding set to false like so:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:clipChildren="false"
android:clipToPadding="false">
<android.support.v7.widget.RecyclerView
android:layout_margin="#dimen/padding_medium"
android:id="#+id/recyclerview_pager"
android:layout_width="match_parent"
android:layout_height="#dimen/holder_height_medium"/>
</LinearLayout>
Moreover, I attach a rather unspectacular LinearLayoutManager like so:
MyLayoutManager layoutManager
= new MyLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerViewPager.setLayoutManager(layoutManager);
The view holder in the adapter is very simple too:
<?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:paddingTop="#dimen/layout_padding_small"
android:paddingRight="#dimen/layout_padding_small"
android:paddingLeft="#dimen/layout_padding_small">
.....
</LinearLayout>
Now I get the following behavior: When I am scrolled to one view, the item that is supposed to be visible in the padding area outside the recycler view is not yet rendered. Only when I start scrolling slightly, the item appears is rendered and displayed in the padding area as desired:
How can I tell the recyclerview to layout the neighboring items before they are actually about to come into view? Using an item prefetch configuration on the LayoutManager did not help. I have searched the methods of the LayoutManager and did not find a good method to override exteding the.
Thanks in advance for any help, let me know if anything is still unclear.

You need to set clipToPadding=false to RecyclerView not to parent LinearLayout and also you can remove clipChildren
like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:layout_margin="#dimen/padding_medium"
android:id="#+id/recyclerview_pager"
android:layout_width="match_parent"
android:clipToPadding="false"
android:paddingEnd="40dp"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:paddingStart="40dp"
android:layout_height="#dimen/holder_height_medium"/>
</LinearLayout>
UPDATE
I've added padding to recyclerview to make visible next item of position.

Related

Show a horizontally large background in back of Recyclerview

I have a background image with a long width which I want to be in back of a Recyclerview with 8 item in it(Recyclerview). This is possible only with a horizontal-scrollview which I coded below but Is there any way to not use horizontal scrollview and have a same result?
<HorizontalScrollView
android:id="#+id/HSV"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/back"
android:paddingLeft="2.5dp"
android:paddingRight="2.5dp"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</HorizontalScrollView>
with the above code I have the desired output, but as I remove the HorizontalScrollView The image is compressing in width and loose its shape I want the image scroll as I scroll the recyclerview
That is The picture I want:
![in Background is the aforementioned image and this eight Item are in recycler view and I want as the items finishing the background image is finishing too.
]1
Design like this in layout.And pass layout of card in image to adapter in Android.Set background to Constraint layout ore Recyclerview. When you have Recyclerview no need to add Horizontal Scrollview. Just pass Layout manager as Horizontal
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#color/secondaryColor"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>idget.ConstraintLayout>
I wonder if I have got your query right, so you want your RecyclerView to be scrolling horizontally. I would suggest to use the LinearLayoutManager for the same, so that you can pass the Scrolling constant to Horizontal in the arguments of LinearLayoutManager.
To read about it, please go ahead and read it LinearLayoutManager
And when you get it, just do it like this in you Java Class.
RecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));
I have this youtube link for you too, if you don't get it what you want. Here you will get the full demo of how to do it, and achieve it. Horizontal RecyclerView
I hope that will answer your query. Thanks :)

Make over scroll glow be outside the padding

I have the following:
<android.support.v7.widget.RecyclerView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
I add this for a cleaner UI:
android:paddingEnd="16dp"
android:paddingStart="16dp"
However, now the scroll bar is inside the padding and looks funky. So I add:
android:scrollbarStyle="outsideInset"
The scroll bar is now outside the padding, however, the over scroll glow is still inside the padding.
How do I get the over scroll glow to stretch across the padding?
EDIT:
Here's the full layout code that encounters the same issue as above.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
What you need to do is to apply android:clipToPadding="false" to RecyclerView. Consider this xml:
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="40dp"
android:paddingBottom="40dp"
android:clipToPadding="false"/>
Here's the result:
Your best bet would be to put padding inside RecyclerView's item layout and not on RecyclerView itself.
However, you could just use margins and the glow should stay inside the RecyclerView.
While the answer given by #azizbekian works, it's also possible to set clipToPadding programmatically.
In Kotlin: recyclerView.clipToPadding = false
In java: recyclerView.setClipToPadding(false)
My personal favorit is to extend RecyclerView and set a LayoutManager, clipToPadding and such in the init function (constructor).

How to add extra space to RecyclerView with clipToPadding=false to prevent premature recycling

I have the following layout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentBottom="true"
android:layout_below="#+id/searchView"
android:clipChildren="false"
android:clipToPadding="false"
android:scrollbars="vertical"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
<SearchView
android:id="#+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
which is a simple RecyclerView below a SearchView. I want the RecyclerView's views to scroll behind the SearchView, so clipChildren and clipToPadding are set to false.
My problem is that RecyclerView is (rightly) recycling its adapter's views when they are still visible, just because they are out of the RecyclerView's area. Is there a way to add an extra space to the RecyclerView within which views are not recycled?
EDIT, a gif showing the problem:
This method should help you setItemViewCacheSize(int size). It sets the number of offscreen views to retain before adding them to the potentially shared recycled view pool. More about it you can find here.

can i create a nestedScroll Layout like this?

I think it can be achieved by NestedScrollingChild NestedScrollingParent.
But I can't really understand them.Who can help me out!
Product manager insists on the design.
The ScrollView contains a LinearLayout ,a "TabLayout" and a ViewPager.
The ViewPager contains 2 fragment contains RecyclerView or just only 2 RecyclerView.
When the ScrollView scroll to Bottom , the RecyclerView can scroll to Bottom.
When the RecyclerView scroll to Top , the ScrollView can scroll to Top.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="1000dp" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="60dp"></TableLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v4.view.ViewPager>
</LinearLayout>
</ScrollView>
Just change your ScrollView to android.support.v4.widget.NestedScrollView
Post your xml if that doesn't work
ِYou can do that, but it's not good at all to use this type of layout
First you need add "wrap_content" abilty ViewPager and RecyclerView's
ViewPager and RecyclerView
Now you can use scrollview easily, put all item in it
If you want some effect or something when user scrolling or some item get visible in scrolling you can get position of item then make listener on
"OnScrollChangedListener" of scrollview

Horizontal RecyclerView inside vertical ScrollView

So I have a horizontal RecyclerView inside a vertical ScrollView. Everything inside my layout is displayed fine and it all scrolls in the directions I want and it does it smoothly.
The only problem I have, is that the RecyclerView is below some other content in the ScrollView and when the RecyclerView is partially visible, it will line the bottom of the RecyclerView with the bottom of the screen on start-up. This means that the content above the RecyclerView is pushed off the screen.
Does anyone know why this happens, and how I can fix it?
Here is a simple layout that does what I just described. You don't even need to populate the RecyclerView, it will still do it.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#fff"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#000"/>
</LinearLayout>
</ScrollView>
Turns out this issue was reported to Google here Issue - 81854
According to Google it is working as intended. The problem is the fact that RecyclerView has focusableInTouchMode set to true. To fix the problem I set focusableInTouchMode and focusable to true on the top-most view of the ScrollView.
Below is the fix for the code sample I provided in the original question:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#fff"
android:focusableInTouchMode="true"
android:focusable="true"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#000"/>
</LinearLayout>
</ScrollView>
I have been looking for a solution for a long time. In the end, I decided by accident. In your activity, find the ScrollView and add a touch listener for it. Everything will work as it should
ScrollView scroll = findViewById(R.id.scroll);
scroll.setOnTouchListener((view, motionEvent) -> {
return false;
});

Categories

Resources