TabHost Viewpager listview doesn't scroll - android

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>

Related

ViewPager with FragmentPagerAdapter Not showing Content

I am trying to use Fragment
This is my activity class:
public class InterfaceAct extends FragmentActivity implements View.OnClickListener {
private ViewPager mViewPager;
private List<Fragment> datas;
private ViewPagerFragmentAdapter viewPagerFragmentAdapter;
private LinearLayout mLLHome,mLLTimer,mLLEdit,mLLAbout;
private ImageView mImageViewHome,mImageViewTimer,mImageViewEdit,mImageViewAbout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activiy_interface);
initDatas();// Init Data For Fragments
initView();// Init Component
initEvent();// Sign for Click Listener
viewPagerFragmentAdapter=new ViewPagerFragmentAdapter(getSupportFragmentManager(),datas);// Init Adpater Class
mViewPager.setAdapter(viewPagerFragmentAdapter);
}
private void initDatas() {
datas=new ArrayList<Fragment>();
datas.add(new MyFragment1());
datas.add(new MyFragment2());
datas.add(new MyFragment3());
datas.add(new MyFragment4());
}
private void initEvent() {
mLLHome.setOnClickListener(this);
mLLTimer.setOnClickListener(this);
mLLEdit.setOnClickListener(this);
mLLAbout.setOnClickListener(this);
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {//ViewPager Scrolling Switch Listner
#Override
public void onPageSelected(int arg0) {
int currentItem=mViewPager.getCurrentItem();
resetImag();
switch (currentItem) {
case 0:
mImageViewHome.setImageResource(R.drawable.home_yes);
break;
case 1:
mImageViewTimer.setImageResource(R.drawable.timer_yes);
break;
case 2:
mImageViewEdit.setImageResource(R.drawable.edit_yes);
break;
case 3:
mImageViewAbout.setImageResource(R.drawable.about_yes);
break;
default:
break;
}
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
private void initView() {
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mLLHome = (LinearLayout) findViewById(R.id.ll_home);
mLLTimer = (LinearLayout) findViewById(R.id.ll_timer);
mLLEdit = (LinearLayout) findViewById(R.id.ll_edit);
mLLAbout = (LinearLayout) findViewById(R.id.ll_about);
mImageViewHome = (ImageView) findViewById(R.id.img_home);
mImageViewTimer = (ImageView) findViewById(R.id.img_timer);
mImageViewEdit = (ImageView) findViewById(R.id.img_edit);
mImageViewAbout = (ImageView) findViewById(R.id.img_about);
}
#Override
public void onClick(View v) {
resetImag();
switch (v.getId()) {
case R.id.ll_home:
mViewPager.setCurrentItem(0);
mImageViewHome.setImageResource(R.drawable.home_yes);
break;
case R.id.ll_timer:
mViewPager.setCurrentItem(1);
mImageViewTimer.setImageResource(R.drawable.timer_yes);
break;
case R.id.ll_edit:
mViewPager.setCurrentItem(2);
mImageViewEdit.setImageResource(R.drawable.edit_yes);
break;
case R.id.ll_about:
mViewPager.setCurrentItem(3);
mImageViewAbout.setImageResource(R.drawable.about_yes);
break;
default:
break;
}
}
private void resetImag() {// Reset Picture
mImageViewHome.setImageResource(R.drawable.home_no);
mImageViewTimer.setImageResource(R.drawable.timer_no);
mImageViewEdit.setImageResource(R.drawable.edit_no);
mImageViewAbout.setImageResource(R.drawable.about_no);
}
}
The ViewPagerFragmentAdapter Looks like this:
public class ViewPagerFragmentAdapter extends FragmentStatePagerAdapter {
private List<Fragment> datas;
public ViewPagerFragmentAdapter(FragmentManager fm,List<Fragment> datas) {
super(fm);
this.datas=datas;
}
#Override
public Fragment getItem(int position) {// Return View Object
return datas.get(position);
}
#Override
public int getCount() {// Return View Num
return datas.size();
}
#Override
public Object instantiateItem(ViewGroup container, int position) {// Init View
return super.instantiateItem(container, position);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {// Destroy View
super.destroyItem(container, position, object);
}
}
The layout for the page, it includes a viewpager and also a menu bar in linear layout:
?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.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="449dp"
android:layout_weight="1"></android.support.v4.view.ViewPager>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#ffffffff"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/ll_home"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_home"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#0000"
android:src="#drawable/home_yes" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:textColor="#b6b3b3" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_timer"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_timer"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#0000"
android:src="#drawable/timer_no" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Timer"
android:textColor="#b6b3b3" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_edit"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_edit"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#0000"
android:src="#drawable/edit_no" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit"
android:textColor="#b6b3b3" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_about"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_about"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#0000"
android:src="#drawable/about_no" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About"
android:textColor="#b6b3b3" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
And This is one of the fragment:
public class MyFragment1 extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab1,null);
}
}
I have four fragment prepared for viewpager(they are basiclly the same but with differnt layout), here is what the tab1 layout might look like:
<?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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Chat"
android:textSize="30sp" >
</TextView>
</LinearLayout>
And I only got the menu bar when I run it, the viewpager is not set and I don't know why
Remove android:layout_weight="1" <Viewpager> and add android:orientation="vertical" to Linear layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="449dp"
></android.support.v4.view.ViewPager>
//-----------

recyclerview not working in fragment

I have a tabed activity , im trying make a recyclerview in activity's fragment , when i Declarate recyclerview in oncreate in runtime gives below error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.daneshhamrah.konkuri/com.daneshhamrah.konkuri.Soalate_Konkur}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
and when i Declarate recyclerview in oncreateview in runtime gives below error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTypeface(android.graphics.Typeface)' on a null object reference
this error is for recyclerview items layout
how can i sove this problem ?
this is my fragment xml 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.daneshhamrah.konk.Soa_Konk$PlaceholderFragment">
<TextView
android:id="#+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:focusableInTouchMode="true">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
>
<TextView
android:id="#+id/soakonk_frag_txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="5dp"
android:text="ss" />
<TextView
android:id="#+id/soakonk_frag_txt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="yy" />
<TextView
android:text="pp"
android:id="#+id/soakonk_frag_txt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.RecyclerView
android:id="#+id/soakonk_frag_rec"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</RelativeLayout>
this is recyclerview items layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/sar_list_single_mainlayout">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:foreground="?attr/selectableItemBackground">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="#+id/soakonk_item_soabtn"
android:layout_width="90dp"
android:layout_height="90dp"
android:background="#drawable/ic_soa_g"
android:layout_margin="5dp"
android:layout_alignParentLeft="true"/>
<TextView
android:id="#+id/soakonk_item_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
<Button
android:id="#+id/soakonk_item_pasbtn"
android:layout_width="90dp"
android:layout_height="90dp"
android:background="#drawable/ic_pas_g"
android:layout_margin="5dp"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
this is java fragment activiy code:
public class Soa_Konk extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_soa__konk);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_soa__konk, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
TextView tv1 ,tv2 ,tv3 ;
Typeface vazirTf = Typeface.createFromAsset(getActivity().getAssets(),"font/Vazir.ttf");
tv1= (TextView)rootView.findViewById(R.id.soakonk_frag_txt1);
tv2= (TextView)rootView.findViewById(R.id.soakonk_frag_txt2);
tv3= (TextView)rootView.findViewById(R.id.soakonk_frag_txt3);
tv1.setTypeface(vazirTf);
tv2.setTypeface(vazirTf);
tv3.setTypeface(vazirTf);
RecyclerView mRecyclerView = (RecyclerView)rootView.findViewById(R.id.soakonk_frag_rec) ;
RecyclerView.LayoutManager mLayoutManager= new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
String[] mArrayNames = {"95","94","93","92","91","90","89","88","87","86","85","84","83","82","81","80","79"};
RecyclerView.Adapter mAdapter = new MyAdapterSoalateKonkur(mArrayNames,getActivity());
mRecyclerView.setAdapter(mAdapter);
return rootView;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
// Show 3 total pages.
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Tab A";
case 1:
return "Tab B";
case 2:
return "Tab C";
case 3:
return "Tab D";
}
return null;
}
}
}
When the Exception occurs for the RecyclerView items, have a look at your MyAdapter class and your related ViewHolder and check if you correctly get your TextView there.

Unable to load the correct tab on the corresponding index on getItem in android

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());
}

How to show two fragment views in one activity in Android?

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;
}
}

how to implement tabhost inside fragment?

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.

Categories

Resources