This is how I implement tab host (ReminderPage and Completed Task). There has a listView in Reminder Fragment. You can have a look here.
MainActivity1
public class MainActivity1 extends ActionBarActivity implements ActionBar.TabListener{
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private String[] tabs = {"Reminder", "Completed Task"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
viewPager = (ViewPager) findViewById(R.id.pager);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for(int i=0; i<2; i++){
actionBar.addTab(actionBar.newTab().setText(tabs[i]).setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int arg) {
actionBar.setSelectedNavigationItem(arg);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabReselected(ActionBar.Tab arg0, FragmentTransaction arg1) {
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction arg1) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab arg0, FragmentTransaction arg1) {
}
}
TabsPagerAdapter
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
if(index==0) {
Fragment fragment = new ReminderPage();
return fragment;
}
if(index==1)
{
Fragment fragment=new CompletedTask();
return fragment;
}
return null;
}
@Override
public int getCount() {
return 2;
}
}
ReminderPage
public class ReminderPage extends Fragment implements View.OnClickListener {
AllAdapter adapter;
ArrayList<SearchList> search = new ArrayList<SearchList>();
ListView listview;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.reminder_page, container, false);
final ActionBar actionBar = ((ActionBarActivity) getActivity()).getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
setHasOptionsMenu(true);
listview = (ListView) rootView.findViewById(R.id.list_todo);
return rootView;
}
reminder_page
<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" tools:context=".MainActivity">
<imageview android:src="@mipmap/to_do" android:layout_margintop="50dp" android:layout_width="130dp" android:layout_height="210dp" android:id="@+id/imageView" android:gravity="center" android:layout_centerhorizontal="true">
<textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginbottom="20dp" android:text="No Reminder List" android:textsize="15dp" android:textcolor="@color/btn_login" android:gravity="center" android:id="@+id/NoData" android:layout_centerhorizontal="true" android:layout_below="@+id/imageView">
<Button
android:layout_width="250dp"
android:layout_height="60dp"
android:text="Add Reminder"
android:id="@+id/button2"
android:layout_centerHorizontal="true"
android:layout_below="@+id/NoData"/>
<relativelayout android:layout_width="match_parent" android:layout_height="wrap_content">
<listview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/list_todo" android:layout_weight="1" android:layout_alignparentleft="true" android:layout_alignparentbottom="true">
AllAdapter
public class AllAdapter extends BaseAdapter {
Context context;
private static ArrayList<SearchList> search;
private LayoutInflater mInflater;
ListView listview;
public AllAdapter(Context context, ArrayList<SearchList> searchList, ListView listview) {
search=searchList;
this.listview=listview;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return search.size();
}
public SearchList getItem(int position) {
return search.get(position);
}
public long getItemId(int position) {
return search.get(position).getID();
}
public void removeItem(int position) {
search.remove(position);
this.notifyDataSetChanged();
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_to_do, null);
holder= new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.task_title);
holder.time = (TextView) convertView.findViewById(R.id.time);
holder.date = (TextView) convertView.findViewById(R.id.date);
final ToggleButton toggle =(ToggleButton)convertView.findViewById(R.id.donePic);
toggle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (toggle.isChecked()) {
Toast.makeText(v.getContext(), "Clicked Laugh Vote", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(v.getContext(), "Not Clicked Laugh Vote", Toast.LENGTH_SHORT).show();
}
}
});
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
holder.text.setText(search.get(position).getTitle());
holder.time.setText(search.get(position).getTime());
holder.date.setText(search.get(position).getDate());
return convertView;
}
static class ViewHolder {
TextView text,time,date;
ToggleButton toggle;
}
}
Hope this helps.