I try to put a tab host inside a frament but when run the code , the tabhost can not display
java code of class extend fragment :
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_host_layout,
container, false);
setHasOptionsMenu(true);
TabHost mTabHost = (TabHost) rootView.findViewById(R.id.tabhost);
TabSpec photospec = mTabHost.newTabSpec("Search");
photospec.setIndicator("Search", getResources().getDrawable(R.drawable.action_search));
Intent searchIntent = new Intent(getActivity(), HostViewIntroduce.class);
photospec.setContent(searchIntent);
TabSpec songspec = mTabHost.newTabSpec("Favorites");
// setting Title and Icon for the Tab
songspec.setIndicator("Favorites", getResources().getDrawable(R.drawable.action_search));
Intent favoriteIntent = new Intent(getActivity(), HostViewTips.class);
songspec.setContent(favoriteIntent);
return rootView;
}
xml code :
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativeLayout1"
android:layout_marginRight="5dip"
android:background="#drawable/image_bg"
android:padding="3dip" >
<ImageView
android:id="#+id/userAvatar"
android:layout_width="wrap_content"
android:layout_height="89dp"
android:src="#drawable/test_avatar" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/thumbnail"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/thumbnail"
android:layout_below="#+id/relativeLayout1"
android:layout_toRightOf="#+id/thumbnail"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="soloco bolo"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:src="#drawable/description_section_icon" />
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="123456"
android:layout_weight="1"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:src="#drawable/ic_menu_add" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="123dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/thumbnail" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="Đà Nẵng"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="68dp"
android:gravity="center_vertical"
android:text="Activate"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="6/1/2013-7/1/2013"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/relativeLayout2"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:orientation="vertical" >
<TabHost
android:id="#+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.30" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="92dp" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/tab1list_place_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/tab2list_place_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/tab3list_place_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
and here is the screen shot :
and here is exactly what I want it to be
https://dl.dropboxusercontent.com/u/37599516/Untitled.png
Try this:
public class AndroidViewPagerActivity extends FragmentActivity {
ViewPager mViewPager;
TabsAdapter mTabsAdapter;
public void setActionBarTitle(String title) {
getActionBar().setTitle(title);
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);
setContentView(mViewPager);
final ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
mTabsAdapter = new TabsAdapter(this, mViewPager);
mTabsAdapter.addTab(bar.newTab().setText("Header"),
FragmentSales1.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Detail"),
Fragment2.class, null);
if (savedInstanceState != null) {
bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
//super.onSaveInstanceState(outState);
outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
}
public static class TabsAdapter extends FragmentPagerAdapter
implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
static final class TabInfo {
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args) {
clss = _class;
args = _args;
}
}
public TabsAdapter(FragmentActivity activity, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mActionBar = activity.getActionBar();
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mActionBar.addTab(tab);
notifyDataSetChanged();
}
#Override
public void onPageScrollStateChanged(int state) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// TODO Auto-generated method stub
}
#Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
mActionBar.setSelectedNavigationItem(position);
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Object tag = tab.getTag();
for (int i=0; i<mTabs.size(); i++) {
if (mTabs.get(i) == tag) {
mViewPager.setCurrentItem(i);
}
}
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
#Override
public int getCount() {
return mTabs.size();
}
}
public class TabsPagerAdapterForSalesOrder extends FragmentPagerAdapter {
public TabsPagerAdapterForSalesOrder(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Header fragment activity
return new FragmentSalesOrderHeader();
case 1:
// Detail fragment activity
return new FragmentSalesOrderDetail();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 2;
}
}
Update
tab.xml
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
Note:
Create your two layout for each tab.
Hope this helps.
Related
Tabhost + viewpager horizontal scroll. if the fragment is empty or contains a textview, it works, but if the fragment contains a list view,, fragment it's doesn't scroll horizontal !!
if I try to scroll from the textview it works or if the fragment is empty it's work , but from the listview no, noted that the textview its width and height are wrap_content while those in the listview are match_parent
frgament 2 (it contains the fragment of tabhost )
fragment.XML
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/main_content1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RESEAU"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"/>
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#000000" />
<HorizontalScrollView
android:id="#+id/hScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
</HorizontalScrollView>
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</FrameLayout>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
fragment2.java
public class Fragment2 extends android.support.v4.app.Fragment implements OnTabChangeListener, OnPageChangeListener {
private FragmentTabHost mTabHost;
private ViewPager viewPager;
private MyFragmentPagerAdapter myViewPagerAdapter;
View v;
public Fragment2() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment2, container, false);
viewPager = (ViewPager) v.findViewById(R.id.pager);
// init tabhos
this.initializeTabHost(savedInstanceState);
// init ViewPager
this.initializeViewPager();
return v;
}
// fake content for tabhost
class FakeContent implements TabHost.TabContentFactory {
private final Context mContext;
public FakeContent(Context context) {
mContext = context;
}
#Override
public View createTabContent(String tag) {
View v = new View(mContext);
v.setMinimumHeight(0);
v.setMinimumWidth(0);
return v;
}
}
private void initializeViewPager() {
List<android.support.v4.app.Fragment> fragments = new Vector<android.support.v4.app.Fragment>();
fragments.add(new TramHor());
fragments.add(new BusHor());
fragments.add(new Train());
this.myViewPagerAdapter = new MyFragmentPagerAdapter(
getChildFragmentManager(), fragments);
this.viewPager = (ViewPager) v.findViewById(R.id.pager);
this.viewPager.setAdapter(this.myViewPagerAdapter);
this.viewPager.setOnPageChangeListener(this);
}
private void initializeTabHost(Bundle args) {
mTabHost = (FragmentTabHost) v.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("", getResources().getDrawable(R.drawable.tram)),
TramHor.class, null);
mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("", getResources().getDrawable(R.drawable.bus)),
BusHor.class, null);
mTabHost.addTab(mTabHost.newTabSpec("fragmentd").setIndicator("", getResources().getDrawable(R.drawable.train)),
Train.class, null);
mTabHost.setOnTabChangedListener(this);
}
public void onTabChanged(String tabId) {
int pos = this.mTabHost.getCurrentTab();
this.viewPager.setCurrentItem(pos);
HorizontalScrollView hScrollView = (HorizontalScrollView) v.findViewById(R.id.hScrollView);
View tabView = mTabHost.getCurrentTabView();
int scrollPos = tabView.getLeft()
- (hScrollView.getWidth() - tabView.getWidth()) / 2;
hScrollView.smoothScrollTo(scrollPos, 0);
}
public void onPageScrollStateChanged(int arg0) {
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
public void onPageSelected(int position) {
this.mTabHost.setCurrentTab(position);
}
}
MyFragmentPageAdapter.java
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
List<android.support.v4.app.Fragment> fragments;
public MyFragmentPagerAdapter(FragmentManager fm, List<android.support.v4.app.Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
#Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
}
a fragment of tabhost
<LinearLayout
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:orientation="vertical"
tools:context="com.example.getgpslocation.fragment.TramHor">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LIGNE/DIRECTION"
android:layout_marginTop="70dp"
android:layout_marginLeft="20dp"/>
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#000000"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lv_sliding_menu2"
android:background="#FFFFFF"
android:choiceMode="singleChoice"
android:layout_gravity="start"/>
</LinearLayout>
I found the solution, viewpager in xml was not in the right place
<LinearLayout
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"
tools:context="com.example.getgpslocation.fragment.Fragment2"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/main_content1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RESEAU"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"/>
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#000000" />
<HorizontalScrollView
android:id="#+id/hScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none"/>
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#+id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
This is my current scenario:
I have 4 Tabsin my acivity. I am calling a new API to load data from the server on each tab. The problem is that the correct fragment is not loading on the correct index. I have googled here and here but the solutions provided there don't help me.My codes are as follows:
public class OffersTab extends FragmentActivity implements OnTabChangeListener,
OnPageChangeListener {
private TabsPagerAdapter mAdapter;
private ViewPager mViewPager;
private TabHost mTabHost;
ImageView imv_home;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.offers);
imv_home = (ImageView) findViewById(R.id.imv_home);
imv_home.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
mViewPager = (ViewPager) findViewById(R.id.viewpager);
// Tab Initialization
initialiseTabHost();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
// Fragments and ViewPager Initialization
mViewPager.setAdapter(mAdapter);
mViewPager.setOnPageChangeListener(OffersTab.this);
}
// Method to add a TabHost
private static void AddTab(OffersTab activity, TabHost tabHost,
TabHost.TabSpec tabSpec) {
tabSpec.setContent(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);
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
// 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 onPageSelected(int arg0) {
}
// Tabs Creation
private void initialiseTabHost() {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
// TODO Put here your Tabs
OffersTab.AddTab(this, this.mTabHost, this.mTabHost
.newTabSpec("Ladies").setIndicator("Ladies"));
OffersTab.AddTab(this, this.mTabHost, this.mTabHost
.newTabSpec("Babies").setIndicator("Babies"));
OffersTab.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Girls")
.setIndicator("Girls"));
OffersTab.AddTab(
this,
this.mTabHost,
this.mTabHost.newTabSpec("Accessories").setIndicator(
"Accessories"));
mTabHost.setOnTabChangedListener(this);
}
}
Adapter class
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem( int index) {
Log.i("index", "" + index);
switch (index) {
case 0:
// Top Rated fragment activity
return new LadiesFragment();
case 1:
// Games fragment activity
return new BabyFragment();
case 2:
// Movies fragment activity
return new GirlsFragment();
case 3:
// Movies fragment activity
return new AccesoryFragment();
default:
break;
}
return new LadiesFragment();
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 4;
}
}
xml layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/yyy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/rel_top_header_exp"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:background="#color/orange" >
<LinearLayout
android:id="#+id/lnr_top_home"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:gravity=""
android:weightSum="3" >
<ImageView
android:id="#+id/imv_srch_home"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_gravity="center_vertical"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:src="#drawable/search_icon" />
<ImageView
android:id="#+id/imv_wish_home"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_gravity="center_vertical"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:src="#drawable/wish_list_icon" />
<ImageView
android:id="#+id/imv_cart_home_exp"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:src="#drawable/my_bag_icon" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/imv_home"
android:text="#string/app_name"
android:textSize="#dimen/text16" />
<ImageView
android:id="#+id/imv_home"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:src="#drawable/back" />
</RelativeLayout>
</LinearLayout>
<TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/yyy" >
<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="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<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="wrap_content"
android:layout_gravity="bottom" />
</LinearLayout>
</TabHost>
</RelativeLayout>
On your fragment/activity implementing the TabLayout.OnTabSelectedListener, use this:
#Override
public void onTabSelected(TabLayout.Tab tab) {
pager.setCurrentItem(tab.getPosition());
}
I have custom dialog to show which has instruction about how to use the app.I am using ViewPager for this inside my custom dialog layout.
I am getting error
java.lang.IllegalArgumentException: No view found for id for fragment FragmentForInstruction1.
I have created a method which is called in onCreate(Bundle savedInstanceState) {} method .The method inflates the layout for dialog.
private void showCustomDialogForInstruction() {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogLayout = inflater.inflate(R.layout.dialog_layout_for_instruction_message, null, false);
layout.setAlpha(0.2f);
dialog.setCanceledOnTouchOutside(false);
dialog.setContentView(dialogLayout);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
ViewPager viewPager=(ViewPager) dialog.findViewById(R.id.pagerInstruction);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new FragmentForInstruction1());
adapter.addFrag(new Fragme`enter code here`ntForWelcomePage2());
adapter.addFrag(new FragmentForWelcomePage3());
adapter.addFrag(new FragmentForWelcomePage4());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new OnPageChangeListener()
{
#Override
public void onPageSelected(int pos)
{
}
#Override
public void onPageScrolled(int pos, float arg1, int arg2)
{
}
#Override
public void onPageScrollStateChanged(int arg0)
{
}
});
Button done = (Button) dialog.findViewById(R.id.done);
done.setText("Got It");
done.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
Button neverShow = (Button) dialog.findViewById(R.id.nevershow);
neverShow.setText("Never Show Again");
neverShow.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return false;
}
});
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.show();
}
my view pager class:
public class ViewPagerAdapter extends FragmentPagerAdapter
{
private final List<Fragment> mFragmentList = new ArrayList<Fragment>();
public ViewPagerAdapter(FragmentManager manager)
{
super(manager);
}
#Override
public Fragment getItem(int position)
{
return mFragmentList.get(position);
}
#Override
public int getCount()
{
return mFragmentList.size();
}
public void addFrag(Fragment fragment)
{
mFragmentList.add(fragment);
}
}
and i call the method in MainActivity :
public class MainActivity extends AppCompatActivity {
SharedPreferences sharedPreferencesNeverShowAgain ;
boolean neverShowAgain;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPreferencesNeverShowAgain = PreferenceManager.getDefaultSharedPreferences(this);
neverShowAgain = sharedPreferencesNeverShowAgain.getBoolean("NeverShowAgain", false);
if(!neverShowAgain){
showCustomDialogForMessage();
}
}
}
and the layout for dialog is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/dialog_shape"
android:orientation="vertical" >
<!-- layout title -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#00000000"
android:gravity="center"
android:orientation="vertical" >
<ViewFlipper
android:id="#+id/flipper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/top_edge_rounded"
android:flipInterval="2000"
android:padding="20dp" >
<TextView
android:id="#+id/dialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="center"
android:text="Input Height"
android:textColor="#color/textColor"
android:textStyle="normal" />
<TextView
android:id="#+id/title1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="center"
android:text="Input Height"
android:textColor="#color/accent"
android:textStyle="normal" />
</ViewFlipper>
<View
android:id="#+id/tri"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/triangle"
android:rotation="180" />
</LinearLayout>
<!-- layout dialog content -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="300dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<android.support.v4.view.ViewPager
android:id="#+id/pagerInstruction"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/layoutIndicater"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>
</ScrollView>
<!-- layout dialog buttons -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_margin="10dp"
android:background="#drawable/all_rounded_edge_plum_for_dialog" >
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentTop="true"
android:background="#color/textColor" />
<View
android:id="#+id/ViewColorPickerHelper"
android:layout_width="1dp"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#color/textColor" />
<Button
android:id="#+id/nevershow"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="#id/ViewColorPickerHelper"
android:background="?android:attr/selectableItemBackground"
android:padding="5dp"
android:text="Close"
android:textColor="#color/textColor" />
<Button
android:id="#+id/done"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/ViewColorPickerHelper"
android:background="?android:attr/selectableItemBackground"
android:padding="5dp"
android:text="Done"
android:textColor="#color/textColor" />
</RelativeLayout>
</LinearLayout>
FragmentForInstruction1 code:
public class FragmentForInstruction1 extends Fragment{
#Override
#Nullable
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragmentinstructionpage1, container,false);
return view;
}
}
and its layout is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/backgroundColor"
android:gravity="center" >
<TextView
android:id="#+id/titleWelcomeFragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#color/colorPrimary"
android:textSize="30sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/messageWelcomeFragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#color/colorPrimary"
android:textSize="20sp"
android:textStyle="normal"
android:paddingLeft="20dp"
android:paddingRight="20dp"/>
</LinearLayout>
You should create an adapter that extends FragmentPagerAdapter:
private class CustomPagerAdapter extends FragmentPagerAdapter {
public CustomPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FragmentForInstruction1();
case 1:
return new FragmentForInstruction2();
case 2:
return new FragmentForInstruction3();
case 3:
return new FragmentForInstruction4();
default:
return new FragmentForInstruction1();
}
}
#Override
public int getCount() {
return 4;
}
}
While crating an instance of it, pass the getChildFragmentManager() instead of getSupportFragmentManager():
viewPager.setAdapter(new CustomPagerAdapter(getChildFragmentManager()));
It is working for my search activity page but not for my profile activity: The indicator light under the selected tab, do I have to add something
HEre is the code:
public class ProfileActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
String[] tabTitles = {"Completed", "Joined"};
adapter = new TabAdapter(getSupportFragmentManager(), profileCompletedFrag, profileJoinedFrag, tabTitles);
// // Initialize the ViewPager and set an adapter
pager = (ViewPager) findViewById(R.id.profilePager);
pager.setAdapter(adapter);
// Bind the tabs to the ViewPager
tabs = (PagerSlidingTabStrip) findViewById(R.id.profileTabs);
tabs.setViewPager(pager);
/**
* on swiping the viewpager make respective tab selected
* */
tabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
if (position == 0) {
currentPage = 0;
pager.setCurrentItem(0);
}
if (position == 1) {
currentPage = 1;
pager.setCurrentItem(1);
}
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
/////////////
tabs.setOnTabReselectedListener(new PagerSlidingTabStrip.OnTabReselectedListener() {
#Override
public void onTabReselected(int position) {
Toast.makeText(ProfileActivity.this, "Tab reselected: " + position, Toast.LENGTH_SHORT).show();
if (position == 0) {
currentPage = 0;
pager.setCurrentItem(0);
}
if (position == 1) {
currentPage = 1;
pager.setCurrentItem(1);
}
}
});
He is the xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="#+id/profileParentLayout">
<LinearLayout
android:orientation="horizontal"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/profileNameLayout"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/profileName"
android:layout_centerInParent="true"
android:text="Bob Marley"/>
</LinearLayout>
<RelativeLayout
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginBottom="10dp"
android:layout_centerHorizontal="true"
android:id="#+id/profileImageLayout"
android:layout_below="#+id/profileNameLayout">
<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:id="#+id/profileImage"
android:src="#drawable/camera_w"
android:layout_centerInParent="true"/>
<ImageView android:id="#+id/editProfileImage"
android:layout_height="30dp"
android:layout_width="30dp"
android:src="#drawable/ic_launcher"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginBottom="10dp"
android:id="#+id/profileDetailsLayout"
android:layout_below="#id/profileImageLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/profileFollowers"
android:text="13 followers"
android:textSize="20dp"/>
<View android:id="#+id/separator"
android:layout_marginLeft="5dip"
android:background="#ffffff"
android:layout_width = "1dip"
android:layout_height="fill_parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/profileFollowing"
android:text="45 following"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textSize="20dp"
/>
<View android:id="#+id/separator2"
android:layout_marginRight="5dip"
android:background="#ffffff"
android:layout_width = "1dip"
android:layout_height="fill_parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/profilePoints"
android:text="124 points"
android:textSize="20dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="?attr/actionBarSize"
android:orientation="horizontal"
android:id="#+id/tabsLayout"
android:layout_below="#+id/profileDetailsLayout">
<!-- for Tabs -->
<com.astuetz.PagerSlidingTabStrip
android:id="#+id/profileTabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:pstsShouldExpand="true"
android:background="?attr/colorPrimary"/>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/profilePager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
</LinearLayout>
<GridView
android:layout_below="#+id/tabsLayout"
android:id="#+id/profileGridView"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth" >
</GridView>
</RelativeLayout>
public class TabAdapter extends FragmentStatePagerAdapter {
String[] tabTitles = {"Completed", "Joined"};
public TabAdapter(FragmentManager fragmentManager) {
// TODO Auto-generated constructor stub
super(fragmentManager);
}
#Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return tabTitles[position];
}
#Override
public Fragment getItem(int arg0) {
switch (arg0) {
case 0:
return ProfileDataFagment.newInstance(0);
case 1:
return ProfileDataFagment.newInstance(1);
default:
break;
}
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 2; // Number of views
}
}
// Also create ProfileDataFagment and set you data according to your position
Hi i'm new to android development i need to show to fragment views in one activity. i need to show this activity immediately after launching app. Please find the attached screen. For your reference, i have added my both XML layouts below :
XML - 1
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white"
android:fillViewport="true"
android:scrollbars="none" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/tvSubtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/grey_dark"
android:gravity="center"
android:padding="15dp"
android:text="Day One"
android:textColor="#color/white"
android:textSize="16sp"
android:typeface="monospace" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/grey_line_border" />
<RelativeLayout
android:id="#+id/rlQuestion1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_selector"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<TextView
android:id="#+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center"
android:text="09:00 - 09:15"
android:textColor="#drawable/list_text_top_color"
android:textSize="12sp"
android:textStyle="bold"
android:typeface="monospace" />
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/time"
android:gravity="center"
android:maxLines="2"
android:padding="2dp"
android:text="Introduction & Briefing"
android:textAllCaps="true"
android:textColor="#drawable/list_text_color"
android:textSize="16sp"
android:typeface="monospace" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/grey" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
XML - 2
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white"
android:fillViewport="true"
android:scrollbars="none" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/tvSubtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/grey_dark"
android:text="Thank you for attending this event.Kindly take a few minutes to share your feedback with us.Please fill in your contact details clearly or attach your business card.Kindly submit this form at the end of the event."
android:textColor="#color/white"
android:textSize="12sp"
android:typeface="monospace" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/grey_line_border" />
<com.andreabaccega.widget.FormEditText
android:id="#+id/etUserId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/transparent"
android:ems="10"
android:hint="Name(Mr/Ms/Mdm/Dr)"
android:inputType="text"
android:padding="12dp"
android:textColor="#color/blue"
android:textColorHint="#color/grey_dark"
android:textSize="16sp"
android:typeface="monospace" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/grey_line_border" />
<com.andreabaccega.widget.FormEditText
android:id="#+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/transparent"
android:ems="10"
android:hint="Company"
android:inputType="text"
android:padding="12dp"
android:textColor="#color/blue"
android:textColorHint="#color/grey_dark"
android:textSize="16sp"
android:typeface="monospace" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/grey_line_border" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
here is my code of main activity that i have tried :
package com.pmg.eventapp;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.pmg.eventapp.R;
import com.viewpagerindicator.TabPageIndicator;
public class MainActivity extends SherlockFragmentActivity {
private static final String[] CONTENT = new String[] { " AGENDA ",
" FEEDBACK " };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_main);
FragmentPagerAdapter adapter = new GoogleMusicAdapter(
getSupportFragmentManager());
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
indicator.setViewPager(pager);
}
#Override
public boolean onOptionsItemSelected(
com.actionbarsherlock.view.MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; finish activity to go home
finish();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onResume() {
super.onResume();
// Set title
getSupportActionBar().setTitle("EVENT APP");
}
class GoogleMusicAdapter extends FragmentPagerAdapter {
public GoogleMusicAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
// Voting
return new AgendaFragment();
case 1:
// QA
return new FeedbackFragment();
default:
return null;
}
}
#Override
public CharSequence getPageTitle(int position) {
return CONTENT[position % CONTENT.length].toString();
}
#Override
public int getCount() {
return CONTENT.length;
}
}
}
Please help me to create remaining two fragments.Thanks!
This is container where your fragment will be shown : main_container_activity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<com.astuetz.viewpager.extensions.PagerSlidingTabStrip
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip"
android:background="#drawable/background_tab" />
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tabs" />
</RelativeLayout>
</LinearLayout>
lets say it is fragment : fragment_activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<ProgressBar
android:id="#+id/progressBar_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
here is your Container which contains two fragment lets say : MainContainer
public class MainContainer extends SherlockFragmentActivity{
private PagerSlidingTabStrip tabs;
ViewPager mViewPager;
AppSectionsPagerAdapter mAppSectionsPagerAdapter;
public static double screenInches;
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.main_container_activity);
tabs = (PagerSlidingTabStrip)findViewById(R.id.tabs);
tabs.setIndicatorColorResource(R.color.color_website);
tabs.setShouldExpand(true);
mViewPager = (ViewPager)findViewById(R.id.pager);
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager(), MainContainer.this);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
tabs.setViewPager(mViewPager);
mViewPager.setOffscreenPageLimit(1);
}
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
Context privatecontext;
public AppSectionsPagerAdapter(FragmentManager fm, Context context) {
super(fm);
privatecontext = context;
}
#Override
public Fragment getItem(int i) {
switch (i) {
case 0:
Fragment fragment = new FragmentActivity();
return fragment;
case 1:
Fragment fragment1 = new FragmentTagsActivity();
return fragment1;
default:
return new FragmentActivity();
}
}
#Override
public int getCount() {
return 2;
}
public CharSequence getPageTitle(int position) {
String name = null;
if (position==0) {
name = privatecontext.getString(R.string.main_tab1);
} else if (position==1) {
name = privatecontext.getString(R.string.main_tab2);
}
return name;
}
}
}
FragmentActivity.java
public class FragmentTagsActivity extends SherlockFragment {
ProgressBar progressBar;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_activity, container, false);
return rootView;
}
}