This is my first time trying to implement a ViewPager. My app basically has a listing and details page and I'd like the user to be able to swipe between the details. The details are based on an id that I pass in from the listing page. The problem is that I'm calling getActivity() in the details fragment, which is coming back null. Like I said, I'm new to implementing a ViewPager so this might be something obvious:
ListingFragment.class:
public class ListingFragment extends SherlockFragmentActivity
{
private ViewPager mViewPager;
private MyFragmentPagerAdapter mMyFragmentPagerAdapter;
private static List<Fragment> fragments;
public int Id = 0;
#Override
public void onCreate(final Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
mViewPager = (ViewPager)findViewById(R.id.viewpager);
mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mMyFragmentPagerAdapter);
DetailsFragment df1 = new DetailsFragment();
df1.Id = 1;
df1.DisplayItems();
fragments.add(lf);
DetailsFragment df2 = new DetailsFragment();
df2.Id = 2;
df2.DisplayItems();
fragments.add(lf);
}
private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
return fragments.get(index);
}
#Override
public int getCount() {
return 2;
}
}
}
DetailsFragment.class
public class DetailsFragment extends SherlockFragmentActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public void onActivityCreated(final Bundle icicle)
{
super.onActivityCreated(icicle);
}
private void DisplayItems()
{
PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("value", false);
...
}
}
UPDATE
I've updated my code to use static newInstance method and a Bundle to pass the id to the details fragment.
ListingFragment.class:
public class ListingFragment extends SherlockFragmentActivity
{
private ViewPager mViewPager;
private MyFragmentPagerAdapter mMyFragmentPagerAdapter;
private static List<Fragment> fragments;
public int Id = 0;
#Override
public void onCreate(final Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
mViewPager = (ViewPager)findViewById(R.id.viewpager);
mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mMyFragmentPagerAdapter);
DetailsFragment df1 = DetailsFragment.newInstance(1);
fragments.add(df1);
DetailsFragment df2 = DetailsFragment.newInstance(2);
fragments.add(df2);
}
private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
return fragments.get(index);
}
#Override
public int getCount() {
return 2;
}
}
}
DetailsFragment.class
public class DetailsFragment extends SherlockFragmentActivity
{
private int detailId;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public void onActivityCreated(final Bundle icicle)
{
super.onActivityCreated(icicle);
}
public static DetailsFragment newInstance(int id) {
DetailsFragment lf = new DetailsFragment();
Bundle bundle = new Bundle();
bundle.putInt("id", id);
lf.setArguments(bundle);
return lf;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.listing, container, false);
detailId = getArguments().getInt("id");
DisplayItems();
return view;
}
private void DisplayItems()
{
PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("value", false);
...
}
}
You shouldn't be setting variables and calling methods that have to do with initialization from the activity like that.Id should be set using setArguments(...) and using a static newInstance(...) method to instantiate your fragment. You should call displayItems() in onCreateView(...) inside your Fragment, not from the attached Activity.
See the official Android guide on Fragments for more information. You definitely should give the whole thing a read. http://developer.android.com/guide/components/fragments.html
Related
android.support.v4.app.Fragment is used, not android.app.fragment
Here is the code:
MaanageActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//x.view().inject(this);
mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
managePager.setAdapter(mAdapter);
managePager.setCurrentItem(0);
managePager.addOnPageChangeListener(this);
manageOrder.setChecked(true);
manageBar.setOnCheckedChangeListener(this);
}
MyFragmentPagerAdapter.java
public class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {
private final int PAGER_COUNT = 4;
private ArrayList<Fragment> fragmentArrayList = null;
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
fragmentArrayList = new ArrayList<Fragment>();
fragmentArrayList.add(new OrderFragment());
fragmentArrayList.add(new UserFragment());
fragmentArrayList.add(new ThreeFragment());
fragmentArrayList.add(new FourFragment());
}
#Override
public int getCount() {
return PAGER_COUNT;
}
#Override
public Fragment getItem(int position) {
return fragmentArrayList.get(position);
}
As you can see, the Adapter contains 4 fragments, let's take a look at
OrderFragment
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Toast.makeText(getActivity(), "gg", Toast.LENGTH_LONG).show();
//Do other things...
}
When I am in UserFragment, I slide left to OrderFragment; now that MyFragmentPagerAdapter extends FragmentStatePagerAdapter, the OrderFragment will be reloaded (invoking onCreateView() etc), so I will see the Toast invoked in OrderFragment.onViewCreated.
However the toast doesn't appear as I expect, why? Don't I use the FragmentStatePagerAdapter correctly?
My goal is init views in main activity, but I got nullpointerexception.
Here my sample code:
public class MainActivity extends SherlockFragmentActivity {
private static final String TAG = "MainActivity";
Fragment one, two, three;
List<Fragment> fragments;
ViewPager pager;
private MyPagerAdapter mPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialisePaging();
initTwo ();
}
private void initialisePaging() {
Log.d(TAG, "initialisePaging");
fragments = new Vector<Fragment>();
one = Fragment.instantiate(getApplicationContext(), One.class.getName());
two = Fragment.instantiate(getApplicationContext(), Two.class.getName());
three = Fragment.instantiate(getApplicationContext(), Three.class.getName());
fragments.add(one);
fragments.add(two);
fragments.add(three);
mPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), fragments) ;
pager = (ViewPager) findViewById(R.id.trinity_viewpager);
pager.setAdapter(mPagerAdapter);
}
private void initTwo (){
View twoView = mPagerAdapter.getItem(1).getView();
TextView twoTv = (TextView) twoView.findViewById(R.id.frag_two_tv);
twoTv.setText("2!!!!");
}
class MyPagerAdapter extends FragmentStatePagerAdapter {
private List<Fragment> fragments;
public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return 3;
}
}
}
Fragments One, Two and Three contains only textViews. I try to init these views and got
NullPointerException:
E/AndroidRuntime(2104): Caused by: java.lang.NullPointerException
E/AndroidRuntime(2104): at ru.alxr.viewpagerapp.MainActivity.initTwo(MainActivity.java:53)
E/AndroidRuntime(2104): at ru.alxr.viewpagerapp.MainActivity.onCreate(MainActivity.java:34)
E/AndroidRuntime(2104): at android.app.Activity.performCreate(Activity.java:5008)
E/AndroidRuntime(2104): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
E/AndroidRuntime(2104): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
Try inflating the view before using findViewById.
UPDATED
I found way to solve problem.
The simpliest way is pass vies via interface:
here is adapter:
static class MyPagerAdapter extends FragmentPagerAdapter {
public static MyPagerAdapter instance = null;
public static MyPagerAdapter getInstance(FragmentManager fm){
if (instance == null) instance = new MyPagerAdapter(fm);
return instance;
}
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment r = null;
switch (position){
default : break;
case 0 : {
r = One.newInstance();
break;
}
case 1 : {
r = Two.newInstance();
break;
}
case 2 : {
r = Three.newInstance();
break;
}
}
return r;
}
#Override
public int getCount() {
return 3;
}
}
and one of fragments:
public final class One extends SherlockFragment {
ViewOneCreated viewOneCreated;
static One one;
Button button;
ExpandableListView someListView;
Map <String, Object> classOneObjects;
private static final String
FRAGMENT = "FRAGMENT",
LISTVIEW = "LISTVIEW",
BUTTON = "BUTTON";
public interface ViewOneCreated{
public void fragmentOneCreated(Map <String, Object> classOneObjects);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
viewOneCreated = (ViewOneCreated) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement onSomeEventListener");
}
}
public static One instance() {
if (one == null) one = new One();
return one;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View baseView = inflater.inflate(R.layout.one_fragment, null);
button = (Button) baseView.findViewById(R.id.fragment_one_translate_button);
someListView= (ExpandableListView) baseView.findViewById(R.id.one_fragment_exp_list_view);
return baseView;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void onViewCreated(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
classOneObjects = new TreeMap<String, Object>();
classOneObjects .put(FRAGMENT, (SherlockFragment) this);
classOneObjects .put(BUTTON, button);
classOneObjects .put(LISTVIEW, someListView);
viewOneCreated.fragmentOneCreated(classOneObjects);
}
//...
}
Need help to fix a code I saw in a https://stackoverflow.com/a/9275732/1938357. The solution I need is get the current view onany page in my viewpager based application
Below is the code for reference
public View getCurrentView(ViewPager pager) {
for (int i = 0; i < pager.getChildCount(); i++) {
View child = pager.getChildAt(i);
if (child.getX() <= pager.getScrollX() + pager.getWidth()
&& child.getX() + child.getWidth() >= pager.getScrollX() + pager.getWidth()) {
return child;
}
}
return getChildAt(0);
I am trying to access the current view in the view pager and used the code above. It works fine when I scroll forwards. But when I scroll backwards then after 2 backward scrolls this stops working.
Public class myActivity extends FragmentActivity
//Variable declarations
protected void onCreate(Bundle savedInstanceState) {
ViewPager mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
View CurrView;
OnPageChangeListener pageChangelistener = new OnPageChangeListener() {
#Override
public void onPageSelected(int pageSelected) {
doTextViewChnges();//access the text view and update it based on pageSelected
---THIS IS WHERE I AM STUCK IN TRYING TO GET THE TEXTVIEW IN MY CURRENT FRAGMWNT/VIEW-------
}
mPager.setOnPageChangeListener(pageChangelistener);
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
return ScreenSlidePageFragment.create(position, <other parameters I want to pass>);
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
And below is my ScreenSlidePageFragment class
public class ScreenSlidePageFragment extends android.support.v4.app.Fragment {
public static final String ARG_PAGE = "pagenumber";
public static final String ARG_PAGEARRAY = "pages";
private int mPageNumber;
//Declare other variables
#SuppressLint("NewApi")
public static ScreenSlidePageFragment create(int pageNumber, String[] pages, int bookmarkPageNumber, String storyName, int linesInOnePage) {
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
args.putStringArray(ARG_PAGEARRAY, pages);
fragment.setArguments(args);
return fragment;
}
#SuppressLint("NewApi")
public ScreenSlidePageFragment() {
}
#SuppressLint("NewApi")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageNumber = getArguments().getInt(ARG_PAGE);
pages = getArguments().getStringArray(ARG_PAGEARRAY);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = (ViewGroup) inflater
.inflate(R.layout.activity_story, container, false);
//load layout with all components filled up for that page
loadView(rootView);
return rootView;
}
public int getPageNumber() {
return mPageNumber;
}
It is very hard to discuss in comments with example of code.
As I see in grepcode FragmentStatePagerAdapter.setPrimaryItem(), receive object as android.support.v4.app.Fragment. But you are receive ClassCastException when trying to cast object as Fragment. Can you check your casting: do you really cast object in android.support.v4.app.Fragment not in android.app.Fragment? If all right, add your exception message to your question, please.
UPDATE:
If your ScreenSlidePagerAdapter has method getCurrentView(), then your method 'getCurrentView()' can work like this:
public View getCurrentView(ViewPager pager) {
return ((ScreenSlidePagerAdapter) pager.getAdapter()).getCurrentView();
}
And method getCurrentView() of your ScreenSlidePagerAdapter:
public View getCurrentView() {
return currView;
}
UPDATE 2:
I've made some modification with your code:
Your Activity with private Adapter:
public class myActivity extends FragmentActivity implements ISelectItemListener {
// Your variable declarations and methods
#Override
protected void onCreate(Bundle savedInstanceState) {
final ViewPager pager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
pager.setAdapter(mPagerAdapter);
pager.setOnPageChangeListener(
new OnPageChangeListener() {
#Override
public void onPageSelected(int pageSelected) {
final ScreenSlidePageFragment fragment = mPagerAdapter.getFragment(pageSelected);
if (fragment != null) {
fragment.doTextViewChnges();
}
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
});
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
//Your variable declarations
private final ScreenSlidePageFragment[] mFragments;
public ScreenSlidePagerAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
mFragments = new ScreenSlidePageFragment[NUM_PAGES];
}
#Override
public int getCount() {
return NUM_PAGES;
}
// It will create new ScreenSlidePageFragment by position if it's not exsist
#Override
public android.support.v4.app.Fragment getItem(int position) {
if (getFragment(position) == null) {
mFragments[position] = ScreenSlidePageFragment.create(position, <other parameters I want to pass>);
}
return getFragment(position);
}
// It will give you ScreenSlidePageFragment if it's exists, or null
public ScreenSlidePageFragment getFragment(int position) {
return mFragments[position];
}
}
}
Your Fragment:
public class ScreenSlidePageFragment extends Fragment {
//Your variables and methods
public static ScreenSlidePageFragment create(int pageNumber, String[] pages, int bookmarkPageNumber, String storyName, int linesInOnePage) {
final Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
args.putStringArray(ARG_PAGEARRAY, pages);
final ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageNumber = getArguments().getInt(ARG_PAGE);
pages = getArguments().getStringArray(ARG_PAGEARRAY);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = (ViewGroup) inflater.inflate(R.layout.activity_story, container, false);
loadView(rootView);
return rootView;
}
public int getPageNumber() {
return mPageNumber;
}
// Let's keep logic of modifications in fragment
public void doTextViewChnges() {
final View view = getView();
// do what you want with your View and his children
}
}
I am trying to implement Swipe view using fragment.
But its very important for me to extend simple activity rather than fragment activity.
I am getting error on getSupportFragmentManager() in the code written below:
public class CollectionDemoActivity extends Activity {
DemoCollectionPagerAdapter mDemoCollectionPagerAdapter;
ViewPager mViewPager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_collection_demo);
mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mDemoCollectionPagerAdapter);
}
public static class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
public DemoCollectionPagerAdapter(FragmentManager fm) {
super(fm);
//int x=10;
}
#Override
public Fragment getItem(int i) {
Fragment fragment = new DemoObjectFragment();
Bundle args = new Bundle();
args.putInt(DemoObjectFragment.ARG_OBJECT, i + 10);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
// For this contrived example, we have a 100-object collection.
return 100;
}
#Override
public CharSequence getPageTitle(int position) {
return "OBJECT " + (position + 1);
}
}
public static class DemoObjectFragment extends Fragment {
public static final String ARG_OBJECT = "object";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_collection_object, container, false);
Bundle args = getArguments();
((TextView) rootView.findViewById(android.R.id.text1)).setText(
Integer.toString(args.getInt(ARG_OBJECT)));
return rootView;
}
}
}
The error is pretty self-explanatory. getSupportFragmentManager() is only defined for the FragmentActivity class. No matter how much you want or need to call that method, it simply doesn't exist for plain Activities.
See also: Difference between Activity and FragmentActivity and Difference between Fragment and FragmentActivity
I have a listview containing item apple, banana, orange ...in second screen activity
when i click on particular item on apple it navigates to Details screen
here output has to appear like apple ..if i swipe page then banana and for nextswipe orage
apple-->banana-->orange
but am getting output like apple-->apple-->apple.
Intent detailIntent =new Intent(SecondScreen.this, Details.class);
startActivity(detailIntent);
public class MyApplication extends Application {
ArrayList<String> totalvalue = new ArrayList<String>();
}
public class Details extends FragmentActivity{
MyApplication app;
ViewPager pager;
MyFragmentPagerAdapter pagerAdapter;
public static int position ;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.detail);
app = ((MyApplication) getApplicationContext());
pager = (ViewPager) findViewById(R.id.pager);
FragmentManager fm = getSupportFragmentManager();
pagerAdapter = new MyFragmentPagerAdapter(fm,app.totalvalue);
pager.setAdapter(pagerAdapter);
}
public static class MyFragmentPagerAdapter extends FragmentPagerAdapter {
private static ArrayList<String> temperList;
public MyFragmentPagerAdapter(FragmentManager fm, ArrayList<String> totalvalue ) {
super(fm);
this.temperList = totalvalue;
}
public Fragment getItem(int position) {
return ThingFragment.newInstance((temperList.get(position)));
}
#Override
public int getCount(){
return temperList.size();
}
private static class ThingFragment extends Fragment {
private String name1;
static ThingFragment newInstance(String string ) {
ThingFragment f = new ThingFragment();
Bundle args = new Bundle();
args.putString("name",temperList.get(position) );
f.setArguments(args);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
name1 = getArguments().getString("name");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.myfragment_layout, container, false);
TextView t = (TextView)v.findViewById(R.id.prodcutt);
t.setText(""+ name1);
return v;
}
}
}
}
args.putString("name",temperList.get(position) ); shouldn't be possible inside your ThingFragment as its static.
That should be args.putString("name",string);
Also your temperList shouldn't be static in your adapter (no reason to, your passing it as a param) just make it final - private final ArrayList<String> temperList;
If that doesn't fix it please post more structured separated code so we can see how your application is built up a bit more. The adapter looks fine, so it maybe a case that your writing/overwriting the array at some point.