I'm trying to develop a swipe slide activity with ViewPager and Fragments. I'm writting this code in the ScreenSlideActivity following tutorial from the Android Documentation.
(https://developer.android.com/training/animation/screen-slide)
But in the code of the tutorial there's a method that's not defined and I have an exception that says 'cannot resolve symbol class ScreenSlidePageFragment'
It seems that this method is not defined but I don't know how to write it because I don't what this method does.
I hope somebody can help me !
#Override
public Fragment getItem(int position) {
return new ScreenSlidePageFragment();
}
This is the full code
package com.flixarts.ar.englishnow;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class ScreenSlideActivity extends FragmentActivity {
private static final int NUM_PAGES = 3;
private ViewPager mPager;
private PagerAdapter pagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
mPager = (ViewPager) findViewById(R.id.pager);
pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(pagerAdapter);
}
#Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
// If the user is currently looking at the first step, allow the system to handle the
// Back button. This calls finish() on this activity and pops the back stack.
super.onBackPressed();
} else {
// Otherwise, select the previous step.
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return new ScreenSlidePageFragment();
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
If you don't have this fragment, create it:
public class ScreenSlidePageFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_screen_slide_page, container, false);
return rootView;
}
}
define public constructor in ScreenSlidePageFragment
public ScreenSlidePageFragment() {
// Required empty public constructor
}
you should create your own Fragment that you can return in your adapter or use the same as the example
public class ScreenSlidePageFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_screen_slide_page, container, false);
return rootView;
}
}
Related
I have two fragments: FirstFragment and SecondFragment. FirstFragment contains ViewPager, which has three pages (three fragments). In the onCreate of activty, I add FirstFragment to main layout, and three pages show normally.
Problem is: when I replace FirstFragment by SecondFragment, and then comeback FirstFragment three pages disappear. Then I scroll ViewPager forward and backward, pages show but only first and third pages show, second page disappear.
Here FirstFragment:
package com.example.bkmsx.afragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
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 android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends Fragment {
ViewPager mViewPager;
static MainActivity mainActivity;
public static FirstFragment newInstance(MainActivity activity){
mainActivity = activity;
return new FirstFragment();
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.first_fragment, null);
mViewPager = (ViewPager) view.findViewById(R.id.view_pager);
ViewPagerAdapter adapter = new ViewPagerAdapter(mainActivity.getSupportFragmentManager());
mViewPager.setAdapter(adapter);
return view;
}
private class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
String text = "";
switch (position) {
case 0: text = "First Page";
break;
case 1: text = "Second Page";
break;
case 2: text = "Third Page";
break;
}
return TextFragment.newInstance(mainActivity, text);
}
#Override
public int getCount() {
return 3;
}
}
}
SecondFragment:
package com.example.bkmsx.afragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class SecondFragment extends Fragment {
static MainActivity mainActivity;
public static SecondFragment newInstance(MainActivity activity) {
mainActivity = activity;
return new SecondFragment();
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
TextView textView = new TextView(mainActivity);
textView.setText("New Fragment");
return textView;
}
}
Any suggestion? Thanks in advance.
In FirstFragment , Use this code and try
public View view; // declare view as global variable
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
if(view == null){
view = inflater.inflate(R.layout.first_fragment, null);
mViewPager = (ViewPager) view.findViewById(R.id.view_pager);
ViewPagerAdapter adapter = new ViewPagerAdapter(mainActivity.getSupportFragmentManager());
mViewPager.setAdapter(adapter);
}
return view;
}
As Divyesh comment has good suggestion there is also need to add
mViewPager.setOffscreenPageLimit(3);
use ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager() ); instead of mainActivity.getSupportFragmentManager()
Try this code Hope it helps
final ViewPager viewPager = (ViewPager) findViewById(R.id.container);
final PagerAdapter adapter = new PaginationAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
viewPager.setOffscreenPageLimit(3);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
Here's my UI hierarchy:
Main Fragment -> Fragment with ActionBar.TabListener -> Fragment with FragmentStatePagerAdapter -> ListFragment
The issue is that the ListFragment does display the first time I go into the tab of the ListFragment, but it does not display the second time (so going to tab 2 displays it, but then going to tab 1 and then tab 2 it doesn't display itself. The ListFragment is in tab 2).
This layout works fine if I remove the FragmentStatePagerAdapter, but now my project requires pages, so I needed to add the FragmentStatePagerAdapter to make the paging work.
However, at this point I dont really know what the problem is. I dont know if the issue is in the FragmentStatePagerAdapter, the ListFragment or if the phone just for some reason looses the data of the ListFragment.
I do know that getItem in FragmentStatePagerAdapter does not get called again when switching tabs.
Here's my code for the TrendsPagerHolder and FragmentStatePagerAdapter (as inner class):
import java.util.ArrayList;
import java.util.Collections;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Fragment;
import android.os.Bundle;
import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TrendsPagerHolder extends Fragment
{
private ArrayList<String>testArray = new ArrayList<String>();
private Context context;
private SharedPreferences preferences;
private Bundle applicationStatus;
// The number of pages to display
private static final int NUM_PAGES = 3;
private ViewPager pager;
//The pager adapter, which provides the pages to the view pager widget
private PagerAdapter pagerAdapter;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
context = getActivity().getApplicationContext();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
context = inflater.getContext();
applicationStatus = savedInstanceState;
View rootView = (View) inflater.inflate(R.layout.trends_screen_slider, container, false);
System.out.println("in TrendsPagerHolder onCreateView");
pager = (ViewPager) rootView.findViewById(R.id.trends_pager);
pagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
pager.setAdapter(pagerAdapter);
pager.setCurrentItem(0);
return rootView;
}
public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter
{
TrendsLV[] arrayOfTrendsFragments;
public ScreenSlidePagerAdapter(FragmentManager fragmentManager)
{
super(fragmentManager);
testArray.add("One");
testArray.add("Two");
testArray.add("Three");
arrayOfTrendsFragments = new TrendsLV[NUM_PAGES];
arrayOfTrendsFragments[0] = new TrendsLV(testArray, 0);
arrayOfTrendsFragments[1] = new TrendsLV(testArray, 1);
arrayOfTrendsFragments[2] = new TrendsLV(testArray, 2);
}
#Override
public Fragment getItem(int position)
{
System.out.println("in getItem. Position = " + position);
return arrayOfTrendsFragments[position];
}
#Override
public int getCount()
{
return NUM_PAGES;
}
}
}
Here's the code for my ListFragment:
import java.util.ArrayList;
import java.util.Collections;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListFragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TrendsLV extends ListFragment
{
private Context context;
private ArrayList<String>testArray;
private Bundle applicationStatus;
private int pageIndex;
public TrendsLV(ArrayList<String> test, int index)
{
testArray = test;
pageIndex = index;
}
public TrendsLV()
{
// required
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
context = inflater.getContext();
applicationStatus = savedInstanceState;
System.out.println("in TrendsLV onCreateView");
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{
if (testArray != null)
{
TrendsListAdapter adapter = new TrendsListAdapter(context, testArray, pageIndex);
setListAdapter(adapter);
}
}
return super.onCreateView(inflater, container, savedInstanceState);
}
}//end class TrendsLV
And here's the code for the Adapter that is called from the ListFragment:
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class TrendsListAdapter extends ArrayAdapter<String>
{
private Context context;
private ArrayList<String>testArray;
private int pageIndex;
public TrendsListAdapter (Context c, ArrayList<String> test, int index)
{
super(c, R.layout.trends_row, test);
context = c;
testArray = test;
pageIndex = index;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.trends_row, parent, false);
TextView label_totalArea = (TextView) rowView.findViewById(R.id.trends_labelTotalArea);
label_totalArea.setText(context.getResources().getString(R.string.stringTotalArea));
return rowView;
}
}
Let me know if you need additional information to solve this issue.
I am testing it on a device using Android 4.4.4.
Thank you.
If you are inflating Fragment inside Fragment you should use getChildFragmentManager() instaed of getFragmentManager().
Here is the doc : http://developer.android.com/reference/android/app/Fragment.html#getChildFragmentManager()
public class TrendsPagerHolder extends Fragment
{
private ArrayList<String>testArray = new ArrayList<String>();
private Context context;
private SharedPreferences preferences;
private Bundle applicationStatus;
// The number of pages to display
private static final int NUM_PAGES = 3;
private ViewPager pager;
//The pager adapter, which provides the pages to the view pager widget
private PagerAdapter pagerAdapter;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
context = getActivity().getApplicationContext();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
context = inflater.getContext();
applicationStatus = savedInstanceState;
View rootView = (View) inflater.inflate(R.layout.trends_screen_slider, container, false);
System.out.println("in TrendsPagerHolder onCreateView");
pager = (ViewPager) rootView.findViewById(R.id.trends_pager);
pagerAdapter = new ScreenSlidePagerAdapter(getChildFragmentManager());
pager.setAdapter(pagerAdapter);
pager.setCurrentItem(0);
return rootView;
}
I think you should create the instance of ListFragment and use that instance in FragmentStatePagerAdapter.. some thing like this..
In Your TrendsLV class , remove the constructor and add below method to create the instance of fragment..
public static TrendsLV newInstance(ArrayList<String> test, int index) {
TrendsLV fragment = new TrendsLV();
testArray = test;
pageIndex = index;
return fragment;
}
Then update your ScreenSlidePagerAdapter as follow..
public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter
{
TrendsLV[] arrayOfTrendsFragments;
public ScreenSlidePagerAdapter(FragmentManager fragmentManager)
{
super(fragmentManager);
testArray.add("One");
testArray.add("Two");
testArray.add("Three");
arrayOfTrendsFragments = new TrendsLV[NUM_PAGES];
arrayOfTrendsFragments[0] = TrendsLV.newInstance(testArray, 0);
arrayOfTrendsFragments[1] = TrendsLV.newInstance(testArray, 1)
arrayOfTrendsFragments[2] = TrendsLV.newInstance(testArray, 2)
}
#Override
public Fragment getItem(int position)
{
System.out.println("in getItem. Position = " + position);
return arrayOfTrendsFragments[position];
}
#Override
public int getCount()
{
return NUM_PAGES;
}
}
or instead of creating the array of fragments.. you can replace the in getItem() method like this..
public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter
{
public ScreenSlidePagerAdapter(FragmentManager fragmentManager)
{
super(fragmentManager);
testArray.add("One");
testArray.add("Two");
testArray.add("Three");
}
#Override
public Fragment getItem(int position)
{
System.out.println("in getItem. Position = " + position);
Fragment instanceFragment = null;
switch (position) {
case 0:
instanceFragment =TrendsLV.newInstance(testArray, 0);;
break;
case 1:
instanceFragment =TrendsLV.newInstance(testArray, 1);;
break;
case 2:
instanceFragment =TrendsLV.newInstance(testArray, 2);;
break;
default:
break;
}
return instanceFragment;
}
#Override
public int getCount()
{
return NUM_PAGES;
}
}
I don't know what is the use of testArray so i haven't made any changes for it.
Hope it helps..!! :)
I have a ViewPager setup with 3 fragment Classes, and a pageadapter class.
MainActivity class
package com.example.swipeview;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity {
ViewPager viewpager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpager = (ViewPager) findViewById(R.id.viewpagers);
com.example.swipeview.PagerAdapter padapter = new com.example.swipeview.PagerAdapter(getSupportFragmentManager());
viewpager.setAdapter(padapter);
}
}
PageAdapter class
package com.example.swipeview;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class PagerAdapter extends FragmentPagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
switch(arg0){
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
case 2:
return new FragmentThree();
default:
break;
}
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
and my first FragmentClass as an example
package com.example.swipeview;
import com.example.swipeview.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment_one_layout, container, false);
}
}//end of class
I've tried my usual methods for creating a listview inside of an Activity class but that doesn't work with the fragment class. I was wondering if anyone could show me how to code a listview into a fragment class.
An example with a custom adapter (using a List as source):
public class ItemsFragment extends Fragment {
/* Must be populated later. */
private final List<Item> items = new ArrayList<>();
private ItemAdapter adapter;
private ListView listView = null;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_items_list, container, false);
adapter = new ItemAdapter(this.getActivity(), items);
listView = (ListView) layout.findViewById(R.id.listview_items);
return layout;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Item currentItem = adapter.getItem(position);
// (...)
}
});
}
/* (...) */
}
I am trying to create a fragment that would have a ViewPager. When the said fragment is called and committed, it only displays the first image and it is not swipable. Any thoughts how to achieve this?
Here is the Fragment
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class HowToFragment extends Fragment {
private ViewPager mViewPager;
private MyAdapter mAdapter;
private int[] image_id = {R.drawable.image_1, R.drawable.image_2, R.drawable.image_3};
private static int image_pos = 0;
public static Fragment newInstance(int pos){
image_pos = pos;
return new HowToFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_how_to_image, container, false);
v.setBackgroundResource(image_id[image_pos]);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mViewPager = new ViewPager(getActivity());
mViewPager.setId(R.id.viewPager_howto);
FragmentManager fm = getFragmentManager();
mAdapter = new MyAdapter(fm);
new setAdapterTask().execute();
}
private class setAdapterTask extends AsyncTask<Void,Void,Void>{
protected Void doInBackground(Void... params) {
return null;
}
#Override
protected void onPostExecute(Void result) {
mViewPager.setAdapter(mAdapter);
}
}
private class MyAdapter extends FragmentStatePagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public int getCount() {
return image_id.length;
}
#Override
public Fragment getItem(int position) {
return HowToFragment.newInstance(position);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
FragmentManager manager = getFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.remove((Fragment) object);
ft.commit();
super.destroyItem(container, position, object);
}
}
}
I doubt you are trying to run this app in Android version less than Honeycomb. What you are trying to achieve is nested fragments. Nested fragment support is available only since jellybean 4.2
I've written a few listView activities, as a proof of concept for myself that I could do it. Now, I'm having trouble loading a listView activity into a single tab for an app with multiple tabs, that allows both swiping and tab selection for navigation. I get the error "The constructor ListView_Adapter(MainActivity.DummySectionFragment) is undefined" when I try to write the code for it. I'm a beginner, and I've lurked pretty hard here for the past few days. Any help is appreciated.
TL;DR : I'm a n00b, and I can't figure out this problem.
My Custom List Adapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class CustomListViewAdapter extends ArrayAdapter<String> {
public CustomListViewAdapter (Context c) {
super(c, R.layout.list_cell);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ListView_Text holder = null;
if (row == null)
{
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_cell, parent, false);
holder = new ListView_Text(row);
row.setTag(holder);
}
else
{
holder = (ListView_Text) row.getTag();
}
holder.populateFrom(getItem(position));
return row;
}
static class ListView_Text {
private TextView cell_name = null;
ListView_Text(View row) {
cell_name = (TextView) row.findViewById(R.id.list_cell_name);
}
void populateFrom(String index) {
cell_name.setText(index);
}
}
}
My Main Activity
import java.util.Locale;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import com.example.twigglebeta2.ListView_Adapter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
private static ListView_Adapter listViewAdapter;
private ListView listView;
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create adapter
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// Switch tab selection to match current page when swiped
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// Create a tab for each 'count' in the activity from getCount()
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
Tab thisTab = actionBar.newTab();
thisTab.setText(mSectionsPagerAdapter.getPageTitle(i));
thisTab.setTabListener(this);
actionBar.addTab(thisTab);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onTabSelected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) {
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
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 DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy,container, false);
switch(getArguments().getInt(ARG_SECTION_NUMBER)){
case 1:
//The constructor ListView_Adapter(MainActivity.DummySectionFragment) is undefined
listViewAdapter = new ListView_Adapter(this);
ListView listView = (ListView) rootView.findViewById(R.id.listView1);
for (int i=0;i<20;i++)
{
listViewAdapter.add("this Index : "+i);
}
return listView;
}
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
}
Instead of listViewAdapter = new ListView_Adapter(this);, you should instead try listViewAdapter = new ListView_Adapter(getActivity().getBaseContext());
The Issue is that you are passing a Fragment to the constructor, and not an Activity context.