2017-10-25 10 views
-1

現在、画面の向きがポートレートからランドスケープに変更されたときに、ビューページがフラグメントを破棄する理由を理解しようとしています。ViewPagerは方向変更時にフラグメントを破棄します

これはViewPagerAdapter.kt

internal class FragmentPagerAdapter(fm: FragmentManager, private val mNumbOfTabs: Int) : FragmentPagerAdapter(fm) { 

    /** 
    * return the fragment for each position 
    */ 
    override fun getItem(position: Int): Fragment? { 
     when (position) { 
      SIGN_UP -> return SignUpFragment() 
      LOGIN -> return LoginFragment() 
      else -> return null 
     } 
    } 

    override fun instantiateItem(container: ViewGroup, position: Int): Any { 
     val ret = super.instantiateItem(container, position) 
     return ret 
    } 

    /** 
    * return number of tabs 
    */ 
    override fun getCount(): Int { 
     return mNumbOfTabs 
    } 

    companion object { 
     private val SIGN_UP = 0 
     private val LOGIN = 1 
    } 
} 

と私はうまく肖像画に変更したときに画面がしかし、風景の中によく合い

Handler().postDelayed({ 
       fadeOutAndHideImage(imagelogo); 
       tabs.addTab(tabs.newTab().setIcon(R.drawable.sign_in).setText(R.string.sign_up)) 
       tabs.addTab(tabs.newTab().setIcon(R.drawable.sign_in).setText(R.string.login)) 
       val adapter = FragmentPagerAdapter(supportFragmentManager, tabs.getTabCount()) 
       viewpager.setAdapter(adapter) 
       viewpager.setOffscreenPageLimit(2) 
       viewpager.addOnPageChangeListener(TabLayoutOnPageChangeListener(tabs)) 
       tabs.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener { 
        override fun onTabSelected(tab: TabLayout.Tab) { 
         // Hide keyboard 
         (getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).hideSoftInputFromWindow(viewpager.getWindowToken(), 0) 
         viewpager.setCurrentItem(tab.position) 
        } 

        override fun onTabUnselected(tab: TabLayout.Tab) { 

        } 

        override fun onTabReselected(tab: TabLayout.Tab) { 

        } 
       }) 
      }, 5000) 

MainActivity

からそれを呼び出すどのような方法であります...私は自分でイメージが話すと思う。いくつかの似たような疑問が、断片を保持することについて何か言った

この

は、ビットバケット内のアプリのリンクです: https://[email protected]/unidevelop/healthy-app.git

Fragment + ViewPager fail when orientation change

Android ViewPager orientation change

ViewPager fragments disappear when change screen rotation

が、結果はそれは違う:(

Landscape

Portrait

[[EDITED]]

SplashLayout.xml

<?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="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:fitsSystemWindows="true" 
    android:orientation="vertical" 
    tools:context="com.freelance.nicaragua_dev.healthyapp.UI.SplashActivity"> 

    <com.freelance.nicaragua_dev.healthyapp.Helper.Kenburns.KenBurnsView 
     android:id="@+id/ken_burns_images" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scaleType="centerCrop"/> 

    <ImageView 
     android:id="@+id/imagelogo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="10dp" 
     android:src="@drawable/logo" /> 

    <com.freelance.nicaragua_dev.healthyapp.Helper.Kenburns.KenBurnsView 
     android:id="@+id/welcome_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:alpha="0.0" 
     android:text="@string/app_name" 
     android:textColor="@color/material_petal" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="24dp" 
     android:gravity="center_horizontal" 
     android:orientation="horizontal"> 
    </LinearLayout> 

    <android.support.v7.widget.CardView 
     android:id="@+id/cardViewPrincipal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginBottom="150dp" 
     android:layout_marginLeft="16dp" 
     android:layout_centerInParent="true" 
     android:layout_marginRight="16dp" 
     android:layout_marginTop="80dp" 
     app:cardCornerRadius="7dp" 
     app:cardElevation="22dp" 
     android:background="@color/transparent"> 

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

      <android.support.design.widget.TabLayout 
       android:id="@+id/tabs" 
       app:tabGravity="fill" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="@android:color/white" 
       app:tabIndicatorHeight="4dp" /> 

      <android.support.v4.view.ViewPager 
       android:id="@+id/viewpager" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="1" /> 
     </LinearLayout> 


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

</RelativeLayout> 

、あなたが同じを使用しているのでSignUp.xml

<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" 
    android:background="@color/transparent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    tools:context=".UI.SignUpFragment"> 

    <android.support.v4.widget.NestedScrollView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="20dp" 
     android:fillViewport="true" 
     android:orientation="vertical"> 

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

      <android.support.design.widget.TextInputLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

       <android.support.design.widget.TextInputEditText 
        android:id="@+id/et_email" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="20dp" 
        android:layout_marginRight="20dp" 
        android:cursorVisible="true" 
        android:gravity="center|left|bottom" 
        android:hint="@string/email" 
        android:inputType="textEmailAddress" 
        android:maxLength="50" 
        android:paddingBottom="10dp" 
        android:textColor="@color/md_black_1000" 
        android:textSize="18sp" /> 

      </android.support.design.widget.TextInputLayout> 

      <android.support.design.widget.TextInputLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:hint="@string/password" 
       app:passwordToggleEnabled="true"> 

       <android.support.design.widget.TextInputEditText 
        android:id="@+id/et_password" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="20dp" 
        android:layout_marginRight="20dp" 
        android:layout_marginTop="30dp" 
        android:cursorVisible="true" 
        android:gravity="center|left|bottom" 
        android:inputType="textPassword" 
        android:maxLength="50" 
        android:paddingBottom="10dp" 
        android:textColor="@color/md_black_1000" 
        android:textSize="18sp" /> 

      </android.support.design.widget.TextInputLayout> 

      <Button 
       android:id="@+id/buttonLogin" 
       android:text="@string/sign_up" 
       android:layout_gravity="center" 
       android:layout_margin="15dp" 
       android:padding="10dp" 
       android:layout_marginRight="5dp" 
       android:layout_width="150dp" 
       android:layout_height="50dp" 
       android:background="@drawable/red_button" 
       android:textColor="@color/md_red_600" /> 

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

</FrameLayout> 
+0

あなたは、異なる配向のために異なるレイアウトを使用していますか? –

+0

いいえ、レイアウトは両方向で同じです –

+0

ビューアのxmlレイアウトも投稿します –

答えて

1

問題はactivity_splash.xmlレイアウトにあります。 cardViewPrincipalのボトムマージンは150dp、上マージンは80dpです。大部分のスクリーンはそのような高いマージンのために十分な不動産を持たないため、これらの値を減らしてください。 2番目の理由は、親ビューに一致させるだけではなく、ビューページの重みを1に設定したことです。 ボーナス(あなたの質問に完全に関係しません) また、RelativeLayoutをという高価なのようにFrameLayoutに置き換える必要があります。フレームレートを低下させるので、使用しないでください。

あなたのXMLは次のようにする必要があります:

<?xml version="1.0" encoding="utf-8"?> 
<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" 
    android:fitsSystemWindows="true" 
    android:orientation="vertical" 
    tools:context="com.freelance.nicaragua_dev.healthyapp.UI.SplashActivity"> 

    <com.freelance.nicaragua_dev.healthyapp.Helper.Kenburns.KenBurnsView 
     android:id="@+id/ken_burns_images" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scaleType="centerCrop"/> 

    <ImageView 
     android:id="@+id/imagelogo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="top|center" 
     android:layout_marginTop="10dp" 
     android:src="@drawable/logo" /> 

    <com.freelance.nicaragua_dev.healthyapp.Helper.Kenburns.KenBurnsView 
     android:id="@+id/welcome_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:alpha="0.0" 
     android:text="@string/app_name" 
     android:textColor="@color/material_petal" 
     android:layout_gravity="bottom|center"/> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|center" 
     android:layout_marginBottom="24dp" 
     android:gravity="center_horizontal" 
     android:orientation="horizontal"> 

     <!--<Button--> 
      <!--android:id="@+id/buttonSignup"--> 
      <!--android:text="@string/sign_up"--> 
      <!--android:layout_marginLeft="5dp"--> 
      <!--android:layout_width="150dp"--> 
      <!--android:layout_height="50dp"--> 
      <!--android:background="@drawable/red_button"--> 
      <!--android:textColor="@android:color/white" />--> 
    </LinearLayout> 

    <android.support.v7.widget.CardView 
     android:id="@+id/cardViewPrincipal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginLeft="16dp" 
     android:layout_marginRight="16dp" 
     android:layout_marginTop="32dp" 
     android:layout_gravity="top" 
     app:cardCornerRadius="7dp" 
     app:cardElevation="22dp" 
     android:background="@color/transparent"> 

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

      <android.support.design.widget.TabLayout 
       android:id="@+id/tabs" 
       app:tabGravity="fill" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="@android:color/white" 
       app:tabIndicatorHeight="4dp" /> 

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


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

</FrameLayout> 
+0

ありがとう!すべてが完璧になります:)あなたは生きているセーバーです! :D –

+0

喜んで:) –

0

ポートラのレイアウトそれモードだけでなく、風景モード、あなたはあなたのAndroidManifest.xmlを編集し、Activityあなたが再描画したくないに次の行を追加することができます。

android:configChanges="keyboardHidden|orientation" 

https://developer.android.com/guide/topics/resources/runtime-changes.html

、これらの構成の1変更、MyActivityは 再起動しません。代わりに、MyActivityは、 onConfigurationChanged()への呼び出しを受け取ります。

多分、あなたがこの場合に探しているものです。

onCreate()に渡されるBundleをクリアすることもできます。このオブジェクトには、ViewPagerが悪い方法で使用する可能性のあるデータが含まれる可能性があるためです。

+0

を更新しました。私はすでにあなたが提案しているように向きを変えましたが、ビューページャは常に私のフラグメントを変更し続けます:( –

+0

'Bundle'をnullにした場合、' super.onCreate(null) '?あなたの' Activity'で? –

関連する問題