I'm new to Fragments. I'm trying to develop swipe tabs.
I have the following Adapter:
public class HomeTabsPagerAdapter extends FragmentPagerAdapter {
public HomeTabsPagerAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
@Override
public android.support.v4.app.Fragment getItem(int idx) {
switch (idx) {
case CONSTS_MAIN.TAB_WATCH:
return new WatchFragment();
case CONSTS_MAIN.TAB_DISCOVER:
return new DiscoverFragment();
case CONSTS_MAIN.TAB_LOG:
return new LogFragment();
}
return null;
}
@Override
public int getCount() {
return CONSTS_MAIN.HOME_NO_OF_TABS;
}
}
and MainActivity.java
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
}
and of course, the Fragment classes:
public class WatchFragment extends Fragment {
public class DiscoverFragment extends Fragment {
public class LogFragment extends Fragment {
And basically everything seems to work fine graphically.
However, I'm trying to call method of the Fragment from the Activity. I'm doing it this way:
FragmentManager fm = getSupportFragmentManager();
WatchFragment watchFragment = (WatchFragment) fm.findFragmentById(R.id.watch_fragment);
watchFragment.refresh();
and here fm.findFragmentById
always return null.
I'm using android.support.v4.app
all the way.
How can I get the Fragment class?
EDIT:
Here is the Fragment's layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id ="@+id/watch_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:label="@string/title_activity_watch"
android:orientation="vertical" >
<com.x.y.custom_controls.CustomWatchChronologicalListView
android:id="@+id/lvWatchChronologicalDiscussions"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
and here is the Activity's layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
Use this:
In your adapter add a SparseArray
to keep references to the fragments added
private final SparseArray<Fragment> mPageReferences = new SparseArray<Fragment>();
In your getItem method after instantiate the fragment add it to the array:
@Override
public android.support.v4.app.Fragment getItem(int idx)
{
Fragment fragment = null;
switch (idx)
{
case CONSTS_MAIN.TAB_WATCH:
fragment = new WatchFragment();
case CONSTS_MAIN.TAB_DISCOVER:
fragment = new DiscoverFragment();
case CONSTS_MAIN.TAB_LOG:
fragment = new LogFragment();
}
if(fragment != null) {
mPageReferences.put( position, fragment );
}
return fragment;
}
And add the followings methods:
@Override
public void destroyItem(ViewGroup container, int position, Object object)
{
super.destroyItem(container, position, object);
mPageReferences.remove(position);
}
public SherlockFragment getFragment(int key) {
return mPageReferences.get(key);
}
Finally you can get the fragment in the index you want like this:
Fragment fragment = (Fragment)adapter.getFragment(index);
If you want to use keys to identify the fragments instead int index, you can use a HashMap
Good Luck!
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments