0

3 つのフラグメントを含むアクティビティを読み込もうとしています。フラグメントごとに Asynctask を使用し、それぞれの onPostExecute() メソッドで Ui 要素を読み込みます。問題は、Asynctask が UI 要素をロードしているときにフラグメントからフラグメントにスワイプすると、アプリケーションが非常に遅くなることです。onUiThread() を呼び出してこの問題を解決しようとしましたが、速度が少し低下しました。Asynctasks は、フラグメントの onCreateView メソッドで使用されます。

このラグを取り除くにはどうすればよいですか?

ここに 1 つの Asynctask があります:

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);
        }
    });

}

}
4

0 に答える 0