Question

I am implementing tabs in my activity. I have 5 tabs and every tab contains a listview (i get the content of the listview through asynctask) enter image description here

That's the image:

Now Codes:

mViewPager
            .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    ab.setSelectedNavigationItem(position);
                }
            });
    this.getSherlockActivity().getSupportActionBar().removeAllTabs();

    ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    com.actionbarsherlock.app.ActionBar.Tab tab1 = ab
            .newTab()
            .setText("Samsung")
            .setTabListener(
                    new TabListener<SamsungLB>(this.getSherlockActivity(),
                            "tabone", SamsungLB.class));

    com.actionbarsherlock.app.ActionBar.Tab tab2 = ab
            .newTab()
            .setText("HTC")
            .setTabListener(
                    new TabListener<HTCLB>(this.getSherlockActivity(),
                            "tabtwo", HTCLB.class));
    com.actionbarsherlock.app.ActionBar.Tab tab3 = ab
            .newTab()
            .setText("LG")
            .setTabListener(
                    new TabListener<LGLB>(this.getSherlockActivity(),
                            "tabthree", LGLB.class));
    com.actionbarsherlock.app.ActionBar.Tab tab4 = ab
            .newTab()
            .setText("Sony")
            .setTabListener(
                    new TabListener<SonyLB>(this.getSherlockActivity(),
                            "tabfour", SonyLB.class));
    com.actionbarsherlock.app.ActionBar.Tab tab5 = ab
            .newTab()
            .setText("Search")
            .setTabListener(
                    new TabListener<SearchLB>(this.getSherlockActivity(),
                            "tabfive", SearchLB.class));
    ab.addTab(tab1);
    ab.addTab(tab2);
    ab.addTab(tab3);
    ab.addTab(tab4);
    ab.addTab(tab5);

    return rootView;
}

    @SuppressLint("NewApi")
        public static class TabListener<T extends SherlockFragment> implements
                ActionBar.TabListener {
            private Fragment mFragment;
            private final Activity mActivity;
            private final String mTag;
            private final Class<T> mClass;

            public TabListener(Activity activity, String tag, Class<T> clz) {
                mActivity = activity;
                mTag = tag;
                mClass = clz;
            }

            public void onTabSelected(Tab tab, FragmentTransaction ft) {
                if (mFragment == null) {
                    mFragment = Fragment.instantiate(mActivity, mClass.getName());
                    ft.add(android.R.id.content, mFragment, mTag);
                } else {
                    ft.attach(mFragment);
                }
            }

            public void onTabUnselected(Tab tab, FragmentTransaction ft) {
                if (mFragment != null) {
                    ft.detach(mFragment);
                }
            }

            public void onTabReselected(Tab tab, FragmentTransaction ft) {
            }

            public void onTabReselected(Tab arg0,
                    android.app.FragmentTransaction arg1) {

            }

            @SuppressLint("NewApi")
            public void onTabSelected(Tab arg0, android.app.FragmentTransaction arg1) {

                mViewPager.setCurrentItem(arg0.getPosition());
            }

            public void onTabUnselected(Tab arg0,
                    android.app.FragmentTransaction arg1) {

            }

            @Override
            public void onTabSelected(com.actionbarsherlock.app.ActionBar.Tab tab,
                    FragmentTransaction ft) {
                // TODO Auto-generated method stub
                mViewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(
                    com.actionbarsherlock.app.ActionBar.Tab tab,
                    FragmentTransaction ft) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTabReselected(
                    com.actionbarsherlock.app.ActionBar.Tab tab,
                    FragmentTransaction ft) {
                // TODO Auto-generated method stub

            }
        }

Basically, every tab have a listview that fills through AsyncTask. i get the results through JSON And parse them. later on i parse the images through imageloader. Why it's lagging??? Specially when i change from LG TO HTC. (Even there the application closes sometimes!). thanks your help will be appreciated :)

UPDATE: All tabs are using the same layout file.

CODES:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listview1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:drawSelectorOnTop="false" />

Kindly help.

Was it helpful?

Solution

As you are using many listviews, there are multiple causes that can slow down your performance..

  1. How are you using adapter, eg: inflating a new view without using recycled convert view.
  2. Improper layout XML declaration on list item, eg: using very complicated layout.
  3. If you are doing a refresh every time you swipe a page, ViewPager.setOffscreenPageLimit(int limit) can prevent a reload while consuming more memory.

And if you're saying your application closes, you should have some stack trace available, posting it up helps us to identify the error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top