【问题标题】:Android - View inflation messes up fragment transaction animationAndroid - 视图膨胀弄乱了片段交易动画
【发布时间】:2013-07-27 00:18:33
【问题描述】:

我有一些片段,其中内部视图被动态膨胀并添加到线性布局中以形成类似于列表视图的形式。这在高端设备上运行良好,但在中低级设备上,动画有非常明显的延迟,有时动画会被完全跳过。我尝试在谷歌上搜索了一下,但在涉及到与膨胀视图相关的过渡动画以及如何在此过程中处理动态视图膨胀的提示时,没有遇到任何具体的问题。

所以回顾一下......用户按下按钮,一个片段进入视图,视图动态膨胀,动画滞后或被跳过。如果可能的话,我真的很想让一切顺利。

编辑:一些示例代码

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()+"");

}

这取决于我有多少 personObjects 在 for 循环中调用

【问题讨论】:

  • 我们……需要……代码。您没有重述任何内容,因为没有可重述的代码!
  • 抱歉不得不把它切碎一点,但这基本上就是我在片段转换/交易时所做的事情

标签: android android-layout fragment layout-inflater


【解决方案1】:

1) 不要将方法设为静态(这样您以后可以访问字段以进行更多优化)

2) 缓存布局充气器,因此您不必为每一行获取它

3) 如果从 XML 中获取视图,不妨在 XML 中设置边距和权重

4) 从 UI 线程创建 Bundle 并在最后一刻设置它或作为回调

5) 在别处创建你的 Fragment,每次按下 onclick 时使用相同的 Fragment,为此使用 FragmentManager,回调会更轻量级

6) 你设置了三个 onclick 监听器来做同样的事情,你能不能只将 ImageView 点击委托给父级,即只设置相对布局上的点击

7) 综上所述,FragmentTransaction 是最慢的,可能想看看将其更改为自定义视图,然后在 onClick 中使用可见和消失的布局动画

试一试

【讨论】:

  • 很酷,感谢您的建议,我知道这些应该会有所帮助。一旦我把他们弄得一团糟,我就会回帖。
  • 此外,不要为您添加的每个片段都启动事务,使用单个 commit 将它们全部添加 - 仅此一项就可以解决您的大部分问题。
  • 但如果我的活动可能使用近 20 个不同的片段怎么办?
  • 你说这是一个有孩子的ListView?你是说你有 20 个不同的孩子,他们一定有共同点,听起来需要重构
  • 抱歉,我撤回对单个事务的评论 - 我认为每一行都已经是一个片段。
猜你喜欢
  • 1970-01-01
  • 2022-06-28
  • 2016-12-13
  • 2016-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-10
  • 1970-01-01
相关资源
最近更新 更多