I am using ViewPager
for swiping between Fragments
, but can I use ViewPager
to swipe between Views
simple XML layout?
This is my page Adapter
for the ViewPager which is used to swipe between Fragments:
import java.util.List;
import com.app.name.fragments.TipsFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.view.ViewGroup;
public class PageAdapter extends FragmentPagerAdapter {
/**
*
*/
List<Fragment> fragments;
public PageAdapter(FragmentManager fm,List<Fragment> frags) {
super(fm);
fragments = frags;
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return TipsFragment.newInstance(0, 0);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
FragmentManager manager = ((Fragment) object).getFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove((Fragment) object);
trans.commit();
super.destroyItem(container, position, object);
}
}
And this is my tip fragment:
public class TipsFragment extends Fragment
{
public static TipsFragment newInstance(int image,int content)
{
TipsFragment fragment = new TipsFragment();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tip_layout, null);
return view;
}
}
How can I modify my code to work with Views instead of Fragment?
We have build a very simple subclass of the
ViewPager
that we use sometimes.This class does not need a adapter as it will load the views from the layout. In order to use it your projects, just use it instead of the
android.support.v4.view.ViewPager
.All the fancy stuff will still work, but you do not need to be bothered with the adapters.
Based on the previous answers, I made the following class to achieve that in a proper and clearest way (I hope):
And you have to set it in your activity:
Where the XML files are obvious view_screen.xml:
And ActivtyMain has the following layout:
Big thanks to Brian and Nicholas for your answer, I hope I add some clearest information and hightlight some good practices for this feature.
Use this example
You can use a single XML layout nesting the children views.
BUT... you need handle this with an adapter also. Here we return the finded view ID without inflate any other layout.
// Set the ViewPager adapter
You need to override these two methods rather than
getItem()
:yes...you can use View instead of Fragment in viewpager. Here you can Find Whole example that will help you to achieve Viewpager without Fragment. Go through this link.enter link description here
I would like to add my solution here. Given that you don't need to use fragments, you can still create a
PagerAdapter
which attachesviews
instead offragments
to theViewPager
.Extend
PagerAdapter
instead ofFragmentPagerAdapter
Now you need to define three classes which will return the
views
to be inflated in theviewpager
. Similar toCpuView
you will haveMemoryView
andNetworkView
classes. Each of them will inflate their respective layouts.And finally a layout which will be inflated in each of the views
P.S.: The reason I wrote this answer is because all the solutions provided here seems to be working fine, but they are inflating the layouts in the PagerAdapter class itself. For large projects it becomes difficult to maintain if their is a lot of code related to the layouts inflated. Now in this example all the views have separate classes and separate layouts. So the project can be easily maintained.