繁体   English   中英

长时间单击后上下文操作栏太慢

[英]Contextual action bar too slow after long click

我有一个活动,它显示存储在sdcard上的图像列表,尽管正在工作,但会产生两种不良情况。

1)关闭进度对话框后,会有大约3秒钟的延迟。 我希望图像在进度对话框消失后立即显示。

2)长时间点击图片后,上下文操作栏花费的时间过长(〜4秒)。

...   
04-26 21:23:03.902  18438-18438/co.glurl.appoid D/dalvikvm﹕ GC_FOR_ALLOC freed 235K, 9% free 26140K/28604K, paused 21ms, total 21ms
04-26 21:23:04.702  18438-18438/co.glurl.appoid D/dalvikvm﹕ GC_FOR_ALLOC freed 21K, 9% free 26453K/28936K, paused 20ms, total 20ms
04-26 21:23:04.712  18438-18438/co.glurl.appoid I/dalvikvm-heap﹕ Grow heap (frag case) to 31.831MB for 4147216-byte allocation
04-26 21:23:04.782  18438-18438/co.glurl.appoid D/dalvikvm﹕ GC_FOR_ALLOC freed 17K, 8% free 30519K/32988K, paused 17ms, total 18ms
04-26 21:23:05.362  18438-18438/co.glurl.appoid I/Choreographer﹕ Skipped 301 frames!  The application may be doing too much work on its main thread.
04-26 21:23:09.902  18438-18438/co.glurl.appoid D/dalvikvm﹕ GC_FOR_ALLOC freed 11106K, 35% free 24621K/37752K, paused 18ms, total 18ms
04-26 21:23:11.452  18438-18438/co.glurl.appoid D/dalvikvm﹕ GC_FOR_ALLOC freed 157K, 30% free 26459K/37752K, paused 18ms, total 18ms
04-26 21:23:12.072  18438-18438/co.glurl.appoid I/Choreographer﹕ Skipped 291 frames!  The application may be doing too much work on its main thread.

码:

public class PhotosGridViewActivity extends Activity {

private PhotosUtils utils;
private ArrayList<String> imagePaths = new ArrayList<String>();
private PhotosGridViewImageAdapter adapter;
private GridView gridView;
private int columnWidth;
private int items;
private ProgressDialog progressDialog;
private boolean setEditableMode = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.photos_grid_view);
    //Initialize a LoadViewTask object and call the execute() method
    new LoadViewTask().execute();
}

//To use the AsyncTask, it must be subclassed
private class LoadViewTask extends AsyncTask<Void, Integer, Void> {
    //Before running code in the separate thread
    @Override
    protected void onPreExecute() {
        //Create a new progress dialog
        progressDialog = new ProgressDialog(PhotosGridViewActivity.this);
        progressDialog.setMessage("Loading...");
        progressDialog.show();
    }

    //The code to be executed in a background thread.
    @Override
    protected Void doInBackground(Void... params) {
        // Initilizing Grid View
        InitilizeGridLayout();
        // loading all image paths from SD card
        imagePaths = utils.getFilePaths();
        // Gridview adapter
        adapter = new PhotosGridViewImageAdapter(PhotosGridViewActivity.this, imagePaths,
                columnWidth);
        try {
            //Get the current thread's token
            synchronized (this) {
                //Initialize an integer (that will act as a counter) to zero
                int counter = 0;
                //While the counter is smaller than four
                while(counter <= 4) {
                    //Wait 850 milliseconds
                    this.wait(1000);
                    //Increment the counter
                    counter++;
                    //Set the current progress.
                    //This value is going to be passed to the onProgressUpdate() method.
                    publishProgress(counter*25);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    //Update the progress
    @Override
    protected void onProgressUpdate(Integer... values) {
        //set the current progress of the progress dialog
        progressDialog.setProgress(values[0]);
    }

    //after executing the code in the thread
    @Override
    protected void onPostExecute(Void result){
        //close the progress dialog
       try {
            progressDialog.dismiss();
            //initialize the View
        } catch (Exception e) {
            e.printStackTrace();
        }
        // setting grid view adapter
        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                gridView.getItemAtPosition(position);
                    Intent i = new Intent(PhotosGridViewActivity.this, PhotosScreenViewActivity.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    i.putExtra("position", position);
                    PhotosGridViewActivity.this.startActivity(i);
            }
        });
    }
}

private void InitilizeGridLayout() {
    utils = new PhotosUtils(PhotosGridViewActivity.this);
    gridView = (GridView) findViewById(R.id.grid_view);
    Resources r = getResources();
    float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
            AppConstant.GRID_PADDING, r.getDisplayMetrics());

    columnWidth = (int) ((utils.getScreenWidth() - ((AppConstant.NUM_OF_COLUMNS + 1) * padding)) / AppConstant.NUM_OF_COLUMNS);


    gridView.setNumColumns(AppConstant.NUM_OF_COLUMNS);
    gridView.setColumnWidth(columnWidth);
    gridView.setStretchMode(GridView.NO_STRETCH);
    gridView.setPadding((int) padding, (int) padding, (int) padding,
            (int) padding);
    gridView.setHorizontalSpacing((int) padding);
    gridView.setVerticalSpacing((int) padding);

    gridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
    gridView.setMultiChoiceModeListener(new MultiChoiceModeListener());
}

public class MultiChoiceModeListener implements
        GridView.MultiChoiceModeListener {
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        mode.setTitle("Select Items");
        mode.setSubtitle("One item selected");
        return true;
    }

    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return true;
    }

    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        return true;
    }

    public void onDestroyActionMode(ActionMode mode) {
    }

    public void onItemCheckedStateChanged(ActionMode mode, int position,
                                          long id, boolean checked) {
        int selectCount = gridView.getCheckedItemCount();
        switch (selectCount) {
            case 1:
                mode.setSubtitle("One item selected");
                break;
            default:
                mode.setSubtitle("" + selectCount + " items selected");
                break;
        }
    }
}

}

怎么了?

问题在这里

  while(counter <= 4) {
                    //Wait 1 second <-this means 5 seconds you will wait because it will be repeated 5 times
                    this.wait(1000); //edit this or remove it
                    //Increment the counter
                    counter++;
                    //Set the current progress.
                    //This value is going to be passed to the onProgressUpdate() method.
                    publishProgress(counter*25);
                }

好的...在这里我无济于事,我用Google搜索了一下,发现有必要异步加载getView()。 进一步参考。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM