Add RecyclerView body - android

Can I use RecyclerView as a parent element for other views declared in XML?
<android.support.v7.widget.RecyclerView ... >
<TextView ... />
<ImageView ... />
</android.support.v7.widget.RecyclerView>
Something like this. I need this in order to get my collapsing toolbar working with main content on my activity.

You have to use a RecyclerView.Adapter with RecyclerView.
But items in RecyclerView.Adapter can have different viewType just like ListView.
With this feature you can achieve something like HeaderView in RecyclerView and FooterView in RecyclerView, and make these views scroll with your items.

No, You can't add any child view inside Recycler View. See the example https://github.com/javatechig/Android-RecyclerView-Example.

Create separate .xml file (e.g. single_recycler_view_row.xml)
and use it to build layout for single RecyclerView item.
Then you need to create an adapter and pass single_recycler_view_row.xml as a parameter.
To make sure that your RecyclerView works well with CollapsingToolbarLayout add
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
attribute to RecyclerView.

Related

2 Recycler Views Within a Linear Layout

I'm having issues with the UI not being properly bound with the recycler view or at least not what I intend to happen. The following image is the two recycler views beside one another. Everything looks fine, but when I run the app, the children within the recycler views extend off-screen. The second image shows that.
If you are using one adapter, you can basically use GridLayoutManager for that. So, you can have 2 columns on your recycler view.
To do that with Kotlin code, just add below functionality to your recycler view reference in your fragment or activity.
recyclerView.apply {
layoutManager = GridLayoutManager(this, 2)
}
Also, there is another way to do that in XML. You can use, app:layoutManager and app:spanCount in your recycler view.
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="2" />

Recyclerview inside NestedScrollView - Scroll to bottom

I have a RecyclerView inside a NestedScrollView:
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="#dimen/_100sdp">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view_chat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="#dimen/_100sdp"
android:layout_marginLeft="#dimen/_30sdp"
android:layout_marginRight="#dimen/_30sdp"
android:background="#color/bright_grey"
></android.support.v7.widget.RecyclerView>
</android.support.v4.widget.NestedScrollView>
My RecyclerView is being filled with items in onCreate()
On a device you would see the first item of the RecyclerView on the very top und would have to scroll down the NestedScrollView in order to see the last item.
Since my items are chat message sorted by the time sent I need the NestedScrollView to be scrolled all the way down so users would see the latest chat message first without having to scroll in the first place.
Any ideas on this?
Given that your RecyclerView is the only child of your NestedScrollView, you would be better off removing the NestedScrollView altogether, and instead applying the fixed height to the RecyclerView. Something like this:
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view_chat"
android:layout_width="match_parent"
android:layout_height="#dimen/_100sdp"
android:layout_marginLeft="#dimen/_30sdp"
android:layout_marginRight="#dimen/_30sdp"
android:background="#color/bright_grey" />
Doing this allows you to have the RecyclerView itself manage scrolling, rather than the parent scroll view. And that allows you to leverage a property of LinearLayoutManager to achieve what you want.
Reverse layout -- setting this will "invert" your list; the first item in your adapter will appear at the bottom of the list, and the default scroll position of the RecyclerView will be to scroll all the way to the bottom.
https://developer.android.com/reference/android/support/v7/widget/LinearLayoutManager.html#setReverseLayout(boolean)
LinearLayoutManager lm = new LinearLayoutManager(this);
lm.setReverseLayout(true);
If you have the same issue and want to keep the NestedScrollView.
It will work like this.
Handler(Looper.getMainLooper()).postDelayed({
binding.nestedScrollView.smoothScrollTo(
0,
binding.recyclerview.measuredHeight,
500
)
// binding.nestedScrollView.scrollTo(0, binding.recyclerview.measuredHeight)
// binding.nestedScrollView.smoothScrollTo(0, binding.recyclerview.measuredHeight)
}, 50L)
For me, it didn't work without delay.

TableLayout with RecyclerView

There is only two layout manager for RecyclerView: LinearLayoutManager and GridLayoutManager. Is there a way to use TableLayout as layout manager with RecyclerView? Any suggestion?
EDIT: I wonder if there is any solution without writing a layout manager from zero. TableLayout behaviour is enough for me, just want to add recycle feature for performance issues on handling large amount fo data.
And creating a table looking listview is not solve my problem I think, because my table is very dynamic, I don't know even column names, customer deciding all details of the table. So cells would be custom, column widths needs to be auto resizing depend on content length. I don't think listview can handle that.
Thanks in advance for helps.
There are many open source libraries and codes available to find out how it can be developed.
https://github.com/evrencoskun/TableView
https://github.com/HYY-yu/TableRecyclerView
https://github.com/Cleveroad/AdaptiveTableLayout
https://github.com/celerysoft/TableFixHeaders
Have a look at this library. Seems it's exactly what you need. It has a recycling system as well.
I tried this for making a table like structure, and it worked.
For your RecyclerView's adapter implementation,
create a Horizontalscrollview layout, and place all your elements inside it, in your adapter layout xml for each row.
In the main layout xml where you are adding the recycler view, add a HorizontalScrollView as its parent.
Now, add a TableLayout to the HorizontalScrollView
I am adding below code for adapter layout xml:
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/horizontalScroll"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--Any custom layout here as per your need-->
</HorizontalScrollView>
I am adding below code for main layout xml:
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<HorizontalScrollView>
<android.support.v7.widget.RecyclerView
android:id="#+id/empView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="select"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</HorizontalScrollView>
</TableLayout>
This will ensure that you are able to have a table like structure to scroll both horizontally and vertically using recyclerview.
The HorizontalscrollView enables you to horizontally scroll the entire list inside the TableLayout. The RecyclerView enables you to vertically scroll the list.
Hope this works for you.
Create a custom row with linearlayout and than attach that row to recyclerView Adapter this is only the solution for making a table like Structure in RecyclerView.

Two List View inside scrollview

I want to create multiple list view inside ScrollView in Android. I have created the two list view inside ScrollView. In first List View each row contains a single text upto 5 rows will be presented. Whereas, in second list view each row will contains multiple paragraph text, ie, text very long . In my case I am unable to scroll the second list to view fully.
Is any other way available to handle this scenario ?
Its Work in My RecyclerView try this:
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</android.support.v4.widget.NestedScrollView>
You need to disable nested scrolling programatically. It doesn't seem to work correctly if done in xml.
recyclerView.setNestedScrollingEnabled(false);
Don't use the listview inside the scroll view, the listview is already scrollable . Using a ListView to make it not scroll is extremely expensive and goes against the whole purpose of ListView. You should NOT do this. Just use a LinearLayout instead.

View invisibility when RecyclerView is empty

I am using a LinearLayout with some views inside. The second last is a RecyclerView, and the last one is an <include> tag linked with a RelativeLayout. I want the last view to be visible permanently, as I want to use it to add items to the RecyclerView.
My problem is that the <include> view below the RecyclerView disappears whenever the adapter is empty. Is there any way to avoid this behaviour?
EDIT
There is a constraint in the way I want the RecyclerView and the "add" View to work together. I would like that it feels as the "add" view is always the last item in the RecyclerView, as it happens in Google Keep lists.
Example Google Keep list example (I cannot add images yet)
You could try to replace your LinearLayout with a RelativeLayout; then you anchor the bottom view to the bottom of the screen, and the RecyclerView would be set above that view. Something like this (just a skeleton):
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent>
<-! other views here -->
<include layout="#layout/something"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#bottomView" />
<RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/some_view_above_recyclerview"
android:layout_above="#+id/bottomView" />
</RelativeLayout>

Categories

Resources