Android: RecyclerView layout problem when list goes off the screen - android

I have an activity where a test (questions with their corresponding possible answers on each fragment) is displayed.
The question is displayed and immediately below the possible answers are displayed with a RecyclerView.
If the question is short and the answers fit on the screen no problem is found, but the problem appears as soon as the question is longer and the RecyclerViews answers goes off the screen.
The issue apparently is RecyclerView's height is not properly calculated and it takes as its height the remaining space between the question bottom and the screen bottom, so if you scroll down you get a cut RecyclerView and only scrolling again inside of it fixes partially the problem.
This is my layout where you can see the RecyclerView:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
</data>
<RelativeLayout
android:id="#+id/llTestMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100"
android:background="#ffffff"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout
android:id="#+id/llBody"
android:layout_weight="80"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dip"
android:baselineAligned="false">
<LinearLayout
android:id="#+id/llTestHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="horizontal"
android:layout_gravity="bottom">
<TextView
android:id="#+id/tvQuestionNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:textColor="#4572ab"
android:textStyle="bold"
android:textSize="20sp"
android:textIsSelectable="false" />
<LinearLayout
android:id="#+id/llZoom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:visibility="gone"
android:orientation="horizontal"
android:layout_gravity="end">
<TextView
android:id="#+id/tvZoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:textIsSelectable="false" />
</LinearLayout>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
<LinearLayout
android:id="#+id/cbAndImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:id="#+id/llTvQuestion"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top|start"
android:orientation="horizontal">
<WebView
android:id="#+id/webViewQuestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:alwaysDrawnWithCache="false" />
</LinearLayout>
<LinearLayout
android:id="#+id/llTvImgInstructions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingStart="10dp"
android:paddingBottom="20dp"
android:orientation="horizontal">
<TextView
android:id="#+id/tvImgInstructions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="false" />
</LinearLayout>
<LinearLayout
android:id="#+id/cbLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:scrollbars="none"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
android:id="#+id/rv" />
</LinearLayout>
<LinearLayout
android:id="#+id/llExplanations"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="20dp"
android:gravity="center"
android:orientation="vertical">
<WebView
android:id="#+id/webViewExplanations"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="70dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:id="#+id/llVerticalPager"
android:layout_weight="20"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:background="#ffffff"/>
</LinearLayout>
<include
android:id="#+id/llFetchingdata"
layout="#layout/testme_fetchingdata" />
</RelativeLayout>
</layout>
And this is how I set the RecyclerView in code:
private void createRecyclerView_TestMode(int pageNumber, TestDTO test)
{
LinearLayoutManager llm = new LinearLayoutManager(context,
LinearLayoutManager.VERTICAL, false);
RecyclerView rv = tBinding.rv;
rv.setLayoutManager(llm);
RVAdapter_Test_QuestionAnswers adapter =
new RVAdapter_Test_QuestionAnswers(pageNumber, test, tBinding, Enum.CallingActivity.Test);
rv.setAdapter(adapter);
}

Related

RecyclerView scrolling very slow with NestedScrollView

I have a RecyclerView with a NestedScrollView and the recycler view scrolling is very very slow. Why is this happening, and how can I fix this?
After googleing and searching in Stack Overflow, i got this solution:
mMyRecyclerView.setNestedScrollingEnabled(false);
But it does not improve the scroll speed in my case, please help.
This is my xml code
<FrameLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="fragment.HomeFrg">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<!-- TODO: Update blank fragment layout -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/ns1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/card_background_new_design"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:cardElevation="5dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/bestdeal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:padding="5dp"
android:paddingEnd="10dp"
android:paddingStart="10dp"
android:text="DEALS"
android:visibility="gone"
android:textColor="#color/colorPrimary"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="#+id/viewall"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:gravity="end"
android:padding="5dp"
android:text="View All"
android:textColor="#color/colorPrimary"
android:textSize="12sp"
android:textStyle="bold" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view_page2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10dp"
android:clipToPadding="false"
android:layout_marginBottom="5dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/activity_main_webview"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="150dp" />
<TextView
android:id="#+id/homefrgMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/nonetwork"
android:textSize="30sp"
android:visibility="visible"
android:textStyle="normal|bold" />
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="42dp"
android:layout_gravity="center"
android:layout_height="42dp"
android:visibility="visible"
android:id="#+id/progressBar_home_frg" />
</FrameLayout>
</FrameLayout>
This is my app on google play store, for your reference.
try to use this when you call your Linear layout manager
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false){
#Override
public boolean canScrollVertically() {
return false;
}
});
I am not sure but try RecyclerView height match_parent to wrap_content

how to fit MaterialCalendarView or apply scrollable in MaterialCalendarView

<LinearLayout 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:layout_alignParentRight="true"
android:orientation="vertical"
android:weightSum="2">
<LinearLayout
android:id="#+id/gview1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.15"
android:orientation="vertical">
<com.prolificinteractive.materialcalendarview.MaterialCalendarView
android:id="#+id/calendarView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:mcv_arrowColor="#color/colorPrimary"
app:mcv_selectionColor="#color/colorPrimary" />
</LinearLayout>
<LinearLayout
android:id="#+id/gview2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/date_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/calendarView"
android:background="#1d925c"
android:gravity="center"
android:padding="10dp"
android:textColor="#ffffff" />
<RelativeLayout
android:id="#+id/rl_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/headertext"
android:layout_margin="10dp"
android:background="#drawable/shadow">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:scrollbarSize="3dp"
android:scrollbarThumbVertical="#drawable/scrollbar"
android:scrollbars="vertical" />
<android.support.v7.widget.AppCompatTextView
android:id="#+id/emptyText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="20dp"
android:gravity="center"
android:text="No Record found"
android:visibility="gone" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
This is my xml using this xml i am displaying top MaterialCalendarView and bottom some textview with Linear layout what happens MaterialCalendarView is not coming in top only half date visible another half not visitable in smaller device so i want fit it in top calendar-view with all date or can we apply scroll on only MaterialCalendarView please suggest me how to achive this .
You can use Scrollview to show scrollable calenderview.
Show below code and change as per your requirement:-
<LinearLayout
android:id="#+id/gview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fillViewport="true">
<com.prolificinteractive.materialcalendarview.MaterialCalendarView
android:id="#+id/calendarView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:mcv_arrowColor="#color/colorPrimary"
app:mcv_selectionColor="#color/colorPrimary" />
</Scrollview>
</LinearLayout>

Items inside scroll view are not showing up on mobile screen

I have a layout file as given below. I have one scroll view inside that one listview and one linear layout. When i install the app in mobile only listview is visible properly but all other layouts are invisible. I have two other textview which are not visible. How to solve this problem?
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.app.Fragments.LFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/l_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#null"
android:scrollbars="none"
android:animationCache="false"
android:scrollingCache="false"
android:smoothScrollbar="true">
</ListView>
<android.support.v7.widget.CardView
android:id="#+id/assmt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorAccent"
android:gravity="center"
android:padding="10dp"
android:text="Assessment"
android:textColor="#color/colorBlack"
android:textSize="16sp"/>
<TextView
android:id="#+id/assmt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="sample"
android:textColor="#color/colorBlack"
android:textSize="16sp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
</LinearLayout>
The Parent View(ScrollView) have a separate scroll option and ListView have a separate scroll so when you try to use scroll listView that scroll option only working this case parent scroll not working.
Please try to use NestedScrollView.
NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.
https://developer.android.com/reference/android/support/v4/widget/NestedScrollView.html
Example : https://inducesmile.com/android-tips/android-recyclerview-inside-nestedscrollview-example/
Put the ScrollView as a parent like this:
<ScrollView 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.app.Fragments.LFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/l_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#null"
android:scrollbars="none"
android:animationCache="false"
android:scrollingCache="false"
android:smoothScrollbar="true">
</ListView>
<android.support.v7.widget.CardView
android:id="#+id/assmt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorAccent"
android:gravity="center"
android:padding="10dp"
android:text="Assessment"
android:textColor="#color/colorBlack"
android:textSize="16sp"/>
<TextView
android:id="#+id/assmt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="sample"
android:textColor="#color/colorBlack"
android:textSize="16sp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
</ScrollView>
Use android:layout_weight property for views inside LinearLayout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="vertical">
<ListView
android:id="#+id/l_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".8"
android:divider="#null"
android:scrollbars="none"
android:animationCache="false"
android:scrollingCache="false"
android:smoothScrollbar="true">
</ListView>
<android.support.v7.widget.CardView
android:id="#+id/assmt"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".2"
android:layout_margin="10dp"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorAccent"
android:gravity="center"
android:padding="10dp"
android:text="Assessment"
android:textSize="16sp"/>
<TextView
android:id="#+id/assmt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="sample"
android:textSize="16sp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>

how to scroll recyclerview scroll the object outside recyclerview

I want to know is it possible to scroll recyclerview and scroll other object without using nested scroll or scrollview?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:gravity="center"
android:text="Text or other oject will list here"
android:id="#+id/info"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/business_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:gravity="center"
android:text="Text or other oject will list here"
android:id="#+id/info"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/business_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Note: set recyclerView.setNestedScrollingEnabled(false); programmatically

Remove Strange Space between RecyclerView Items

I am newbie to android development and learning it to my own. I have a very strange problem of having strange space that comes in between every item of my recycler view. I have the following:
My recycler view is vertical with grid view layout of 2 spans(columns). What I have been trying is:
Code for Layout
<?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:layout_marginLeft="#dimen/size_five"
android:layout_marginRight="#dimen/size_five"
android:layout_marginTop="#dimen/size_fifteen"
android:background="#color/blackColor"
android:clickable="true"
android:focusable="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50">
<ImageView
android:id="#+id/album_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/blackColor"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/lowerline"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="15"
android:background="#color/hotpinkColor"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/size_ten">
<TextView
android:id="#+id/album_title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="left"
android:text="Album Title"
android:textColor="#color/whiteColor"
android:textSize="#dimen/size_eight"
android:textStyle="bold"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="#dimen/size_ten"
android:layout_weight="5"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/downloader"
android:layout_width="#dimen/size_thirty_six"
android:layout_height="#dimen/size_thirty_six"
android:layout_centerInParent="true"
android:src="#drawable/donwloaderimager"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/setteraswall"
android:layout_width="#dimen/size_fourty"
android:layout_height="#dimen/size_fourty"
android:layout_centerInParent="true"
android:src="#drawable/setteras"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Screen Shot
The Black space is showing the extra strange space that I want to remove.
Change:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50">
<ImageView
android:id="#+id/album_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/blackColor"
/>
</LinearLayout>
to
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50">
<ImageView
android:id="#+id/album_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/blackColor"
/>
</LinearLayout>
That is change the height of the second parent linear layout to wrap_content

Categories

Resources