我想向具有尋呼機視圖的活動添加屏幕旋轉支持。我想要的是,當用戶更改屏幕方向時,已打開的分頁視圖的選項卡應以新方向打開。但現在活動重新啓動並且每次打開第一個選項卡。請幫助我。提前致謝。如何通過屏幕旋轉存儲尋呼機視圖的狀態
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];
}
}
感謝@Lyla ...樂於助人......現在,我得到這個想法... –