1

我有一个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方法,它会更改片段的背景图像。但是一旦它改变了,它就不是永久的,因为当我翻转碎片并返回到第一个碎片时,变化不会被保存,而是显示原始布局。我明白,一旦失去视觉,碎片的布局就会被破坏。有没有办法保留布局更改?

回答

0

您是否尝试过在Fragments onCreate()方法中调用onRetainInstance(true)?

当然,解决问题的一种方法是在Activity中存储每个片段的背景资源,以便在创建片段时将其正确初始化。这还涉及将背景数据存储在onSaveInstanceState()方法中的一个包中。

+0

yeap。没有帮助。顺便说一句,它是“setRetainInstance(true);” – Nazerke

相关问题