View invisibility when RecyclerView is empty - android

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>

Related

RecyclerView behind cardView is clickable when it should not be

I have a cardview, behind which there is a recyclerview of "Suggested Contacts", much like the default dialer in android. The CardView is placed to the bottom of the screen, and occupies about 70% of the screen, while the RecyclerView extends its height to match_parent. When the RecyclerView is scrolled, the CardView disappears and reveals the whole list.
The user can click on one of the RecyclerView's items to make a call directly. The issue is that items behind the CardView, which are not visible to the user, are also clickable.
How can I have the RecyclerView items which are visible only as clickable? (i.e. while the cardView is visible)
xml code:
<ScrollView
android:id="#+id/sv_suggested_contacts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="expandSuggestions">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_suggested_contacts"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
<androidx.cardview.widget.CardView
android:id="#+id/dialer_card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
[...]
I'm assuming your requirement is that the cardview should block all clicks, so that items below don't take the click right.
If that is the case and you are sure cardview is on top of the recyclerview you simply need to set an onClickListener on your cardview and do nothing inside it. Basically an empty click.
Or you can in xml for cardview add
android:onClick="nullClick"
and in the activity create the empty function
public void nullClick(View view) {
}
whichever way you like.
you can hide (setVisibility(View.GONE) item behind recycler, or set them not clickable

Move layout up when layout above shrinks in height

Hi I have a fragment with two RecyclerViews in it, one above the other.
The first is a list of items for the user to take an action on and the second is where the items will be populated once the action is taken. So when an item is removed from the top list it is added to the bottom list.
The issue I am having is when I remove an item from the top RecyclerView all the remaining items in the top RecyclerView move up to fill in the space left by the removed item, but this leaves a gap between the top and bottom RecyclerViews.
How can I move the bottom recyclerview up to fill in the gap created once an item is removed from the top RecyclerView
Here is my layout xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_alignParentTop="true"
android:id="#+id/pending_tasks"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:layout_below="#id/pending_tasks"
android:id="#+id/completed_tasks"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
I have tried calling invalidate() on the bottom RecyclerView but that has not worked. Any help would be appreciated, thanks!
It's impossible to update layout size once it's been displayed on the screen. If you modify the content of your RecyclerView you can't just simply refresh the layout to shrink it.
I would suggest using single RecyclerView and storing lower bound of the first list in some variable. Then simply update it accordingly to modifications in your first list.
Another solution which may help you:
Change Relative layout width and height dynamically

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.

Recyclerview binds all views at the same time

I have a vertical recyclerview (with a GridLayoutManager) inside another recyclerview (with LinearLayoutManager). The problem I am facing right now is that, the inner recyclerview (with GridLayoutManager) binds all of it's items at the same time, even the views that are not on the screen at the moment (onBindViewHolder() gets called for all of its items).
To give you more information, in my layout file, I put height of my recycler view as wrap_content.
I think the problem is, since there are 2 nested vertically recyclerviews, when the parent RV wants to measure its children and the children is another RV, in onMeasure() it computes the size needed for the entire RV, not just the portion that it wants to bind on the screen.
Any idea how to solve this?
Here is the layout file for my outer recyclerview:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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/component_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
And here is the code for my inner recyclerview:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/gutter"
android:paddingBottom="#dimen/gutter">
<TextView
android:id="#+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/gutter"
android:textSize="30sp"
android:textColor="#android:color/white"
android:fontFamily="sans-serif-thin"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/my_slider"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
P.S.: I'm using this adapter delegate for my outer recyclerview:
https://github.com/sockeqwe/AdapterDelegates
I think nested recyclerviews are a very bad idea. When i try to scroll, which recyclerview has to respond the the scolling, the parrent or child.
That is why I think you are looking for the ExpandableListView? That's limited to only two levels of listings, but that sounds like it would work for your needs). It also solves the soling issue.
It would look something like this:
EDIT: even nested ExpandableListViews are possible:
EDIT: check this lib for horizontal scroling
This is a known bug.
You should not put a RecyclerView inside another RecyclerView because RecyclerView gives its children infinite space. Hence the inner RecyclerView keeps measuring till the dataset is exhausted. Try setting setAutoMeasureEnabled(false) to false on layout manager or you can solve this problem by using a wrapper adapter instead of inner recycler view.
The first thing you need to know is that, when you nest scrolling layouts, the inner ones will get infinity allowed height, effectively making them wrap_content. There is in fact a relatively easy way to fix this problem.
Say I had two nested RecyclerViews such as these, in this case vertically oriented.
<RecyclerView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical>
<View .../>
<!-- other stuff -->
<RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"/>
</RecyclerView>
The inner recyclerView here will bind all of it's children immediately every time because, from it's position, your screen will have infinite height.
The solution is to set the height of your inner recyclerview to some static value, not wrap_content or match parent, as either of those will simply fill up the outer recyclerview with one view that will all be bound at once due to it's large height. If you make the height of the inner recyclerview the same as the display's height, you should see your problem go away.
Here is an implementation that will not bind all children at once:
<RecyclerView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical>
<View .../>
<!-- other stuff -->
<RecyclerView
android:layout_width="match_parent"
android:layout_height="#dimen/screen_height"
android:orientation="vertical"/>
</RecyclerView>
Note the layout_height of the inner RecyclerView is now a fixed value pulled from the dimensions file. You yourself will have to come up with a reasonable value to put there.
Side Note: In order to make all of this work and for scrolling to work properly, you may have to play around with the parameter: NestedScrollingEnabled in your RecyclerViews - there are several known bugs relating to this that you may need to work around.
i.e.: innerRecyclerView.setHasFixedSize(true); and innerRecyclerView.setNestedScrollingEnabled(false).
so what happens here when you place a scrollview(no fixed size because of wrap content) inside another scrollview(again no fixed size because of wrap content),both nested scroll view fails to render.
So there is two solutions--
1- Either you will have to think of alternative solution for nested scrollviews
2- You can give outside recyclerview cell fixed height so that inside recycler view can get some fixed layout to render itself.
I could solve my issue by using only one Recyclerview, where it has a grid layout, and based on the component items i'm adding into it, i change the spancount for that. Basically instead of adding the inner recyclerview, i add the items that were supposed to go to the inner recyclerview, to the outer recyclerview.

ScrollView not scrolling which has ListView inside (listview is scrolling)

I have a ListView inside ScrollView, the ListView works fine (it scrolls) but the ScrollView is not scrolling
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</ScrollView>
</LinearLayout>
I want the full page to scroll (The textview should also scroll)
TextView
ListView
item1
item2
.
.
The full page must scroll along with the textview!
This will never work because your listView is effectively a scrollView, so you have a scrollView within a scrollView. Is the textView of fixed size or can it be a large amount of text? I would consider some redesign here as this is not a very good way to design a UI.
If the textView is big which means you only see a small bit of the list then you should be able to scroll by touching the textView only, but i would make the scrollView the Parent and remove the first LinearLayout, it is not needed
Instead of ListView switch to RecyclerView and inside your MainActivity.java in onCreate() do this recyclerView.setNestedScrollingEnabled(false);
You should never use a ScrollView with a ListView, because ListView takes care of its own vertical scrolling. Most importantly, doing this defeats all of the important optimizations in ListView for dealing with large lists, since it effectively forces the ListView to display its entire list of items to fill up the infinite container supplied by ScrollView.
The TextView class also takes care of its own scrolling, so does not require a ScrollView, but using the two together is possible to achieve the effect of a text view within a larger container.

Categories

Resources