2013-07-27 45 views
1

我有一些片段,其中的內部視圖動態膨脹,並添加到一個linearlayout類似於列表視圖的形式排序。這在高端設備上工作得很好,但在中下級設備中,動畫中有非常明顯的滯後,有時動畫會被完全跳過。我嘗試了一下google搜索,並沒有遇到任何具體的事情,當涉及到過渡動畫相關的膨脹意見和提示如何處理動態視圖通脹在這樣一個過程中。Android - 查看通貨膨脹彌補碎片交易動畫

所以回顧....用戶按下按鈕,片段進入視圖,視圖動態膨脹,動畫滯後或跳過。如果可能的話,我真的很想讓一切順利。

編輯:一些示例代碼

public static void addPersonRow(PersonObject po){ 

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 50); 
    params.setMargins(0, 5, 0, 5); 
    final View view = IncidentReport_v2.ir2.getLayoutInflater().inflate(R.layout.ir_involved_people_row, null); 
    view.setLayoutParams(params); 
    view.setTag(po.originalName); 

    RelativeLayout rl = (RelativeLayout) view.findViewById(R.id.ir_involved_ppl_add_row_rl); 

    TextView name = (TextView) view.findViewById(R.id.ir_involved_ppl_name_txt); 
    name.setText(po.firstName+" "+po.lastName); 

    ImageButton open = (ImageButton) view.findViewById(R.id.ir_involved_ppl_reopen_btn); 
    Bundle b = new Bundle(); 
    b.putParcelable("personObj", po); 
    open.setTag(b); 
    name.setTag(b); 
    rl.setTag(b); 

    OnClickListener openClick = new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      pplAddFrag = new IR_PeopleInvolved_AddForm_Fragment(); 
      Bundle b = new Bundle(); 
      b.putParcelable("existingPerson", (Bundle)v.getTag()); 
      pplAddFrag.setArguments(b); 
      android.support.v4.app.FragmentManager fragmentManager = IncidentReport_v2.ir2.getSupportFragmentManager();  
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
      fragmentTransaction.setCustomAnimations(R.anim.bounce, R.anim.bounce_out, R.anim.bounce, R.anim.bounce_out); 
      fragmentTransaction.add(R.id.ir_main_frame, pplAddFrag, "pplAddFrag"); 
      fragmentTransaction.addToBackStack("pplAddFrag"); 
      fragmentTransaction.commit();  

      IncidentReport_v2.theMenu.removeItem(R.id.incident_report_save); 
      IncidentReport_v2.ir2.invalidateOptionsMenu(); 

     } 
    }; 

    open.setOnClickListener(openClick); 
    name.setOnClickListener(openClick); 
    rl.setOnClickListener(openClick); 

    ImageButton delete = (ImageButton) view.findViewById(R.id.ir_involved_ppl_delete_btn); 
    delete.setTag(po); 
    delete.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      pplHolder.removeView(view); 
      IncidentReport_v2.people.remove((PersonObject)v.getTag()); 
      IR_InvolvedFragment.pplCounterTxt.setText(IncidentReport_v2.people.size()+""); 
     } 
    }); 

    pplHolder.addView(view); 

    if(!IncidentReport_v2.people.contains(po)){ 
     IncidentReport_v2.people.add(po); 
    } 

    IR_InvolvedFragment.pplTxt.setTextColor(IncidentReport_v2.ir2.getResources().getColor(R.color.green)); 
    IR_InvolvedFragment.pplCounterTxt.setText(IncidentReport_v2.people.size()+""); 

} 

調用此方法的內部for循環依賴於有多少personObjects有

+0

我們... ...需要的代碼。你沒有回顧任何事情,因爲沒有代碼來重述! – Blundell

+0

不好意思把它砍了一下,但那基本上是我在片段轉換/交易時做的事情 –

回答

1

1)不作方法靜態的(這樣你就可以訪問域更多的優化版本)

2)緩存佈局吹氣,這樣你就不必如果從XML獲取視圖得到它的每一行

3)可以作爲我們LL設置頁邊距和重量在XML

4)創建捆綁關閉UI線程,並在最後時刻還是作爲回調

5)創建片段在其他地方設置,使用相同的片段,每次onclick被按下使用FragmentManager爲此,回調將更輕量級

6)你設置三個onclick監聽器做同樣的事情,你可以不只是委託ImageView點擊父,即只設置點擊關於相對佈局

7)畢竟以上,FragmentTransaction是最慢的東西,可能想看看它改變了customview,只需使用可見&在onclick曠古佈局動畫

給一些,一個去

+0

非常感謝我認爲這些應該有所幫助的建議。有一次我把他們弄糟後回來。 –

+0

此外,不要爲您添加的每個片段啓動一個事務,只需使用一個'commit'將它們全部添加,僅這一項就可以解決您的大部分問題。 – Delyan

+0

但是如果我的活動可能會使用近20個不同的碎片呢? –