Daniel Dias
Daniel Dias

Reputation: 65

Android FragmentActivity How to refresh fragment when it become visible

I'm developing an application for Android and need some help implementing Tabs

I need it to be compatible with android 2.2
I started to implement Tabs but now i dont know if it was the best idea

What I want is basically:

I want to open a activity where it will have the tabs
In each of the Tab, I want to load content dinamicly, like the play store apk:

Play Store Tabs

My main problem is:

I have created a AsyncTask with an ProgressDialog for each fragment to load the data and show it on the fragment, but when the fragment activity open, it initialize all the tabs and makes to load all the fragments at the same time.

I want to load the Tab content only when the Tab becomes visible, because the user may want to see only one tab, so no need to get the other Tabs

How should I do that? Should I use TabFragments or others methods?

My current Tab activiy is:

public class Tabs2 extends FragmentActivity implements OnTabChangeListener, OnPageChangeListener{
ViewPagerAdapter pageAdapter;
private ViewPager mViewPager;
private TabHost mTabHost;
private String interID;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Infla o layout
    setContentView(R.layout.tablayout);

    mViewPager = (ViewPager) findViewById(R.id.viewpager);

    // Tab Initialization
    initialiseTabHost();

    // Fragments and ViewPager Initialization
    List<Fragment> fragments = getFragments();
    pageAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragments);
    mViewPager.setAdapter(pageAdapter);
    mViewPager.setOnPageChangeListener(Tabs2.this);

}

// Method to add a TabHost
private static void AddTab(Tabs2 activity, TabHost tabHost, TabHost.TabSpec tabSpec) {
    tabSpec.setContent(activity.new MyTabFactory(activity));
    tabHost.addTab(tabSpec);
}

// Manages the Tab changes, synchronizing it with Pages
public void onTabChanged(String tag) {
    int pos = this.mTabHost.getCurrentTab();
    this.mViewPager.setCurrentItem(pos);
}

// Manages the Page changes, synchronizing it with Tabs
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
    int pos = this.mViewPager.getCurrentItem();
    this.mTabHost.setCurrentTab(pos);
}

@Override
public void onPageScrollStateChanged(int arg0) {
}

@Override
public void onPageSelected(int arg0) {
}

private List<Fragment> getFragments(){

    List<Fragment> fList = new ArrayList<Fragment>();

    // Put here your Fragments
    fList.add(Fragment.instantiate(this, TabFragmentA.class.getName()));
        fList.add(Fragment.instantiate(this, TabFragmentB.class.getName()));
    fList.add(Fragment.instantiate(this, TabFragmentC.class.getName()));

    return fList;
}

// Tabs Creation
private void initialiseTabHost() {
    mTabHost = (TabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup();
    // Put here your Tabs
    Tabs2.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab1").setIndicator("Detalhes"));
    Tabs2.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab2").setIndicator("Comentários"));
    Tabs2.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab3").setIndicator("cenas"));
    mTabHost.setOnTabChangedListener(this);
}

// Um simples factory que retorna View para o TabHost
class MyTabFactory implements TabContentFactory {

    private final Context mContext;

    public MyTabFactory(Context context) {
        mContext = context;
    }

    public View createTabContent(String tag) {
        View v = new View(mContext);
        v.setMinimumWidth(0);
        v.setMinimumHeight(0);
        return v;
    }
}

}

The ViewPagerAdatper:

public class ViewPagerAdapter extends FragmentPagerAdapter {
    private List<Fragment> mFragments;

    public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        mFragments = fragments;
    }

    @Override
    public Fragment getItem(int i) {  
        return mFragments.get(i);
    }

    @Override 
    public int getCount() {
        return mFragments.size();
    }

}

The XML for the Tabs activiy

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
android:layout_width="fill_parent"  
android:layout_height="fill_parent"  
android:orientation="vertical" >

<TabHost 
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="35dp"
            android:orientation="horizontal"
            android:gravity="bottom"
            android:padding="0dip" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:isScrollContainer="true"
            android:scrollbars="vertical" />

    </LinearLayout>
</TabHost>

For each Tab I used Fragments

public class TabFragmentA extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (container == null) {
            return null;
        }
        // HERE I Call the AsyncTask and return the data with the layout
        return (RelativeLayout) inflater.inflate(R.layout.layout_a, container, false);
    }
}

Upvotes: 0

Views: 4902

Answers (1)

tyczj
tyczj

Reputation: 73721

since you are using a viewpager this is how a viewpager works and for good reason. A view pager by default loads the previous, current and next tab so that the content is ready when the user gets to the tab. you cannot change the viewpager to only load one page at a time.

If you notice this is exactly how the play store works

Upvotes: 4

Related Questions