0

I want to make responsive my ViewPager but I don't know why it isn't working.

I have got a TabLayout with 3 different tabs which (at the moment) displays 3 times the same fragment using ScreenSlide and you can switch when slide the viewPager.

If I manually set the height for example 300dpi I can see the text of my fragment (not all the text only the text depending of the dpi in height) in every tab of my viewPager, but if I use match-parent or wrap-content instead of use a manual height I can't see anything.

Maybe my viewPager can't get the dimensions of my fragment or something else?

Thanks for your attention and time and excuse my bad English.

content.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:cardElevation="12dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.anonymous.mh4.monsters.MonsterInfoActivity"
    tools:showIn="@layout/activity_monster_info">

    <android.support.v4.widget.NestedScrollView
        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.support.v7.widget.CardView
            android:id="@+id/onemonstername"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardBackgroundColor="@color/colorPrimary"
            app:cardElevation="4dp">

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

                <TextView
                    android:id="@+id/tvmonster"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="ANJANATH"
                    android:textAlignment="center"
                    android:textColor="@color/cardview_light_background"
                    android:textSize="30sp" />
            </LinearLayout>
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:id="@+id/monstrertypecard"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardBackgroundColor="@android:color/holo_red_light"
            app:cardElevation="4dp"
            app:cardUseCompatPadding="true"
            app:contentPadding="0dp">

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

                <TextView
                    android:id="@+id/type"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="Wyvern Brutal"
                    android:textAlignment="center"
                    android:textColor="@color/cardview_light_background"
                    android:textSize="18sp" />
            </LinearLayout>
        </android.support.v7.widget.CardView>
        <Space
            android:layout_width="match_parent"
            android:layout_height="10dp" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:visibility="visible"
            app:srcCompat="@drawable/anjaimg" />

        <android.support.v7.widget.CardView
            android:id="@+id/breakablecard"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardElevation="5dp"
            app:cardUseCompatPadding="true">

            <TextView
                android:id="@+id/breakableview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Rompíble: Cabeza, Patas traseras, Cola (cortable)"
                android:textAlignment="center"
                android:gravity="center"
                android:textColor="@color/colorPrimaryDark"
                android:textSize="18sp" />
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:id="@+id/gamedescriptioncard"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardBackgroundColor="@color/cardview_shadow_start_color"
            app:cardElevation="2dp"
            app:cardUseCompatPadding="true">

            <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:gravity="center"
                    android:orientation="horizontal">

                    <ImageView
                        android:id="@+id/monstericoninfo"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:adjustViewBounds="true"
                        android:cropToPadding="false"
                        card_view:srcCompat="@drawable/bigm1icon" />


                    <TextView
                        android:id="@+id/gamedescrip"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentStart="true"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="14dp"
                        android:layout_marginStart="14dp"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:text="Los Anjanath patrullan por el Bosque Primigenio en busca de su comida preferida, los Aptonoth. Este agresivo monstruo ataca a cualquiera sin titubear."
                        android:textAlignment="center"
                        android:textSize="16dp" />


                </LinearLayout>

            </LinearLayout>


        </android.support.v7.widget.CardView>


        <Space
            android:layout_width="match_parent"
            android:layout_height="0dp" />

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


            <android.support.design.widget.TabLayout
                android:id="@+id/tabLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <android.support.design.widget.TabItem
                    android:id="@+id/ecologyTab"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Ecologia" />

                <android.support.design.widget.TabItem
                    android:id="@+id/killTab"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Cómo matarlo" />

                <android.support.design.widget.TabItem
                    android:id="@+id/tipsTab"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Tips" />
            </android.support.design.widget.TabLayout>


            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="350dp" />
        </LinearLayout>

        <android.support.v7.widget.CardView
            android:id="@+id/descripcardview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="visible"
            app:cardElevation="2dp"
            app:cardUseCompatPadding="true">

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

                <TextView
                    android:id="@+id/ecology"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="@dimen/text_margin"
                    android:text="El Anjanath es uno de los depredadores que domina el Bosque Primigenio, donde rivaliza con Rathalos, y también puede encontrarse en el Yermo de Agujas. Se alimenta principalmente de Aptonoth, a los que caza usando su agudo olfato. El Anjanath marca su territorio dejando su saliva pegajosa sobre árboles y rocas para mantener a los intrusos a raya.El Anjanath puede atacar con sus enormes fauces, arremetiendo con su cuerpo o dando coletazos, no obstante, cuando se enfurece es capaz de utilizar fuego para atacar. De forma similar al Glavenus, un órgano en su garganta le permite imbuir su boca en llamas, pudiendo luego expulsar llamaradas para atacar o dar mordiscos."
                    android:textSize="16sp" />


            </LinearLayout>


        </android.support.v7.widget.CardView>


        <android.support.v7.widget.CardView
            android:id="@+id/howtokillcard"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"
            app:cardElevation="2dp"
            app:cardUseCompatPadding="true">

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

                <TextView
                    android:id="@+id/howtokillview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="@dimen/text_margin"
                    android:text="El Anjanath es posiblemente el monstruo más peligroso que pulula por el Bosque Primigenio. Es letal a cualquier distancia y sus ataques de Fuego son muy peligrosos. Es muy débil contra el Agua. Tu mejor opción es mantenerte cerca de sus patas traseras (cuidado, cocea, pero es su ataque más débil), golpearle y evitar sus mordiscos y ataques de barrido con la cola. Evita colocarte detrás de él, porque dará latigazos con la cola. Si quieres neutralizar su aliento de Fuego, concentra los ataques en la cabeza hasta romperla."
                    android:textSize="16sp" />


                </LinearLayout>
    
    
            </android.support.v7.widget.CardView>
    
            <android.support.v7.widget.CardView
                android:id="@+id/tipscard"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone"
                app:cardElevation="2dp"
                app:cardUseCompatPadding="true">
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
    
                    <TextView
                        android:id="@+id/tipsview"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/text_margin"
                        android:text="Su cabeza y cada pata trasera se pueden romper y su cola se puede cercenar. Atacarle lo suficiente en la cabeza cuando tiene la garganta iluminada puede hacer que caiga. Su oído y su olfato son muy agudos, pero su vista es bastante pobre. En peleas territoriales, vence al Gran Jagras y pierde contra Rathalos."
                        android:textSize="16sp" />
    
    
                </LinearLayout>
    
    
            </android.support.v7.widget.CardView>
    
    
        </LinearLayout>
    
        </android.support.v4.widget.NestedScrollView>
    
    </android.support.constraint.ConstraintLayout>

myFragment.xml

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/descripcardview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    app:cardElevation="2dp"
    app:cardUseCompatPadding="true">


        <TextView
            android:id="@+id/textViewFragmentEco"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:padding="15dp"
            android:text="El Anjanath es El Anjanath es uno de los depredadores que domina el Bosque Primigenio, donde rivaliza con Rathalos, y también puede encontrarse en el Yermo de Agujas. Se alimenta principalmente de Aptonoth, a los que caza usando su agudo olfato. El Anjanath marca su territorio dejando su saliva pegajosa sobre árboles y rocas para mantener a los intrusos a raya.El Anjanath puede atacar con sus enormes fauces, arremetiendo con su cuerpo o dando coletazos, no obstante, cuando se enfurece es capaz de utilizar fuego para atacar. De forma similar al Glavenus, un órgano en su garganta le permite imbuir su boca en llamas, pudiendo luego expulsar llamaradas para atacar o dar mordiscos.uno de los depredadores que domina el Bosque Primigenio, donde rivaliza con Rathalos, y también puede encontrarse en el Yermo de Agujas. Se alimenta principalmente de Aptonoth, a los que caza usando su agudo olfato. El Anjanath marca su territorio dejando su saliva pegajosa sobre árboles y rocas para mantener a los intrusos a raya.El Anjanath puede atacar con sus enormes fauces, arremetiendo con su cuerpo o dando coletazos, no obstante, cuando se enfurece es capaz de utilizar fuego para atacar. De forma similar al Glavenus, un órgano en su garganta le permite imbuir su boca en llamas, pudiendo luego expulsar llamaradas para atacar o dar mordiscos."
            android:textAlignment="gravity"
            android:textSize="16sp" />
</android.support.v7.widget.CardView>

I tried a lot of different combinations with match_parent and wrap_content. (In fragment and in the content XML)

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Tatsuya
  • 352
  • 4
  • 18
  • I don't see the ViewPager inside of any layout files. – cincy_anddeveloper Mar 06 '18 at 16:13
  • First XML, more or less at the middle. @WadeWilson – Tatsuya Mar 06 '18 at 16:15
  • This is a interesting problem, I've never run into this before. I'm wondering if it's because you set the CarView's height to be wrap_content instead of a explicity value. Try making the CardView the same height as the you declared the VIewpager. If that doesn't work, try replacing the CardView with a LinearLayout and see if the Fragment layout fills the ViewPager. – cincy_anddeveloper Mar 06 '18 at 16:23
  • "Try making the CardView the same height as the you declared the VIewpager." If i set in the fragment a fixed height in a element, will be responsive in the content.xml or still fixed height? @WadeWilson Any way, i'll try at night to edit the fragment XML, but i'm not thinking that is a fragment problem. I post it here to try if the expert people can find some solution in the XML. If nobody can't see anything im going to think that there is a code problem. – Tatsuya Mar 06 '18 at 16:38
  • Also, I am not familiar with ConstraintLayout but it could be making your ViewPager smaller that desired due to other element around it. That is complete speculation as I am not a ConstraintLayout expert. – cincy_anddeveloper Mar 06 '18 at 16:50
  • I don't think it, all dimensions / margins / paddings seems to work fine when the viewPager know the height. But I apreciate your comments. Thanks for help me. – Tatsuya Mar 06 '18 at 17:02
  • I think your nested scrollview is conflicting with your viewpager. – AIK Mar 06 '18 at 17:04
  • @AIK How will you fix it? – Tatsuya Mar 06 '18 at 17:13
  • well it is a complex situation i have never tried like this but try removing nested scrollview and then check please. – AIK Mar 06 '18 at 17:14
  • I'll try if it scroll still works removing the nested scrollview. When I check it i'll feedback @AIK. Thanks for comment and propose a solution. – Tatsuya Mar 06 '18 at 17:19
  • For the moment im going to try it: https://stackoverflow.com/questions/32330639/dynamic-height-viewpager/32410274#32410274. – Tatsuya Mar 06 '18 at 18:33

1 Answers1

0

Solution: https://github.com/vabhishek/WrapContentViewPagerDemo

Thanks to much to everybody who commented.

Create a custom ViewPager:

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


public class CustomPager extends ViewPager {

    private View mCurrentView;

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

    public CustomPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mCurrentView == null) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            return;
        }
        int height = 0;
        mCurrentView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        int h = mCurrentView.getMeasuredHeight();
        if (h > height) height = h;
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void measureCurrentView(View currentView) {
        mCurrentView = currentView;
        requestLayout();
    }

    public int measureFragment(View view) {
        if (view == null)
            return 0;

        view.measure(0, 0);
        return view.getMeasuredHeight();
    }
}
Tatsuya
  • 352
  • 4
  • 18