Use Fragment with ViewPager - android

I am using View pager to create a swipe view effect
Now i want to implement different fragments on same activity fragments are different layout which are displayed according to the condition.
I am stuck on how to create pagerAdapter and how to create fragment class.
Code:
public class mFragment extends Fragment{
Context mContext;
public mFragment(Context context){
mContext = context;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.main, ActivityScreen.mViewPager,false);
}
}
public class mPagerAdapter extends FragmentPagerAdapter
{
int i=-1;
public mPagerAdapter(FragmentManager fm)
{
super(fm);
mViewPager.removeAllViews();
}
#Override
public int getCount()
{
return data.size();
}
#Override
public Fragment getItem(int pos) {
return new mFragment(Context ctx);
}
}
Please help me on this

Override #instantiateItem method and return the fragments to update in the view
use this link
will be helpful m i think

in your FragmentActivity add an adapter for FragmentPagerAdapter then
#Override Fragment getItem and add Fragments to it accourding to requirement

Related

How to create display multiple layouts in onCreateView method

I am trying to create an intro slider screen using fragments and ViewPager. I want to display multiple layouts in onCreateView method but don't know how to do it.
Any help please?
Main activity
public class MainActivity extends AppCompatActivity {
public ViewPager pager;
public The_fragment_adapter the_fragment_adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = (ViewPager) findViewById(R.id.pager);
the_fragment_adapter = new The_fragment_adapter(getSupportFragmentManager());
pager.setAdapter(the_fragment_adapter);
}
}
Fragment Class
public class The_fragment extends Fragment {
public The_fragment(){
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View my_view=inflater.inflate(R.layout.theui,container,false);
return my_view;
}
}
Adapter View
public class The_fragment_adapter extends FragmentStatePagerAdapter {
public int[] the_layouts = {R.layout.page1,R.layout.page2};
public The_fragment_adapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return new The_fragment();
}
#Override
public int getCount() {
return the_layouts.length;
}
}
A ViewPager is for paging multiple fragments, not layouts. The getItem() method should return the fragment you want for the position you want.
Create a second fragment with the layout you want:
public Fragment The_second_fragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View my_view=inflater.inflate(R.layout.page2,container,false);
return my_view;
}
}
Then change your adapter methods:
#Override
public Fragment getItem(int position) {
switch 0:
return new The_fragment();
switch 1:
return new The_second_fragment();
}
#Override
public int getCount() {
return 2;
}
See the excellent tutorial in the Android Developer's Guide. And, in the future if you follow the Java naming conventions you are more likely to get a better answer as other programmers will be able to understand your code more easily.

ViewPager getChildAt() returns null

I am using ViewPager in my activity and it only has two fragments, both implemented in similar structure as follow, so there should always be a fragment returned when getInstanced is called:
public class ListReminderFragment extends Fragment {
private View rootView;
private static ListReminderFragment listReminderFragment;
public static ListReminderFragment getInstance() {
if(listReminderFragment != null)
return listReminderFragment;
else {
listReminderFragment = new ListReminderFragment();
return listReminderFragment;
}
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.content_main_screen, container, true);
return rootView;
}
#Nullable
#Override
public View getView() {
return rootView;
}
}
This is my adapter:
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return ListReminderFragment.getInstance(); // temporary
}
#Override
public int getCount() {
return 2;
}
}
And I am initialising my ViewPager as below, in OnCreate() in the activity that has the ViewPager:
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
ViewPager viewPager = (ViewPager) findViewById(R.id.main_view_pager);
viewPager.setAdapter(viewPagerAdapter);
But when I call viewPager.getChildAt(), viewPagerAdapter.getItem(0).getView(), they both return null,
when I call viewPager.getChildCount(), it returns 0 instead of 2.
How should I access the views in side the fragment in ViewPager?

How to implement a viewpager inside of a fragment?

I have been looking and having a hard time finding a clear cut example? I am trying to understand how to create a viewpager within a fragment that is open as a drawer item from my mainActivity...
This is my attempt but I think I am doing something wrong whether it be that I am including it incorrectly or have it in the wrong spot...
public class RandomFragment extends android.app.Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_random, container, false);
return rootView;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ViewPager mViewPager = (ViewPager) view.findViewById(R.id.random_pager);
mViewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return 4;
}
#Override
public Fragment getItem(int position) {
Bundle args = new Bundle();
args.putInt(TextViewFragment.POSITION_KEY, position);
return TextViewFragment.newInstance(args);
}
}
I use this one hope it help you too : https://github.com/thecodepath/android_guides/wiki/ViewPager-with-FragmentPagerAdapter
So you can use this one: https://github.com/astuetz/PagerSlidingTabStrip

ViewPager displays the same ListFragments for all pages

I'm trying to use a ListFragment with a ViewPager using the FragmentPagerAdapter. The pager consists of two instances of the ListFragment. The ViewPager control works and allows me to swipe between the fragments however the same ListFragment appears in both pages. It seems that getItem() method of the ViewPager creates two different instances of the ListFragment but it displays only the second instance of it for both pages. Here is a code:
// FragmentPagerAdapter
private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
Log.i("Page: ", index + "");
return PageFragment.newInstance(pages.get(index));
// pages is an ArrayList<ArrayList<Restaurant>> pages
}
#Override
public int getCount() {
return numberOfPages;
}
}
// ListFragment
public class PageFragment extends ListFragment {
private static ArrayList<Restaurant> restaurantsList;
public static PageFragment newInstance(ArrayList<Restaurant> restaurantsList) {
PageFragment pageFragment = new PageFragment();
pageFragment.setRestaurantsList(restaurantsList);
return pageFragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
setListAdapter(new RestaurantArrayAdapter(getActivity().getBaseContext(), getRestaurantsList()) );
return view;
}
private void setRestaurantsList(ArrayList<Restaurant> list)
{
restaurantsList = list;
}
private ArrayList<Restaurant> getRestaurantsList()
{
return this.restaurantsList;
}
}
Well mate,
The problem is you've declared you array member as "Static",
private static ArrayList<Restaurant> restaurantsList;
Lose the static and you should be fine.

Android ViewPager, switching from fragment to another fragment

I have a viewpager with 2 pages, on each fragment i put a button to switching fragment,
but if i change orientation switching doesn't work.
For switching fragment I using my OnChangePageButtonClick interface
Why is this happening?
ViewPager Activity:
public class ViewPagerMusic extends FragmentActivity implements OnChangePageButtonClick {
private ViewPager vp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_pager_music);
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(super.getSupportFragmentManager(), fragments);
vp = (ViewPager)findViewById(R.id.viewpager);
vp.setAdapter(viewPagerAdapter);
}
private class ViewPagerAdapter extends FragmentStatePagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch(position) {
case 0:
MainPage mainPage = new MainPage();
mainPage.setOnChengeButtonListener(ViewPagerMusic.this);
return mainPage;
case 1:
PlaylistPage playlistPage = new PlaylistPage();
playlistPage.setOnChengeButtonListener(ViewPagerMusic.this);
return playlistPage;
}
return null;
}
#Override
public int getCount() {
return 2;
}
}
#Override
public void selectPage(int page) {
vp.setCurrentItem(page);
}
}
my Frgamnets:
public class MainPage extends Fragment {
public MainPage() {
setRetainInstance(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.music_main_page, container, false);
ImageButton btnPlaylist = (ImageButton)v.findViewById(R.id.btnGoPlaylist2);
btnPlaylist.setOnClickListener(onButtonListener);
return v;
}
private OnClickListener onButtonListener = new OnClickListener() {
#Override
public void onClick(View view) {
onChangePageButtonClick.selectPage(1);
}
};
public void setOnBackButtonListener(OnChangePageButtonClick onChangePageButtonClick) {
this.onChangePageButtonClick = onChangePageButtonClick;
}
private OnChangePageButtonClick onChangePageButtonClick;
}
Playlist fragment is similar to MainPage fragment.
Your fragments are storing a reference to the ViewPagerMusic activity. However, after rotation, the activity is destroyed and recreated, so the fragments now contain a reference to the old activity.
Instead of storing a reference to the activity, you can call the activity's method from the fragment like this:
((ViewPagerMusic) getActivity()).selectPage(1);
just create this method in your view pager
#Override
public void selectPage(int page) {
vp.setCurrentItem(page);
}
and then call this in your onclick method
((ViewPagerMusic) getActivity()).selectPage(1);

Categories

Resources