android fragment+ FragmentTabHost+viewpager 切換狀態不儲存的問題

來源:互聯網
上載者:User

android fragment+ FragmentTabHost+viewpager 切換狀態不儲存的問題

 

@author dujinyang

難得有時間上來寫部落格。

fragment雖然好用,但個人還是覺得有利有弊。

網上大都說切換狀態,有挺多問題,

有很多種解決方案。

第一種常常遇到的情況:

if(rootView == null)rootView = inflater.inflate(R.layout.fragment_home, container,false);ViewGroup parent = (ViewGroup) rootView.getParent(); if (parent != null)      parent.removeView(rootView); initView(rootView);  return rootView;

 

 

第二種是LISTVIEW:

同樣的道理,在listview的適配器裡面,

 

@Overridepublic View getView(int position, View convertView, ViewGroup parent) {View view=convertView;if(view==null){view=new TextView(getActivity().getApplicationContext());}((TextView)view).setText(這是第+position+條測試資料);((TextView)view).setTextSize(20);return view;}

 

 

 

第三種估計比較少人遇到:

折騰了一下午,因為需求關係,需要在TAB第一選項裡面加入 ViewPager+Tabhost +動畫,

其實搞了很久才發現,原來是動畫那一層,沒有儲存,但是因為是圖片的原因,又無法儲存位置,

測試了很久解決了。

OnPageChangeListener裡面重寫的onPageSelected,switch的選取器裡動畫層輸出由它內建arg0的,
為每一個tab增加多一個自身選項,作為位置儲存,不能在外層記錄位置,因為會出BUG的。通過每一個tab的位置計算出來原來所在的位置,這樣就可以解決滑動時產生的位置資訊。
希望對大家有用。如果有其它的疑難雜症,可留言或加我Q。

 

轉載請註明出處:http://blog.csdn.net/djy1992/article/details/46674169

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.