Good night, I'm new at this, the think that i need to do is this effect using a sroll which conceal a title & displays the following. For example: what happens in the contact list, when you spend the A and B, but still keeps all contacts letter.
I have an example implemented in ios with ViewForHeaderInSection this is what they used to do this effect in instagram this is what I need to do in android.
This is the closest thing I found:
Visit How to implement a scroll view on only part of my layout
Thanks, I apologize for my English is not excellent.
after search and search, i develop my woner layour with propierties...
This class help tu implements a #SlidePage into Fragment.
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.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.List;
import mijem.bitgray.us.mijem_android.R;
public class FragmentTutorial extends Fragment {
/**
* The number of pages (wizard steps) to show in this demo.
*/
private static final int NUM_PAGES = 3;
/**
* The pager widget, which handles animation and allows swiping horizontally
* to access previous and next wizard steps.
*/
private ViewPager mPager;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private PagerAdapter mPagerAdapter;
ImageView indicator0;
ImageView indicator1;
ImageView indicator2;
List<ImageView> listIndicators = new ArrayList<ImageView>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_viewpager_tutorial, container, false);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) rootView.findViewById(R.id.pager);
mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
resetIndicators();
listIndicators.get(mPager.getCurrentItem()).setImageResource(
R.drawable.indicador_slide_on);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
mPagerAdapter = new ScreenSlidePagerAdapter(getActivity()
.getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
indicator0 = (ImageView) rootView.findViewById(R.id.indicator0);
indicator1 = (ImageView) rootView.findViewById(R.id.indicator1);
indicator2 = (ImageView) rootView.findViewById(R.id.indicator2);
listIndicators.add(indicator0);
listIndicators.add(indicator1);
listIndicators.add(indicator2);
return rootView;
}
#Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
//TabListener.onResumeFragment(this);
}
public void resetIndicators() {
for (ImageView img : listIndicators)
img.setImageResource(R.drawable.indicador_slide_off);
}
/**
* A simple pager adapter that represents 5 ScreenSlidePageFragment objects,
* in sequence.
*/
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
fragment.setPosition(position);
return fragment;
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
enter code here
and the adapter:
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
fragment.setPosition(position);
return fragment;
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
enter code here
Code of fragmen pattern
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.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.List;
import mijem.bitgray.us.mijem_android.R;
public class FragmentTutorial extends Fragment {
/**
* The number of pages (wizard steps) to show in this demo.
*/
private static final int NUM_PAGES = 3;
/**
* The pager widget, which handles animation and allows swiping horizontally
* to access previous and next wizard steps.
*/
private ViewPager mPager;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private PagerAdapter mPagerAdapter;
ImageView indicator0;
ImageView indicator1;
ImageView indicator2;
List<ImageView> listIndicators = new ArrayList<ImageView>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_viewpager_tutorial, container, false);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) rootView.findViewById(R.id.pager);
mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
resetIndicators();
listIndicators.get(mPager.getCurrentItem()).setImageResource(
R.drawable.indicador_slide_on);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
mPagerAdapter = new ScreenSlidePagerAdapter(getActivity()
.getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
indicator0 = (ImageView) rootView.findViewById(R.id.indicator0);
indicator1 = (ImageView) rootView.findViewById(R.id.indicator1);
indicator2 = (ImageView) rootView.findViewById(R.id.indicator2);
listIndicators.add(indicator0);
listIndicators.add(indicator1);
listIndicators.add(indicator2);
return rootView;
}
#Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
//TabListener.onResumeFragment(this);
}
public void resetIndicators() {
for (ImageView img : listIndicators)
img.setImageResource(R.drawable.indicador_slide_off);
}
/**
* A simple pager adapter that represents 5 ScreenSlidePageFragment objects,
* in sequence.
*/
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
fragment.setPosition(position);
return fragment;
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
Related
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;
}
}
I am currently struggling with giving tinder card animation effect in my view pager.
I am new to animation and dont know much about it.
I am currently using :AndTinder library.
here is my view pager code.
import android.database.Cursor;
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.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
public class ItemPagerFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
/**
* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.
*/
private ViewPager mPager;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private CursorPagerAdapter adapter;
private ImageButton favButton;
private ImageButton shareButton;
public ItemPagerFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
System.gc();
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_item_pager, container, false);
// Instantiate a ViewPager and a PagerAdapter in app.
mPager = (ViewPager) rootView.findViewById(R.id.pager);
getLoaderManager().initLoader(-5, null, this);
mPager.setOffscreenPageLimit(0);
adapter = new CursorPagerAdapter(getFragmentManager(), null);
mPager.setAdapter(adapter);
mPager.setOnPageChangeListener(new OnPageChangeListener() {
// This method will be invoked when a new page becomes selected.
#Override
public void onPageSelected(int position) {
//Toast.makeText(getActivity(),
//"Selected page position: " + ++position, Toast.LENGTH_SHORT).show();
}
// This method will be invoked when the current page is scrolled
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// Code goes here
}
// Called when the scroll state changes:
// SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
#Override
public void onPageScrollStateChanged(int state) {
// Code goes here
}
});
return rootView;
}
#Override
public void onDestroyView() {
System.gc();
super.onDestroyView();
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
CursorLoader loader = new CursorLoader(getActivity(),
DataProvider.CONTENT_URI_DATA,
null,
null, // "favorite = 0"
null,
"RANDOM()");
return loader;
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
adapter.swapCursor(data);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}
/**
* A simple pager adapter.
*/
private class CursorPagerAdapter extends FragmentStatePagerAdapter {
//HashMap<Integer,ItemDetailFragment> hash = new HashMap<Integer,ItemDetailFragment>();
ItemDetailFragment[] record = new ItemDetailFragment[3];
//HashMap<Integer,ItemDetailFragment> hash = new HashMap<Integer,ItemDetailFragment>(3);
private Cursor mCursor;
public CursorPagerAdapter(FragmentManager fm, Cursor c) {
super(fm);
mCursor = c;
}
#Override
public Fragment getItem(int position) {
if (mCursor.moveToPosition(position)) {
Bundle arguments = new Bundle();
arguments.putLong(ItemDetailFragment.ARG_ITEM_ID, mCursor.getLong(mCursor.getColumnIndex(DataProvider.COL_ID)));
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
// System.out.println("putting page " + position + " in " + position % 3);
record[position%3] = fragment;
//hash.put(position , fragment);
return fragment;
}
return null;
}
#Override
public int getCount() {
if (mCursor != null) {
return mCursor.getCount();
}
return 0;
}
public void swapCursor(Cursor cursor) {
mCursor = cursor;
try {
notifyDataSetChanged();
}
catch(Exception e)
{
System.out.println("exception caught");
}
}
public void destroyItem(ViewGroup container, int position, Object object){
record[position%3] = null;
super.destroyItem(container,position,object);
}
private ItemDetailFragment getfragment(int position)
{
return record[position%3];
//return hash.get(position);
}
public int getItemPosition(Object object) {
ItemDetailFragment f = (ItemDetailFragment ) object;
int position = mPager.getCurrentItem();
ItemDetailFragment current = adapter.getfragment(position);
if(current.equals(f))
{
int fav = Integer.parseInt(f.data.getFavorite());
if(fav == 1)
f.update(1);
else
f.update(0);
}
return super.getItemPosition(object);
}
//changes to be done here
/*public int getItemPosition(Object object) {
ItemDetailFragment f = (ItemDetailFragment ) object;
int position = mPager.getCurrentItem();
System.out.println("prerna update "+position);
ItemDetailFragment current = adapter.getfragment(position);
if(current.equals(f))
{
System.out.println("prerna got it");
int fav = Integer.parseInt(f.data.getFavorite());
if(fav == 1)
f.update(1);
else
f.update(0);
}
return super.getItemPosition(object);
// return POSITION_NONE;//comment afterwards
}*/
}
}
can anybody please help me to do this.
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.