我有一個ViewPager與3片段,每個片段都有自己的獨特佈局。每個Fragment通過從保存ViewPager的FragmentActivity的newInstance()方法中獲取佈局ID作爲參數來知道使用哪個佈局。此ViewPager由FragmentStatePagerAdapter控制,名爲MyPagerAdapter。保留查看尋呼機片段佈局的變化
public class MainActivity extends FragmentActivity {
ViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
}
private class MyPagerAdapter extends FragmentStatePagerAdapter {
SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return FirstFragment.newInstance("FirstFragment, Instance 1");
case 1: return SecondFragment.newInstance("SecondFragment, Instance 1");
case 2: return ThirdFragment.newInstance("ThirdFragment, Instance 1");
}
}
@Override
public int getCount() {
return 3;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
}
public void setBackgroundImage(View view){
int i = pager.getCurrentItem();
Fragment page = ((MyPagerAdapter)pager.getAdapter()).getRegisteredFragment(i);
page.getView().findViewById(R.id.first).findViewById(R.id.button1);
page.getView().findViewById(R.id.first).setBackgroundResource(R.drawable.ic_launcher);
}
}
activity_main.xml中:
<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"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
這裏是FirstFragment:
public class FirstFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.first_frag, container, false);
TextView tv = (TextView) v.findViewById(R.id.tvFragFirst);
tv.setText(getArguments().getString("msg"));
return v;
}
public static FirstFragment newInstance(String text) {
FirstFragment f = new FirstFragment();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}}
first_frag.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_orange_dark"
android:id="@+id/first" >
<TextView
android:id="@+id/tvFragFirst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="26dp"
android:text="TextView" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/tvFragFirst"
android:text="Button"
android:onClick="setBackgroundImage"/>
</RelativeLayout>
它有更多的按鈕和imagebuttons與方法的onClick ,但它不是必需的除了需要知道我有許多類似於setBackgroundImage的onClick方法來改變佈局的外觀之外,這個問題是必須的。
SecondFragment和ThirdFragment與它們的佈局類似。
您可以在MainActivity中看到setBackgroundImage方法,它會更改片段的背景圖像。但是一旦它改變了,它就不是永久的,因爲當我翻轉碎片並返回到第一個碎片時,變化不會被保存,而是顯示原始佈局。我明白,一旦失去視覺,碎片的佈局就會被破壞。有沒有辦法保留佈局更改?
yeap。沒有幫助。順便說一句,它是「setRetainInstance(true);」 – Nazerke