The item position in the RecyclerView is not saved - android

I use the mode adjustResize, RecyclerView and EditText, when the keyboard appears, the window shrinks but the visible elements in the RecyclerView go down.
The structure of the layout looks like this:
<LinearLayout
android:orientation="vertical">
<android.support.v7.widget.RecyclerView />
<LinearLayout
android:orientation="vertical"
android:layout_gravity="bottom"/>
</LinearLayout>
I set setStackFromEnd to true, in LinearManager but it does not help.
Versions of libraries: 25.3.0
How can this unpleasant problem be avoided?

instead of using linear layout on top use Relative layout like following...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/bottom_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/bottom_view"/>
</RelativeLayout>

Related

How to create a SwipeRefreshLayout that only can be pulled outside the RecyclerView?

I want to create a layout that has for example the main layout is a vertical LinearLayout, inside it is a TextView and a wrapped RecyclerView. I want to add a SwipeRefreshLayout that only can be swiped outside the RecyclerView.
Here is the simple XML for this layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:padding="40dp"
tools:context=".fragments.warehouse.WHTabOne">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAlignment="center"
android:textSize="18sp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
Where should I put the SwipeRefreshLayout to make it only works for outside the RecyclerView?
Make SwipeRefreshLayout your root layout and put everything inside it. Example:
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout 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:id="#+id/swipe_refresh_layout"
android:padding="40dp"
tools:context=".fragments.warehouse.WHTabOne">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAlignment="center"
android:textSize="18sp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
What I was asking for is to prevent the SwipeRefreshLayout from starting when swiping on RecyclerView only, and swiping everywhere else activate SwipeRefreshLayout.
But I found that the RecyclerView is by default overrides SwipeRefreshLayout until it reaches the top of the RecyclerView then it start to activate SwipeRefreshLayout. I thought it won't work because my list was too short and could't notice that. Now that I've added more items to the list, it's clear. So basically, adding everything under SwipeRefreshLayout is the right thing by default it will make it in right way.

Relative Layout inside Card View is not filling the parent even after giving match_parent

I have a cardView layout as below. I have used Stetho to inspect layout in chrome. I could see that Linear Layout is filling the screen width. However, relative Layout is not filling the parent.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_view_show_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:clickable="true"
android:elevation="100dp"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/image_layout_show_card"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image_view_show_card"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" />
<View
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
Relative layout fills the parent. Try applying different for the layouts to prove it. Something like this...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#3367bb"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/image_layout_show_card"
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image_view_show_card"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" />
<View
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
You will see that the complete layout becomes black, that means it covers the parent.
If you remove background color of relative layout, you will get to see blue color of linear layout.
If the issue is something else, please ask a new question
The solution is to add this inside of your CardView:
app:cardPreventCornerOverlap="false"

How can i add double ScrollView in XML?

I am currently trying to add a double ScrollView inside my XML.
I am using two different views in my layout - a list and categories.
When a user clicks on a category - the list of videos will become visible and the categories will become invisible.
Currently, the category buttons are scroll-able and when I click a category button, the list of videos does become visible. However, the issue is that the list of videos do not scroll.
Here is my layout code:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/tools"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--this is parent layout where I call ListView-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/parent01"
android:descendantFocusability="beforeDescendants"
android:fitsSystemWindows="true"
android:focusableInTouchMode="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:orientation="vertical"
android:weightSum="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="5dp"
android:divider="#android:color/transparent"
android:dividerHeight="1dp"></ListView>
<ProgressBar
android:id="#+id/nextProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:visibility="gone"
style="#android:style/Widget.DeviceDefault.Light.ProgressBar.Small"/>
</RelativeLayout>
</LinearLayout>
<!--This is the categories layout-->
<ScrollView
android:layout_width="wrap_content"
android:orientation="vertical"
android:id="#+id/ll_buttons"
android:layout_marginTop="60dp"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="200dp">
<Button
android:layout_weight="0.5"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/rec_img"/>
<Button
android:layout_weight="0.5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/rec_img"
android:id="#+id/button2"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="200dp">
<Button
android:layout_weight="0.5"
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/rec_img"/>
<Button
android:layout_weight="0.5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/rec_img"
android:id="#+id/button4"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
for nested scrolling better to use NestedScrollView
NestedScrollView as the name suggests is used when there is a need for a scrolling view inside another scrolling view.
ScrollView vs NestedScrollView
another thing use RecyclerView insted of Listview
RecyclerView vs. ListView
sample layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
// add here all your controlls
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
and dont forgot to set setNestedScrollingEnabled() property in RecyclerView like below code
If this property is set to true the view will be permitted to initiate nested scrolling operations with a compatible parent view in the current hierarchy. If this view does not implement nested scrolling this will have no effect. Disabling nested scrolling while a nested scroll is in progress has the effect of stopping the nested scroll.
mRecyclerView.setNestedScrollingEnabled(false);
use nested scroll views
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.widget.NestedScrollView>

layout_weight is not working in scroll view

I am trying to use this code but its not working when i put layout weight.If i remove layout weight and enter height for image view,it works fine.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10">
<ImageView
android:id="#+id/informative_image_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3.7"
android:scaleType="fitXY"></ImageView>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view_option_menu"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6.3" />
</LinearLayout>
</ScrollView>
Actually you don't need scrollview. Remove it. And if you are using recyclerview inside scrollview then you need to specify height in dp of list to make it work properly.

How can I add horizontal, vertical RecyclerView inside a vertical scroll

I want to add vertical as well as horizontal RecyclerViews inside a vertical scrollview.
Is it possible to have multiple recycler views inside vertcal scrollview
what control I need to use.
Also RecyclerView nested in one ScrollView is not good practice so can anybody tell me whats right way to do it can I add RecyclerView inside another RecyclerView or I need to use horizontal and vertical scrollview only to achieve this.
You can do something like this for getting two RecyclerView nested in one ScrollView which is by the way not recommended.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/tools"
android:focusableInTouchMode="true"
android:padding="8dp"
android:background="#drawable/old_map"
android:layout_height="match_parent">
<ScrollView
android:layout_below="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scrollbars="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="600dp">
<view
android:scrollbarSize="6dp"
android:id="#+id/recent_post"
class="android.support.v7.widget.RecyclerView"
android:layout_width="match_parent"
android:layout_height="400dp"
android:scrollbars="vertical" />
<view
android:scrollbarSize="6dp"
android:id="#+id/recent_post"
class="android.support.v7.widget.RecyclerView"
android:layout_width="match_parent"
android:layout_height="400dp"
android:scrollbars="horizontal" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
I don't namely know what kind of scrolling do you want but it is possible to use both scrolls at the same time:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:id="#+id/HorizontalScrollView02"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- HERE YOUR CODE-->
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>

Categories

Resources