I created an activity, when I start an activity which implements viewpager, the viewpager created various fragments. I want to use different layouts for each fragment, but the problem is that the fragments is not show inside viewpager.
DataActivity.java
public class DataActivity extends FragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
ViewPager myViewPager;
MyPagerAdapter myPagerAdapter;
super.onCreate(savedInstanceState);
setContentView(R.layout.data_activity);
myViewPager = (ViewPager) findViewById(R.id.viewpager);
mytPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
myViewPager.setAdapter(myPagerAdapter);
}
private class MyPagerAdapter extends FragmentPagerAdapter{
int numberOfPages = 5;
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public android.support.v4.app.Fragment getItem(int index) {
return DataFirstFragment.newInstance();
}
#Override
public int getCount() {
return numberOfPages;
}
}
}
DataFirstFragment is not shown inside viewpager
Anyone can help me please...!
I found solution for above problem, Only replce this
return DataFirstFragment.newInstance();
with:
return new DataFirstFragment();
In Below code i don't see where do you add fragment to adaptor ? ?
private class MyPagerAdapter extends FragmentPagerAdapter{
int numberOfPages = 5;
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public android.support.v4.app.Fragment getItem(int index) {
return DataFirstFragment.newInstance();
}
#Override
public int getCount() {
return numberOfPages;
}
}
SO when viewPager asks adaptor to get item , it does not have anything.
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?
I have a ViewPager, and there's a button when clicked, deletes the current fragment from the PagerAdapter, and prevents the user from swiping there. I researched and found this Remove Fragment Page from ViewPager in Android to be the closest to what I want.
In my fragment to be deleted:
#Override
public void onClick(View v) {
db.delete(count); //SqliteDatabase deletion
displayActivity.removeCurrentFragment(0);
}
My PagerAdapter
public class PagerAdapter extends FragmentPagerAdapter {
long baseId = 0;
public static List<Fragment> fragmentArrayList = new ArrayList<Fragment>();
public PagerAdapter(FragmentManager fm){
super(fm);
fragmentArrayList.add(new FragmentOne());
fragmentArrayList.add(new FragmentTwo());
fragmentArrayList.add(new FragmentThree());
}
#Override
public Fragment getItem(int position) {
return fragmentArrayList.get(position);
}
}
#Override
public int getCount() {
return fragmentArrayList.size();
}
#Override
public int getItemPosition(Object object){
return PagerAdapter.POSITION_NONE;
}
#Override
public long getItemId(int position) {
// give an ID different from position when position has been changed
return baseId + position;
}
}
This is the activity (DisplayActivity) that hosts the pagerAdapter
public class DisplayActivity extends AppCompatActivity {
ViewPager viewPager;
PagerAdapter pagerAdapter;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.db_layout);
viewPager = (ViewPager) findViewById(R.id.pager);
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
}
public void removeCurrentFragment(int position){
pagerAdapter.fragmentArrayList.remove(position);
pagerAdapter.notifyDataSetChanged();
}
What did I do wrong here?
I'm getting an error of
.removeCurrentFragment(int)' on a null object reference
So this error suggests that my method call removeCurrentFragment is referencing a null object, how do I fix it?
You have used
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
so you are creating a new local variable instead of using your global variable. Instead change your line as
pagerAdapter = new PagerAdapter(getSupportFragmentManager());
It should work.
Your onCreate method should look like this.
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.db_layout);
viewPager = (ViewPager) findViewById(R.id.pager);
pagerAdapter = new PagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
}
In the android documentation on FragmentStatePagerAdapter it is explained that the adapter can be set up like this:
public static class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return NUM_ITEMS;
}
#Override
public Fragment getItem(int position) {
return MyFragment.newInstance();
}
}
I would like to use this, however, I would also like to manage the Fragments that were added with a FragmentManager. How can I do this? My approach is throwing a NullPointerException.
public static class MyAdapter extends FragmentStatePagerAdapter {
private FragmentManager mFragmentManager;
public MyAdapter(FragmentManager fm) {
super(fm);
mFragmentManager = fm;
}
#Override
public int getCount() {
return NUM_ITEMS;
}
#Override
public Fragment getItem(int position) {
mFragmentManager.beginTransaction()
.add(MyFragment.newInstance(), MyFragment.TAG).commit();
return mFragmentManager.findFragmentByTag(MyFragment.TAG);
}
}
How can I do this?
You write your own PagerAdapter implementation from scratch. FragmentPagerAdapter and FragmentStatePagerAdapter will do their own fragment transactions, colliding with yours.
I have viewpager with some fragments and i set to each fragment my model class according which UI of fragment should be edited. That model class i set to fragment at constructor before i add that fragment to FragmentStatePagerAdapter. My problem is that after rotating screen or when system kills my application and i restore it, my fragment are recreated by system and my model class instance is lost. Even if I set new FragmentStatePagerAdapter, fragments created by system are visible. Is there any right way of achive that i create new instances of my fragments and place them into viewpager? I tried so many thinks like removing fragments from FragmentManager, adding Adapter to viewpager again, change FragmentStatePagerAdapter only to FragmentPagerAdapter and nothing helped.
My activity class with loading model classes in loader from some resource:
List<Fragment> visibleFragment = new ArrayList<Fragment>();
for (Screen screen : wizard.screens) {
if (screen.visible) {
visibleFragment.add(new ScreenUI(screen));
}
}
viewPager.setAdapter(null);
viewPager.setAdapter(new WizardPagerAdapter(getSupportFragmentManager(), visibleFragment));
My FragmentStatePagerAdapter
public class WizardPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments;
private final int count;
public WizardPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
mFragments = fragments;
count = mFragments.size();
}
#Override
public Fragment getItem(int index) {
return mFragments.get(index);
}
#Override
public int getCount() {
return count;
}
}
try add in Manifest android:configChanges="orientation|keyboardHidden|screenSize" to activity.
OR something like that
public class ViewPagerTaskAdapter extends FragmentStatePagerAdapter {
Map<Integer, FragmentViewTask> fragmentsTask = new HashMap<Integer, FragmentViewTask>();
#Override
public Fragment getItem(int arg0) {
FragmentViewTask myFragment = FragmentViewTask.newInstance(arg0,
tasks.get(arg0));
fragmentsTask.put(arg0, myFragment);
return myFragment;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
fragmentsTask.remove(position);
}
public FragmentViewTask getFragment(int key) {
return fragmentsTask.get(key);
}
#Override
public int getCount() {
if (tasks != null)
return tasks.size();
return 0;
}
}
Have you tried using onRetainInstance(true) on your Fragments?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
I am developing an Android app made up by 3 List fragments. I'm using a ViewPager object to make the user swipe between fragments. I have used the same code with fragments. But when I change it to use list fragments i'm getting an error
the return type is incompatible with FragmentPagerAdapter.getItem(int)
I'm stuck here for hours. please help..
My Fragment Activity
public class PageViewActivity extends FragmentActivity {
MyPageAdapter pageAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page_view);
List<ListFragment> fragments = getFragments();
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
}
private List<ListFragment> getFragments(){
List<ListFragment> fList = new ArrayList<ListFragment>();
fList.add(AllMsgFragment.newInstance("Fragment 1"));
fList.add(ErrorMsgFragment.newInstance("Fragment 2"));
fList.add(SuccessMsgFragment.newInstance("Fragment 3"));
return fList;
}
and my FragmentPageAdapter
class MyPageAdapter extends FragmentPagerAdapter {
private List<ListFragment> fragments;
public MyPageAdapter(FragmentManager fm, List<ListFragment> fragments) {
super(fm);
this.fragments = fragments;
}
#Override
public Fragment getItem(int position) {//The return type is incompatible with FragmentPagerAdapter.getItem(int)
return this.fragments.get(position);
}
#Override
public int getCount() {
return this.fragments.size();
}
#Override
public CharSequence getPageTitle(int position) {
return "Page #" + ( position + 1 );
}
}
Thanks in advance
Finally, thank you #hieuxit. Changing
import android.app.ListFragment;
to
import android.support.v4.app.ListFragment;
Solved my issue...