How do I speeding up a click notification from GridView inside a Fragment to an Activity Listener?
-
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(); }
-
Answer:
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.
emil10001 at Stack Overflow Visit the source
Related Q & A:
- How do I make a python web program that is on a ubuntu server allow access to the server?Best solution by Yahoo! Answers
- How do I scroll to a certain widget in a QScrollArea?Best solution by Stack Overflow
- How do I permanently block a user forever? I mean, find or enter the username, when the user isnt on my list?Best solution by Yahoo! Answers
- How do I get audible new mail notification back on yahoo?Best solution by Yahoo! Answers
- How do I apply to a Starbucks inside of another store?Best solution by answers.yahoo.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.