ImageView causes NestedScrollView to automatically scroll upward - android

I have an ImageView inside RelativeView that is inside NestedScrollView, when I set the ImageView height it causes the an automatic upward scroll right after the inflation. This behaviour doesn't happen when I set the ImageView height to be wrap_content. What could be the reason? Is this some kind of bug in the support library?
Notes:
If I kept the ImageView height as 170 & removed the RelativeLayout this upward auto-scroll doesn't happen.
If I kept the RelativeLayout & set ImageView height to wrap_content this upward auto-scroll doesn't happen.
<android.support.v4.widget.NestedScrollView
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"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.mydomain.test">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="170dp"
android:scaleType="fitXY"
android:src="#drawable/cover"/>
</RelativeLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Update & Fix:
Adding android:descendantFocusability="blocksDescendants" to the vertical LinearLayout fixed the problem to be:
<android.support.v4.widget.NestedScrollView
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"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.mydomain.test">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:descendantFocusability="blocksDescendants">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="170dp"
android:scaleType="fitXY"
android:src="#drawable/cover"/>
</RelativeLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>

I found the solution and the reason of the problem, In the code above I have the RelativeLayout inside a vertical LinearLayout, and when I added
android:descendantFocusability="blocksDescendants"
to the vertical LinearLayout, the problem got fixed.

Related

ScrollView padding cut off items

I try set padding on my ScrollView that contain LinearLayout, but i noticed that the first/last items cutting off.
Its seem like the ScrollView padding himself with the background, but don't notify a LinearLayout about that.
anyone know whay this happe?
Thanks..
My code:
<android.support.constraint.ConstraintLayout
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:padding="20dp"
tools:context="com.example.aviad.scrollview.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="40dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:fillViewport="true"
android:background="#54d45f"
android:paddingTop="15dp"
android:paddingBottom="15dp">
<LinearLayout
android:id="#+id/conteiner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"/>
</ScrollView>
</android.support.constraint.ConstraintLayout>
Result:
enter image description here
Just add android:clipToPadding="false" to your LinearLayout

Scroll View is not working with relative layout

I tried to make a scroll view. However,it does not display the viewPager inside the scroll view. I don't know which part is wrong.
This is my xml file
<?xml version="1.0" encoding="utf-8"?>
<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.qianonnphoon.tradeal.displaytrade.DisplayTradeActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabBackground="#drawable/tab_selector"
app:tabGravity="center"
app:tabIndicatorHeight="0dp">
</android.support.design.widget.TabLayout>
</android.support.v4.view.ViewPager>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="23dp"
android:layout_below="#+id/view_pager"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/tvOwnItemName"/>
</RelativeLayout>
</ScrollView>
This is the output
The viewPager is not displayed. The viewPager will be displayed only when I removed the scroll view
try this out:
<ScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
android:fillViewport="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</ScrollView>
Try adding this piece of code before loading your viewPager
NestedScrollView scrollView = (NestedScrollView) findViewById
(R.id.nest_scrollview);
scrollView.setFillViewport (true);
Add
android:fillViewport="true"
to scrollview and set scrollview height to
android:layout_height="300dp"
solve my problem.
However, I dont know why the height cannot set as wrap_content

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.

NestedScrollView could not scroll with match_parent height child

I implement NonSwipeableViewPager with a fragment has NestedScrollView like this, what I expect is that the scrollview can scroll up and show 2 textviews:
<?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"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="#layout/header" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp"
android:src="#drawable/ic_up" />
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text 1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text 2" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
But it could not scroll, I tried many ways but still did not get any solution
<?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"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
This linearlayout should have android:layout_height="wrap_content".
Reason for that is that if the scrollview's child is the same size as the scrollview itself (both match_parent for height) it means that there is nothing to scroll through, since they are of same size and the scrollview will only be as high as the screen.
If the linearlayout has a height of wrap_content then the height is not related to the height of the screen and the scrollview will be able to scroll through it.
Just remember that a scrollview can only have 1 direct child, and that child needs android:layout_height="wrap_content"
In my case app:layout_behavior="#string/appbar_scrolling_view_behavior" this is working only if some one face problem will be try it and may be solve your problem too. you should add also android:fillViewport="true" but without this my code working.
<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:fillViewport="true"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#drawable/subscription_background"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
For me it worked when i added "android:layout_marginBottom="100dp"" for last child in androidx.core.widget.NestedScrollView
if you have used netedscrollview as follow you have to use
android:scrollbars="vertical"
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="#id/toolbar_updateUserDetails"
app:layout_constraintBottom_toBottomOf="parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scrollbars="vertical"
>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
You have to calculate your other child because last child binding on nestedScrollView. You add margin like child height. It is working.

Horizontal ScrollView in fragment Android

I would like to make my ScrollView filling out the full layout of a fragment and scrolling horizontally, not vertically.
How can I achieve that?!
Like the album covers
Thanks
Its not vertically, it is horizontally
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
// Add your ImageButtons
</LinearLayout>
</HorizontalScrollView>
You have to set the scrollview to
android:fillViewport="true"
Below is the complete code
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/myscrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
</ScrollView>

Categories

Resources