2016-12-24 46 views
0

我想向具有尋呼機視圖的活動添加屏幕旋轉支持。我想要的是,當用戶更改屏幕方向時,已打開的分頁視圖的選項卡應以新方向打開。但現在活動重新啓動並且每次打開第一個選項卡。請幫助我。提前致謝。如何通過屏幕旋轉存儲尋呼機視圖的狀態

package com.example.ali.namallibrary; 
import android.app.Fragment; 
import android.content.Context; 
import android.support.design.widget.TabLayout; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.PagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import java.io.Serializable; 

public class aboutLibrary extends AppCompatActivity { 

CustomAdapter customAdapterpter = null; 
TabLayout tabLayout; 
ViewPager viewPager; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_about_library); 
    customAdapterpter = new CustomAdapter(getSupportFragmentManager(),getApplicationContext()); 

    viewPager = (ViewPager) findViewById(R.id.viewPager); 
    viewPager.setAdapter(customAdapterpter); 

    tabLayout = (TabLayout) findViewById(R.id.tabBar); 
    tabLayout.setupWithViewPager(viewPager); 
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 
      viewPager.setCurrentItem(tab.getPosition()); 
     } 

     @Override 
     public void onTabUnselected(TabLayout.Tab tab) { 
      viewPager.setCurrentItem(tab.getPosition()); 
     } 

     @Override 
     public void onTabReselected(TabLayout.Tab tab) { 
      viewPager.setCurrentItem(tab.getPosition()); 
     } 
    }); 
} // end of oncreate 

private class CustomAdapter extends FragmentPagerAdapter { 
    private String[] fragmentNames = {"About","Collection","Timing","Contact"}; 
    public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext) { 
     super(supportFragmentManager); 
    } 
    @Override 
    public android.support.v4.app.Fragment getItem(int position) { 
     switch (position) 
     { 
      case 0 : 
       return new aboutLibraryFrag(); 
      case 1 : 
       return new libraryCollectionFrag(); 
      case 2 : 
       return new libraryTimingFrag(); 
      case 3 : 
       return new contactUsFrag(); 
      default: 
       return null; 
     } 
    } 
    @Override 
    public int getCount() { 
     return fragmentNames.length; 
    } 
    @Override 
    public CharSequence getPageTitle(int position) { 
     return fragmentNames[position]; 
    } 
} 

回答

0

無論何時您旋轉屏幕,活動都會被完全銷燬並重新創建。爲了解決這個問題,有一個稱爲onSaveInstanceState的生命週期方法。在這種方法中,您可以保存一個Bundle對象。 A Bundle是您定義的一組鍵值對,旋轉後您需要這些鍵值對。在該Bundle中,可以存儲視圖尋呼機的當前位置。我對ViewPagers沒有太多的工作,但getCurrentItem可能是你要找的。你需要讓你的ViewPager成爲一個成員變量,併爲該鍵設置一個常量。然後我猜它會看起來像:

public void onSaveInstanceState(Bundle savedInstanceState) { 
    // Save the user's current game state 
    savedInstanceState.putInt(CURRENT_POSITION_KEY, mViewPager.getCurrentItem()); 

    // Always call the superclass so it can save the view hierarchy state 
    super.onSaveInstanceState(savedInstanceState); 
} 

onCreate方法採用Bundle savedInstanceState - 如果該項活動從旋轉重新啓動時,該參數將有你在onSaveInstanceState保存一切。因此,你可以做這樣的事情在onCreate

// Check whether we're recreating a previously destroyed instance 
if (savedInstanceState != null) { 
int currentPos = savedInstanceState.getInt(CURRENT_POSITION_KEY); 
mViewPager.setCurrentItem(currentPos) 
} 
有關加載和保存旋轉的狀態

更多信息可以在這裏找到:https://developer.android.com/guide/components/activities/activity-lifecycle.html#saras

+0

感謝@Lyla ...樂於助人......現在,我得到這個想法... –