Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android片段中的调用异步任务速度较慢_Java_Android_Android Fragments_Android Asynctask - Fatal编程技术网

Java Android片段中的调用异步任务速度较慢

Java Android片段中的调用异步任务速度较慢,java,android,android-fragments,android-asynctask,Java,Android,Android Fragments,Android Asynctask,我正在尝试加载包含3个片段的活动。对于每个片段,我使用和Asynctask,并在每个片段的onPostExecute()方法上加载Ui元素。问题是,当Asynctask加载ui元素时,当我从一个片段切换到另一个片段时,应用程序的速度非常慢。我试图通过调用onUiThread()来解决这个问题,它稍微降低了速度。Asynctasks在片段的onCreateView方法中使用 我能做些什么来消除这种滞后 这里有一个异步任务: public class MovieCommentsAsynctask e

我正在尝试加载包含3个片段的活动。对于每个片段,我使用和Asynctask,并在每个片段的onPostExecute()方法上加载Ui元素。问题是,当Asynctask加载ui元素时,当我从一个片段切换到另一个片段时,应用程序的速度非常慢。我试图通过调用onUiThread()来解决这个问题,它稍微降低了速度。Asynctasks在片段的onCreateView方法中使用

我能做些什么来消除这种滞后

这里有一个异步任务:

public class MovieCommentsAsynctask extends AsyncTask<String, Void, String> {
private String TAG = "MovieCommentsAsynctask";
private Context context;
private boolean test;
private HttpClient client;
private List<MovieCommentsObject> comments;
private LayoutInflater inflater;
private Activity activity;
//
private RelativeLayout rl;
private ProgressBar pb;
private PullToRefreshScrollView sv;
private LinearLayout ll;
private Button btn;

public MovieCommentsAsynctask(Context context, RelativeLayout rl,
        boolean test, Activity activity) {
    this.context = context;
    this.activity = activity;
    this.rl = rl;
    this.test = test;
    client = new DefaultHttpClient();
    bind();
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

private void bind() {
    ll = (LinearLayout) rl.findViewById(R.id.ll_fragment_movie_comments);
    sv = (PullToRefreshScrollView) rl
            .findViewById(R.id.sv_fragment_movie_comments);
    pb = (ProgressBar) rl.findViewById(R.id.pb_fragment_movie_comments);
    btn = (Button) rl.findViewById(R.id.btn_fragment_movie_comments_add);

}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    Log.i(TAG, "onPreExecute");
    if (test) {
        pb.setVisibility(View.VISIBLE);
    } else {

    }
}

@Override
protected String doInBackground(String... params) {
    Log.i(TAG, "doInBackground");
    String url = params[0];
    BasicResponseHandler handler = new BasicResponseHandler();
    HttpGet get = new HttpGet(url);
    try {
        return client.execute(get, handler);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    Log.i(TAG, "onPostExecute");
    pb.setVisibility(View.GONE);
    if (result != null && result.startsWith("[")) {
        Log.i(TAG, "restult = " + result);
        Gson gson = new Gson();
        Type type = new TypeToken<List<MovieCommentsObject>>() {
        }.getType();
        comments = gson.fromJson(result, type);
        init();
    }
}

private void init() {
    for (int i = 0; i < comments.size(); i++) {
        MovieCommentsObject comment = comments.get(i);
        addComment(comment);
    }
    ll.setVisibility(View.VISIBLE);
}

private void addComment(final MovieCommentsObject comment) {

    activity.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            final View v = inflater.inflate(R.layout.adapter_movie_comments, null);
            SmartImageView siv = (SmartImageView) v
                    .findViewById(R.id.siv_adapter_movie_comments);
            WebView wv_user = (WebView) v
                    .findViewById(R.id.wv_adapter_movie_comments_user);
            WebView wv_date = (WebView) v
                    .findViewById(R.id.wv_adapter_movie_comments_date);

            WebView wv_text = (WebView) v
                    .findViewById(R.id.wv_adapter_movie_comments_text);
            TextView tv_type = (TextView) v.findViewById(R.id.tv_adapter_movie_comments_type);
            wv_text.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return true;
                }
            });
            wv_user.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return true;
                }
            });
            wv_date.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return true;
                }
            });

            long date = comment.getInserted();
            if (date > 0) {
                FunctionsGeneral.justify(wv_date, FunctionsGeneral.getTime(date));
            }
            FunctionsGeneral.justify(wv_user, comment.getUser().getUsername());
            FunctionsGeneral.justify(wv_text, comment.getText_html());
            String image = comment.getUser().getAvatar();
            if (image != null && image.trim().length() > 0) {
                siv.setImageUrl(image);
            }
            if(comment.getType() != null){
                tv_type.setText(comment.getType());
            }
            final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.FILL_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            params.bottomMargin = 10;
            ll.addView(v, params);
        }
    });

}

}
公共类MovieCommentsAsynctask扩展异步任务{
私有字符串TAG=“MovieCommentsAsynctask”;
私人语境;
私有布尔检验;
私有HttpClient;
私人名单评论;
私人充气机;
私人活动;
//
私人亲戚;
私人ProgressBar pb;
私有PullToRefreshScrollView sv;
私人线路布局;
专用按钮btn;
公共电影CommentSasyncTask(上下文、相对性),
布尔测试(活动){
this.context=上下文;
这个。活动=活动;
此参数为0.rl=rl;
这个。测试=测试;
client=新的DefaultHttpClient();
bind();
充气器=(充气器)上下文
.getSystemService(上下文布局\充气机\服务);
}
私有无效绑定(){
ll=(LinearLayout)rl.findViewById(R.id.ll\u fragment\u movie\u comments);
sv=(PullToRefreshScrollView)rl
.findViewById(R.id.sv\u片段\u电影\u评论);
pb=(ProgressBar)rl.findViewById(R.id.pb\u片段\u电影\u评论);
btn=(按钮)rl.findViewById(R.id.btn\u片段\u电影\u评论\u添加);
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
Log.i(标记“onPreExecute”);
如果(测试){
pb.setVisibility(View.VISIBLE);
}否则{
}
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
Log.i(标签“doInBackground”);
字符串url=params[0];
BasicResponseHandler=新的BasicResponseHandler();
HttpGet=新的HttpGet(url);
试一试{
返回client.execute(get,handler);
}捕获(客户端协议例外e){
e、 printStackTrace();
返回null;
}捕获(IOE异常){
e、 printStackTrace();
返回null;
}
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
Log.i(标记“onPostExecute”);
pb.setVisibility(View.GONE);
if(result!=null&&result.startsWith(“[”)){
Log.i(标签,“result=”+结果);
Gson Gson=新的Gson();
Type Type=new-TypeToken(){
}.getType();
comments=gson.fromJson(结果,类型);
init();
}
}
私有void init(){
对于(int i=0;i0){
函数general.justify(wv_日期,函数general.getTime(日期));
}
函数general.justify(wv_user,comment.getUser().getUsername());
函数general.justify(wv_text,comment.getText_html());
字符串image=comment.getUser().getAvatar();
if(image!=null&&image.trim().length()>0){
siv.setImageUrl(图像);
}
if(comment.getType()!=null){
tv_type.setText(comment.getType());
}
最终LinearLayout.LayoutParams参数=新的LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL\u父级,
LinearLayout.LayoutParams.WRAP_内容);
参数bottomMargin=10;
ll.addView(v,参数);
}
});
}
}

您实际上是在onPostExecute中创建UI对象,还是只是填充现有视图的数据?在其中一个视图中,我填充gridview,在另一个视图中,我创建视图并将其添加到布局中(已经存在)。你可能想发布一些代码来显示你在做什么。我已经用创建UI对象的Asynctask编辑了我的文章。为什么不在异步任务中创建并返回你的注释列表?如果下载数据后你仍然在UI线程上做繁重的工作,那么异步任务的意义何在?JSON的处理不是很快/很轻您应该在doInBackground调用中这样做