冻结UI时片段交易 [英] Freezing UI when fragment transaction

查看:294
本文介绍了冻结UI时片段交易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在面临的问题与使用FragmentManager替换片段。我的问题是冻结UI。我试图找到一些好的做法和/或库来处理​​我的问题。

I'm facing problem with replacing fragment using FragmentManager. My problem is freezing the UI. I'm trying to find some good practices and/or library to handle my problems.

我的code的部分:

主要业务布局:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</android.support.v4.widget.DrawerLayout>

主要业务类

public class MyActivity extends Activity 

使用方法

  public void nextFragment(Fragment fragment, int position) {
    fragment.setArguments(new Bundle());
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager
            .beginTransaction()
            .replace(R.id.content, fragment).commit();

}

每个片段我的是像

import android.app.Fragment;

public class SomeFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    RelativeLayout rootView = (RelativeLayout) inflater.inflate(
            R.layout.fragment_layout, container, false);
    //many long time data downloading & creating view
    }

private void nextFragment() {

        ((MyActivity) getActivity()).nextFragment(
                new SomeNextFragment(), 0);

    }
}

现在,因为下载的每个片段的数据开始我的用户界面冻结。感谢您事先的任何帮助。

Now because of downloading data on each fragment start my UI is freezing. Thanks in advance for any help.

推荐答案

作为一个规则,你不应该做长时间运行/ UI线程阻断操作。要么使用一个工作线程或<一个href=\"http://stackoverflow.com/questions/14250989/how-to-use-asynctask-correctly-android\"><$c$c>AsyncTask.

As a rule, you should not do the long running/blocking operations on the UI thread. Either use a worker thread or an AsyncTask.

一般情况下,我建议你创造任何你可以在 onCreateView 然后设定值后台操作完成之后。例如,创建一个TextView马上,然后当你从后台操作的结果,然后将在已经存在的TextView文本。

Generally, I would suggest that you create whatever you can in onCreateView then set values after the background operation is complete. For example, create a TextView right away, then when you get the result from the background operation, then set the text in the already existing TextView.

要使用一个线程:

final Handler handler = new Handler();
new Thread(new Runnable(){
    @Override
    public void run(){
       //long running code
       //this is running on a background thread
       final String someText = //result of long running code
       handler.post(new Runnable(){
           @Override
           public void run(){
               //since the handler was created on the UI thread,
               //   this code will run on the UI thread
               someTextView.setText(someText);
           }
       });
    }
}).start();

您也可以使用 getActivity()runOnUiThread(新的Runnable(){...});由而不是使用处理程序

You can also use getActivity().runOnUiThread(new Runnable(){...}); instead of using Handler

这篇关于冻结UI时片段交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆