0

我要補充TextViewsEditTexts片段(詳細資料())當我在一個按鈕(按鈕添加)點擊。 我能夠插入它們,但是當我滑動我的6個片段並返回第一個片段時,此項目消失。如何編輯片段並保存其狀態?

有人可以幫助我嗎?

FragmentActivity類:

public class SubActivityTab extends FragmentActivity 
{ 
    SectionsPagerAdapter spa; 
    ViewPager vp; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.scrolltab_layout); 

     spa = new SectionsPagerAdapter(getSupportFragmentManager(), this); 

     vp = (ViewPager) findViewById(R.id.pager); 
     vp.setAdapter(spa); 
    } 
} 

FragmentPagerAdapter類:

public class SectionsPagerAdapter extends FragmentPagerAdapter 
{ 
    Context c; 

    public SectionsPagerAdapter(FragmentManager fm, Context c) { 
     super(fm); 
     this.c = c; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     switch(position) 
     { 
      case 0: 
      return Details.newInstance(); 
      ... 
     } 
     return null; 
    } 

    @Override 
    public int getCount() { 
    return 6; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     Locale l = Locale.getDefault(); 
     switch (position) 
     { 
     case 0: 
     return c.getString(R.string.tab1).toUpperCase(l); 
       ... 
     } 
     return null; 
    }  
} 

片段類:

public final class Details extends Fragment { 

    public static View layout; 
    public static LinearLayout ll; 

    public static Details newInstance() 
    {  
     return new Details(); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     super.onCreateView(inflater, container, savedInstanceState); 

     layout = inflater.inflate(R.layout.details, container, false); 
     ll = (LinearLayout) layout.findViewById(R.id.linearlayout2); 
     Button add = (Button) layout.findViewById(R.id.button_add); 

     add.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v) { 

     insertLine(); 
     insertClassEditText(); 
     insertLevel(); 
     }}); 

     return layout; 
    } 

    private void insertLine() 
    { 
     View temp = (View) layout.findViewById(R.id.line); 
     View v = new View(temp.getContext()); 
     v.setBackgroundColor(Color.rgb(209, 209, 209)); 
     v.setId(0); 
     v.setLayoutParams(temp.getLayoutParams()); 
     ll.addView(v); 

    } 

    private void insertClassEditText() 
    { 
     TextView label_classe = (TextView) layout.findViewById(R.id.textView10); 
     EditText edit_classe = (EditText) layout.findViewById(R.id.editText9); 

     TextView new_label_classe = new TextView(label_classe.getContext()); 
     EditText new_edit_classe = new EditText(edit_classe.getContext()); 

     new_label_classe.setId(1); 
     new_edit_classe.setId(2); 

     new_label_classe.setText("Label"); 
     new_label_classe.setTextAppearance(new_label_classe.getContext(), android.R.style.TextAppearance_Medium); 
     ll.addView(new_label_classe); 

     new_edit_classe.setInputType(edit_classe.getInputType()); 
     new_edit_classe.setLayoutParams(edit_classe.getLayoutParams()); 
     ll.addView(new_edit_classe); 

    } 

    private void insertLevel() 
    { 
     TextView label_lv = (TextView) layout.findViewById(R.id.textView11); 
     EditText edit_lv = (EditText) layout.findViewById(R.id.editText10); 

     TextView new_label_lv = new TextView(label_lv.getContext()); 
     EditText new_edit_lv = new EditText(edit_lv.getContext()); 

     new_label_lv.setId(3); 
     new_edit_lv.setId(4); 

     new_label_lv.setText(label_lv.getText()); 
     new_label_lv.setTextAppearance(new_label_lv.getContext(), android.R.style.TextAppearance_Medium); 
     ll.addView(new_label_lv); 

     new_edit_lv.setInputType(edit_lv.getInputType()); 
     new_edit_lv.setLayoutParams(edit_lv.getLayoutParams()); 
     ll.addView(new_edit_lv); 
    } 
} 

回答

1

我相信ViewPager可以在您滑動時回收視圖,因此當您返回時,它會從onCreateView重建。

我會建議設置一些標籤並傳遞到onSaveInstanceState

private void onSaveInstanceState(Bundle b) { 
    if (label_classe != null) { 
    b.putBoolean("label_classe", true); 
    } 
} 

然後在onCreateView你把savedInstanceState,檢查哪些元素被創建並重新創建它們。

所以在片段:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 

    .... 
    if (savedInstanceState.getBoolean("label_classe")) { 
     insertClassEditText(); 
    } 

    .... 
} 

而只是可以肯定,在活動時間:

public class SubActivityTab extends FragmentActivity 
{ 
    SectionsPagerAdapter spa; 
    ViewPager vp; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.scrolltab_layout); 

     spa = new SectionsPagerAdapter(getSupportFragmentManager(), this); 

     vp = (ViewPager) findViewById(R.id.pager); 
     vp.setSaveState(true); 
     vp.setSaveFromParentEnabled(true); 
     vp.setAdapter(spa); 
    } 
} 
+0

謝謝Arturs您答案,但我嘗試過,並沒有工作......同樣的問題 – Mondo 2013-03-18 23:42:31

0

試圖改變自己的onCreateView方法:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 

    if(layout ==null){ 
     layout = inflater.inflate(R.layout.details, container, false); //Dont inflate every time.layout saved, data of layout,like EidtText also saved. 
    } 

    ViewParent parent = mLayout.getParent(); 
    if(parent!=null){ 
     ((ViewGroup)parent).removeView(layout);//the view will be add to NoSaveStateFrameLayout after onCreateView,so remove self everytime 
    } 
    return layout; 
} 
相關問題