0

I used the FragmentActivity+FragmentTabHost layout, ListView using the universal-image-loader to display the pictures. But I don't know why, the tab when switching will be very slowly and few seconds not responding. So,I've looked everywhere for a solution to this, but nothing.

main activity class:

public class SMMainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sm_index);
        initView();
    }

    private void initView() {
        layoutInflater = LayoutInflater.from(this);
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        int count = fragmentArray.length;

        for (int i = 0; i < count; i++) {
            TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i])
                    .setIndicator(getTabItemView(i));
            mTabHost.addTab(tabSpec, fragmentArray[i], null);
        }
    }

    private View getTabItemView(int index) {
        View view = layoutInflater.inflate(R.layout.tab_indicator, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.icon);
        imageView.setImageResource(mImageViewArray[index]);
        TextView textView = (TextView) view.findViewById(R.id.title);
        textView.setText(mTextviewArray[index]);

        return view;
    }

    private FragmentTabHost mTabHost;
    private Class<?> fragmentArray[] = { SMHomeFragment.class,
            SMCollectionFragment.class, SMShopCarFragment.class,
            SMMeFragment.class };
    private LayoutInflater layoutInflater;
    private int mImageViewArray[] = { R.drawable.indexicon3,
            R.drawable.indexicon4, R.drawable.indexicon2, R.drawable.indexicon };
    public static String mTextviewArray[] = { "index", "other", "shopcar", "me" };
}

Listview adpter class:

public class ProductAdpter extends BaseAdapter {
    public ProductAdpter(Activity activity, List<StoreProduct> products) {
        this.mActivity = activity;
        this.products = products;
        inflater = LayoutInflater.from(activity);
    }

    public int getCount() {
        if(products==null)
            return 0;
        return products.size();
    }

    public StoreProduct getItem(int position) {
        return products.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    private class ViewHolder {
        public ImageView productImageview;
        public TextView productName;
        ... ...
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        final StoreProduct data = products.get(position);
        ViewHolder holder;
        View vi = convertView;
        if (convertView == null){
            vi = inflater.inflate(R.layout.listview_sm_product, null);
            holder = new ViewHolder();
            holder.productImageview = (ImageView) vi
                    .findViewById(R.id.product_imageview);
            holder.productName = (TextView) vi.findViewById(R.id.product_name);
            ... ...
            vi.setTag(holder);
        } else {
            holder = (ViewHolder) vi.getTag();
        }
        ImageLoader.getInstance().displayImage(data.getPicturePath(), holder.productImageview, animateFirstListener);
        holder.productName.setText(data.getName());
        ... ...
        return vi;
    }

    private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {
        static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                }
            }
        }
    }


    private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
    private Activity mActivity;
    private static LayoutInflater inflater = null;
    private List<StoreProduct> products;
}

ImageLoader configuration:

DisplayImageOptions options = new DisplayImageOptions.Builder()
        .showImageOnLoading(R.drawable.ic_stub)
        .showImageForEmptyUri(R.drawable.ic_empty)
        .showImageOnFail(R.drawable.ic_error)
        .cacheInMemory(true)
        .cacheOnDisk(true)
        .considerExifParams(true)
        .build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
        .threadPriority(Thread.NORM_PRIORITY - 2)
        .denyCacheImageMultipleSizesInMemory()
        .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
        .diskCacheSize(50 * 1024 * 1024) // 50 Mb
        .diskCacheFileNameGenerator(new Md5FileNameGenerator())
        .imageDownloader(new BaseImageDownloader(context,5 * 1000, 30 * 1000)) 
        .defaultDisplayImageOptions(options)
        .tasksProcessingOrder(QueueProcessingType.LIFO)
        //.writeDebugLogs()
        .build();
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config);
Tianmu
  • 31
  • 5

1 Answers1

0

Try picasso

A powerful image downloading and caching library for Android http://square.github.io/picasso/

Chetan Gaikwad
  • 1,268
  • 1
  • 13
  • 26
  • i find the reason at http://stackoverflow.com/questions/15500331/fragmenttabhost-performance-is-slow – Tianmu Dec 18 '14 at 09:25
  • this very slowly because that when I am switching tab one to another & another one, that is calling onCreateView() & onActivityCreated() every time. – Tianmu Dec 18 '14 at 09:27