How do I speeding up a click notification from GridView inside a Fragment to an Activity Listener?

StackOverflow https://stackoverflow.com/questions/9490558

سؤال

I am writing an app that has a couple of fragment views managed by an activity. The fragments are basically a GridView with a bunch of images and a details view that is triggered when the user clicks on an image in the GridView.

This all works fine, except that since there is a lot of other stuff going from the activity to the grid fragment (I am continuously adding things to the grid view and displaying them immediately), it takes several seconds for the click to be recognized. I had assumed that building the image would be the time-consuming bit, and that when I click the item, that that event should be sent to the activity immediately, since there isn't any heavy lifting involved. Once the message gets to the activity, the activity will stop updating the grid view, and work on building the details view.

The issue here is that clicking an element in the grid view is taking several seconds (5 to 10 on a slower phone) to register. What do I need to do to speed this action up?

Relevant GridFragment code:

public class GridFragment extends Fragment{
...
public class ImageAdapter extends BaseAdapter  {
    private GridContent gridContent;
    private Context mContext;

    ...
    public View getView(final int position, View convertView, ViewGroup parent) {
        // ImageView is my private holder class
        ImageView imageView;
        imageView = new ImageView(mContext);
        imageView.setImageBitmap(gridContent.get(position).getThumb());

        imageView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // This is what I expect to see immediately upon clicking something, 
                //  but takes several seconds to show up in logcat
                if (Constants.DEBUG){ Log.d(TAG, "an item was clicked  - " 
                    + gridContent.get(position).getId());}
                    //mListener is a listener implemented by my activity
                    // gridContent.get(position) just returns a small object, 
                    //  it shouldn't be doing much work
                    mListener.onItemSelected(gridContent.get(position));
            }

          });

        return imageView;
    }
}
}

Relevant Activity code:

@Override
public void onItemSelected(Item item) {
    // Displays a loading dialog
    showLoadDialog();
    Item pi = item;
    // This builds an image from the web, it might be slow depending on the 
    //  phone's connection
    pi.genImage();
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    hideLoadDialog();
    ft.addToBackStack(Constants.DETAILS_STACK)
    .add(android.R.id.content, DetailsFragment.newInstance(0,pi))
    .commit();
}
هل كانت مفيدة؟

المحلول

I mostly resolved this issue. The resolution was specific to my implementation, however the slowness was caused by the fact that I was doing a lot of processor intensive tasks, some of which were done on the UI thread and some of them were eating up a fair bit of memory.

I was building bitmaps (on a non-ui thread) by pulling the data down from the server and building the image straight in memory, then adding that bitmap to the grid view. What worked better was to use an AsyncTask to download the images to the sd card, then to add those images in by referencing their location on the sd card. Much faster and made my app more responsive.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top