ScrollView very slow while Scrolling - java

I have ScrollView which looks like the following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.onecode.humam.kh.Main2Activity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp"
android:fillViewport="true"
android:id="#+id/sc">
<RelativeLayout
android:id="#+id/partner_detail_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<view
android:id="#+id/recycler_view"
android:layout_below="#id/recycler_view"
android:layout_width="fill_parent"
android:layout_height="match_parent"
class="android.support.v7.widget.RecyclerView"/>
<ImageButton
android:id="#+id/button_next"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:minHeight="25dp"
android:minWidth="70dp"
android:src="#android:drawable/ic_media_next"
android:onClick="x"
android:visibility="invisible"/>
</RelativeLayout>
</ScrollView>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pb"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:indeterminate="false" />
</RelativeLayout>
It's very slow when scrolling, does anyone know a solution on how to solve it?

What causes the lag in scrolling is that you have nested scrollable views.
You cannot have RecyclerView inside a ScrollView.
[EDIT]
If you really need nested scrollable views that, consider using NestedSrcollView

Try not to use RecyclerView inside ScrollView

Related

I'm using a framelayout and inside it I want a listview and a floating action button, but the listview doesn't extend

Why is this not working?
It's a problem with the listview.
I've tried in many ways and ending up with the button not showing
<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"
tools:context=".fragments.notes.NotesFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="#drawable/ic_baseline_add_24" />
</LinearLayout>
</ScrollView>
</FrameLayout>```
The first problem is that you cannot put a ListView inside a ScrollView, ListView itself is scrollable and the second problem it's with the margin if you use a bottom menu, I also encountered this problem. Try this one
<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"
tools:context=".fragments.notes.NotesFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom|end"
android:layout_marginRight="20dp"
android:layout_marginBottom="70dp"
android:src="#drawable/ic_baseline_add_24" />
</RelativeLayout>
</FrameLayout>```

How to animate an image above a listview when scrolling?

I have an image above my listview that is currently doing nothing.But I want my image to be animated when the user scrolls in the listview just like
this.
This is an example of an old library called StikkyHeader ,but it doesn't seem to work now.
Is there any other libraries for the same reason or even a way I can do it without libraries?
Try this one
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:minHeight="200dp"
>
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/collapsingToolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:minHeight="150dp"
app:expandedTitleMarginBottom="25dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
>
<ImageView
android:layout_width="match_parent"
android:src="#drawable/magic_bg"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.5"
android:layout_height="match_parent"/>
<TextView
android:layout_width="match_parent"
android:text="#KeepMakingMagic"
android:textSize="25dp"
android:gravity="center"
android:textColor="#color/white"
android:textStyle="bold"
app:layout_collapseMode="parallax"
android:layout_marginBottom="50dp"
android:layout_gravity="bottom"
app:layout_collapseParallaxMultiplier="-0.1"
android:layout_height="wrap_content"/>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<!-- You can have list view here Instead of scrollview But you need to make sure layout behaviour is same-->
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorAccent"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:background="#color/colorAccent"
android:layout_height="1000dp">
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Demo :
Don't forget to play with things

When banner ad not loaded a white space appears in its place

My app has a banner ad at the bottom.It takes some time load.While it loads a white space appears in its place as shown below:
How do i slove this problem.Should improve the loading speed of the banner ad or do something with my xml layout.
Here is the xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.android.gametalks.MainActivity"
>
<LinearLayout
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/no_internet_view"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Internet Connection!"/>
<Button
android:id="#+id/retry_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Retry"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_above="#id/adView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</LinearLayout>
</LinearLayout>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
You can remove android:layout_above="#id/adView from list container and add to your ListView paddingBottom = height_of_your_banner (or about it) and add android:clipToPadding="false". Now your listview will fill whole screen and if you scroll to bottom - you will be able to see al items without overlap from banner. Also you can remove odd containers from ListView so instead of:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_above="#id/adView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</LinearLayout>
</LinearLayout>
Will be just:
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

Java Android ScrollView buttons always at the top

Hi I have a ScrollView and inside I have a LinearLayout and inside I have a buttons. I don't know how I can have this buttons always at the top when I scroll a view. Now when I scroll view a buttons disappear.
My code :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:weightSum="1">
<LinearLayout
android:id="#+id/LinearLayout02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|bottom"
android:layout_marginTop="0dp">
<Button
android:id="#+id/cpic"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:text="Zdjęcie" />
<Button
android:id="#+id/up"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:text="Wyślij" />
</LinearLayout>
<ImageView
android:id="#+id/Imageprev"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_marginTop="58dp" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
what I understand from your question i think you can achieve it by putting your button outside your scroll view inside XML , this would give you a fix button.
hope this helps.

why cannot i see a textView above another view in relative layout?

I have this xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp" >
<TextView
android:id="#+id/errorMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/pointsText"
android:visibility="gone"/>
<TextView
android:id="#+id/pointsText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/offers_list"
android:text="you have 100 points"
android:visibility="visible"/>
<ListView
android:id="#+id/offers_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false"
android:dividerHeight="0dp"
android:divider="#00000000"/>
</RelativeLayout>
Yet I cannot see pointsText in my eclipse graphical previewer.
What am I missing?
You have
android:layout_above="#+id/offers_list"
for your TextView but you don't have any positioning for your ListView. Since RelativeLayouts place their children at the top-left by default, your ListView would be at the top and there would be nowhere above it to place your TextView.
Try removing that property from your TextView and add
android:layout_below="#id/pointsText"
on your ListView.
I just tested this and it does work. I can see the pointsText TextView above the list. So, you will want to do something different with your error TextView or you will quite possibly have the same issue with that when you want to change its visibility.
A bit off-topic
RelativeLayout doesn't have an orientation property so that doesn't do you any good but doesn't do you any real harm either.
When you use relative layouts, layout elements lay upon each other. To prevent that you should place element Relatively to other layouts. make your code like this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp" >
<TextView
android:id="#+id/errorMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/pointsText"
android:visibility="gone"/>
<TextView
android:id="#+id/pointsText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/offers_list"
android:text="you have 100 points"
android:visibility="visible"
android:layout_below="#+id/errorMsg"/>
<ListView
android:id="#+id/offers_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false"
android:dividerHeight="0dp"
android:divider="#00000000"
android:layout_below="#+id/pointsText"/>
</RelativeLayout>
Now you will be able to see your all layouts
try this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/errorMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<TextView
android:id="#+id/pointsText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="you have 100 points"
android:visibility="visible"/>
</LinearLayout>
<ListView
android:id="#+id/offers_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/linearLayout1"
android:divider="#00000000"
android:dividerHeight="0dp"
android:drawSelectorOnTop="false" />
</RelativeLayout>
If this is a fragment inside an activity, you may want to check the nesting of say, the AppBarLayout and the layout into which you add the fragment

Categories

Resources