2

I have an android fragment layout,I want to put my page view inside the scroll view. When app running without scroll view the image it's showing but when I put the page view code inside the scroll view the image it's not showing.

Here is my code

<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"
        tools:context="com.example.wk.sigah.FragmentHome"
        android:background="@color/colorPrimary">


        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v4.view.ViewPager
                android:id="@+id/viewImage"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </android.support.v4.view.ViewPager>

        </ScrollView>

</LinearLayout>
Annie
  • 2,397
  • 3
  • 18
  • 26
Kharisma
  • 107
  • 1
  • 1
  • 9

1 Answers1

3

Override onMeasure in class and extends ViewPager as follows, this will make it get the height of the biggest child it currently has.

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;

public class CustomViewPager extends ViewPager {

public CustomViewPager(Context context) {
    super(context);
}

public CustomViewPager(Context context, AttributeSet attrs){

    super(context, attrs);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int height = 0;
    for(int i = 0; i < getChildCount(); i++) {

        View child = getChildAt(i);

        child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

        int h = child.getMeasuredHeight();

        if(h > height) height = h;

    }

    heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}

In XML :

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:fillViewport="true">

   <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

    <com.your.package.name.CustomViewPager
        android:id="@+id/viewImage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" />

</LinearLayout>

</android.support.v4.widget.NestedScrollView>

Tutorial

Vidhi Dave
  • 5,614
  • 2
  • 33
  • 55
  • my _app:layout_behavior_ is missing – Kharisma Mar 26 '18 at 10:45
  • It say _validates resource reverence inside Android XML files_ – Kharisma Mar 26 '18 at 10:48
  • @Kharisma In xml you need to replace the custom view pager line to your package and in java you need to declare bby CustomViewPager not viewpager. you have done that correctly? – Vidhi Dave Mar 26 '18 at 10:50
  • yes, when I put _app:layout_behavior_ in _nested scroll view_ is still missing – Kharisma Mar 26 '18 at 10:57
  • @Kharisma remove that from nested scrollview. and run the code and let me know – Vidhi Dave Mar 26 '18 at 10:59
  • Why this xml. use the xml i have given in the answer. what are you doing that i am not able to understand. just copy paste my answer and check once. and if any error then give proper error log – Vidhi Dave Mar 26 '18 at 11:09
  • What you need to do is create one java class name this to CustomViewPager. then add my answer code to that class. Now in your xml add this code snippet. And in this replace package name with your. that's it. it has no error i have tested – Vidhi Dave Mar 26 '18 at 11:12
  • got some error _Error:(14) Error parsing XML: unbound prefix_, _Error:Execution failed for task ':app:processDebugResources'. > com.android.ide.common.process.ProcessException: Failed to execute aapt_, _Error:(8) No resource identifier found for attribute 'fillViewPort' in package 'android'_ – Kharisma Mar 26 '18 at 11:25
  • @Kharisma Sorry spelling mistake :( change that to `android:fillViewport="true"` – Vidhi Dave Mar 26 '18 at 11:27
  • my _app:layout_behavior="@string/appbar_scrolling_view_behavior"_ still unbound prefix, but when I delete it apps is running, what about that? – Kharisma Mar 26 '18 at 11:35
  • @Kharisma It is running and giving perfect output as needed?? – Vidhi Dave Mar 26 '18 at 11:38
  • @Kharisma for app `xmlns:app="http://schemas.android.com/apk/res-auto"` this is namespace. in android studio you can alt+enter on `app:` and you will get that namespace – Vidhi Dave Mar 26 '18 at 11:40
  • yes, how can I test the scroll view work properly ? can you help me give example show one text view below the CustomPageViewer in XML file? – Kharisma Mar 26 '18 at 11:57
  • @Kharisma Happy to help :) Happy coding :) – Vidhi Dave Mar 27 '18 at 04:15