Android fragment 切換載入數據卡頓問題

來源:互聯網
上載者:User

標籤:android   fragment   卡頓   

      接著上一篇項目的進度,上一篇講了如何利用fragment來實現下拉式功能表,公用菜單,以實現切換主介面資料的功能,這時候遇到的問題是:使用了fragment的切換介面方法,但載入的資料太多,使用者從一個介面切換到這個介面的時候,至少有一兩秒的卡頓,這是無法忍受的,代碼如下:

private void initOpenMenuItem(View popupWindow_view) {<span style="white-space:pre"></span>DrawableCenterTextView menu_price = (DrawableCenterTextView) popupWindow_view<span style="white-space:pre"></span>.findViewById(R.id.menu_price);<span style="white-space:pre"></span>menu_price.setOnClickListener(new OnClickListener() {<span style="white-space:pre"></span>FragmentTransaction transaction;<span style="white-space:pre"></span>@Override<span style="white-space:pre"></span>public void onClick(View v) {<span style="white-space:pre"></span>transaction = manager.beginTransaction();<span style="white-space:pre"></span><span style="background-color: rgb(255, 255, 0);"><span style="font-size:18px;color:#ff0000;"><strong>transaction.replace(R.id.fragment_container, priceFragment);</strong></span></span><span style="white-space:pre"></span>titleView.setText(priceFragment.getFragmentTitle());<span style="white-space:pre"></span>transaction.commit();<span style="white-space:pre"></span>popupWindow.dismiss();<span style="white-space:pre"></span>}<span style="white-space:pre"></span>});<span style="white-space:pre"></span>}
     上述代碼在切換fragment介面的時候,根據fragment的方法api,replace是要重建執行個體的,這就勢必導致資料重新載入,我們必須得採取隱藏的方式或者加一個進度條來緩解。這裡我採用的是fragment提供的hide方法,來實現資料的快速顯示,修改代碼如下:

private void initOpenMenuItem(View popupWindow_view) {DrawableCenterTextView menu_price = (DrawableCenterTextView) popupWindow_view.findViewById(R.id.menu_price);menu_price.setOnClickListener(new OnClickListener() {FragmentTransaction transaction;@Overridepublic void onClick(View v) {transaction = manager.beginTransaction();/* * qiulinhe:2015年7月21日10:54:51 * 解決切換卡頓的問題 */<span style="font-size:18px;color:#333333;"><strong style="background-color: rgb(192, 192, 192);">if (!priceFragment.isAdded()) {    // 先判斷是否被add過                transaction.hide(openPositionFragment).add(R.id.fragment_container, priceFragment).commit(); // 隱藏當前的fragment,add下一個到Activity中                titleView.setText(priceFragment.getFragmentTitle());} else {                transaction.hide(openPositionFragment).show(priceFragment).commit(); // 隱藏當前的fragment,顯示下一個                titleView.setText(priceFragment.getFragmentTitle());</strong></span><span style="color:#c0c0c0;">}</span>popupWindow.dismiss();}});}
       原理就是:

翻看了Android官方Doc,和一些組件的原始碼,發現,replace()這個方法只是在上一個Fragment不再需要時採用的簡便方法。

正確的切換方式是add(),切換時hide(),add()另一個Fragment;再次切換時,只需hide()當前,show()另一個。
這樣就能做到多個Fragment切換不重新執行個體化:


            由於是公司項目,不能上傳整個工程檔案,不過可以參考一下我的解決方式,有同樣的問題的同學可以交流一下還有沒有更優雅的解決辦法。

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Android fragment 切換載入數據卡頓問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.