I have five activities/screens that I would like to be able to swipe between, each has a different function but are interrelated hence the UI concept of swiping between each.
I have found many discussions around ViewPager and PagerAdapters etc. but cannot seem to find one where swiping switches between different activity screens.
Is this even possible? Could someone point me towards an example project of this with source code? Or show me how to adapt an existing tutorial to do what I wish?
Thanks so much, have a good one!
You can't use ViewPager to swipe between Activities. You need to convert each of you five Activities into Fragments, then combine everything in one FragmentActivity with the Adapter you use with ViewPager.
Here's a link that goes into detail on converting your current Activities info Fragments.
This is the Fragment topic on the Android Developers website, it has a lot of useful info.
Here's another example (full source) that inflates TextViews on each page.
Here's an example that I typed up:
PagerAdapter:
public class PagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<Fragment>();
public PagerAdapter(FragmentManager manager) {
super(manager);
}
public void addFragment(Fragment fragment) {
mFragments.add(fragment);
notifyDataSetChanged();
}
#Override
public int getCount() {
return mFragments.size();
}
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
}
This should be called in onCreate of your FragmentActivity:
private void initPaging() {
FragmentOne fragmentOne = new FragmentOne();
FragmentTwo fragmentTwo= new FragmentTwo();
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
pagerAdapter.addFragment(fragmentOne);
pagerAdapter.addFragment(fragmentTwo);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(pagerAdapter);
}
This is an example of the layout you'd use for your FragmnetActivity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
To create Fragment create a new class that extends Fragment. The first two methods you'll want to override are onActivityCreated and onCreateView.
Here's how you could do that:
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(THE_LAYOUT_FROM_YOUR_ORIGINAL_ACTIVITY, container, false);
return view;
}
}
Fix For mPageradapter
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
pagerAdapter.addFragment(fragmentOne);
pagerAdapter.addFragment(fragmentEvents);
viewPager = (ViewPager) super.findViewById(R.id.pager);
viewPager.setAdapter(pagerAdapter);
Related
How to add a listview inside a viewpager (without using fragments)? Could you please help me? Thanks in advance.
It will be easy if you use Fragment in ViewPager, BUT if you dont want to use ViewPager with Fragment than ViewPager Without Fragments is example where you add Views and layouts to ViewPager and is more complex.
A good example is an image gallery, where the user can swipe between different pictures. On these types of pages, all you really want to display is a view of static content (in this case, an image), how to utilize the ViewPager with just plain-old Views and layouts.
It will be best to use fragment for each pages of viewPager. In this way you will be able to maintain individual pages easily.
In your fragment class(a java class which extends Fragment) you can work with your listview like initializing,setting adapter to listview,setting onClickListner for listview items same as you do in Activity
You can't use ViewPager to swipe between Activities. You need to convert each of you Activities into Fragments, then combine everything in one FragmentActivity with the Adapter you use with ViewPager.
This is not how ViewPager works. You feed the pages to ViewPager with a PagerAdapter. Your ListView will be contained within a Fragment created by the PagerAdapter.
In the layout:
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/home_pannels_pager" />
In the FragmentActivity with this layout:
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
Example of simle PagerAdapter:
public class PagerAdapter extends FragmentPagerAdapter {
public FrontPageAdapter(FragmentManager Fm) {
super(Fm);
}
#Override
public Fragment getItem(int position) {
Bundle arguments = new Bundle();
arguments.putInt("position", position);
FragmentPage fragment = new FragmentPage();
fragment.setArguments(arguments);
return fragment;
}
#Override
public int getCount() {
// return count of pages
return 3;
}
}
Example of FragmentPage:
public class FragmentPage extends Fragment {
public FragmentPage() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_frontpage, container, false);
// probably cast to ViewGroup and find your ListView
Bundle arguments = getArguments();
int position = b.getInt("position");
return view;
}
}
Answered here - https://stackoverflow.com/a/28798795/7764015
I have a ViewPager with 3 sliding fragments. Each of the fragment has listview which contains some images. I'm trying to show a new fragment over the viewpager (like an activity) when one of the images in the listview is clicked and also have the ability to navigate back to the viewpager when back button is pressed.
My code for the listview onClick which is inside the fragment is as follows(for testing, I'm using onClick on the entire listview and not the images)
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG,"Clicked");
ItemDetailFragment fragment = ItemDetailFragment.newInstance("ab","cd");
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.containerr,fragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
The log statement prints out fine, but the fragment doesn't show up.
Here's my activity which has the viewpager
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:theme="#android:style/Theme.WithActionBar">
<com.firstapp.myapplication.CustomViews.SlidingTabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/viewPager">
</android.support.v4.view.ViewPager>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/containerr">
</FrameLayout>
</LinearLayout>
Please help.
Also, in the examples in android docs, any interaction in a fragment which leads to a fragment transaction is first passed to the parent activity which handles adding or replacing a new fragment. I am handling that here in the fragment itself. Which one is the correct way and what is the difference between the two methods?
EDIT : I need to add fragment over the viewPager and not to it. It's like having a new screen come up on clicking an element inside the viewpager's fragment.
Thanks!
I solved it by adding the viewpager in a fragment(instead of having it directly in my activity itself).
My activity now is
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
and I moved the previous activity layout (mentioned in the question) to a ViewPagerFragment
Now in my activity's onCreate I do
ViewPagerFragment viewPagerFragment = new ViewPagerFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, viewPagerFragment)
.commit();
and for showing a new fragment over the viewpager on clicking an item inside the viewpager's fragment, I do
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mListener.onFragmentInteraction(null);
}
});
The listener is an interface implemented in the parent activity as follows
#Override
public void onFragmentInteraction(Uri uri) {
ItemDetailFragment itemDetailFragment = ItemDetailFragment.newInstance("ab","cd");
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container,itemDetailFragment)
.addToBackStack(null)
.commit();
}
I implemented this via the listener interface because as per the docs
All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.
And it works now! :D
Initialize your fragment in Activity class and add them to viewpager like below
List<Fragment> fragments = getFragments();
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
pager = (ViewPager) findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
Add fragments to your viewpager
private List<Fragment> getFragments() {
List<Fragment> fList = new ArrayList<Fragment>();
fList.add(Fragment1.newInstance());
fList.add(Fragment2.newInstance());
fList.add(Fragment3.newInstance());
return fList;
}
/********************** Fragment Page adapter *************************/
private class MyPageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
#Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
#Override
public int getCount() {
return this.fragments.size();
}
}
And create your fragment classes and handle the listview inside those class
I've got an app with left navigation drawer, switching between different fragments. Inside one of those fragment I want to implement SlidingTabLayout.
So, what I did is I copied the SlidingTabLayout.java and SlidingTabStrip.java from Google's iosched app. I used them inside my fragment:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<mynamespace.widget.SlidingTabLayout
android:id="#+id/home_fragment_tabs"
android:background="?attr/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager
android:id="#+id/home_fragment_pager"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent" />
</LinearLayout>
And then in my Fragment class, inside onCreateView method, I instantiated my pager and SlidingTabLayout like this:
mPager = (ViewPager) view.findViewById(R.id.home_fragment_pager);
mPager.setAdapter(new HomePagerAdapter(getActivity().getSupportFragmentManager()));
mTabs = (SlidingTabLayout) view.findViewById(R.id.home_fragment_tabs);
mTabs.setViewPager(mPager);
And this is my FragmentPagerAdapter:
class HomePagerAdapter extends FragmentPagerAdapter
{
private String[] tabs = { "Today", "This week", "With star" };
private Fragment[] tabFragments = {
new TodayTabFragment(),
new WeekTabFragment(),
new StarTabFragment()
};
public HomePagerAdapter(FragmentManager fm)
{
super(fm);
}
#Override
public Fragment getItem(int i)
{
return tabFragments[i];
}
#Override
public int getCount()
{
return 3;
}
#Override
public CharSequence getPageTitle(int position)
{
return tabs[position];
}
}
Also I got separate Fragment class and layout for each tab, which is nothing special. And it works, but as soon as I switch to other fragment with my navigation drawer, when I get back, content of one of tabs just dissapears. Mostly it happens to second tab. Also, if I select the HomeFragment from navigation drawer, when is already displayed, I get a nullpointerexception inside SlidingTabLayout.java at this line: mViewPager.setCurrentItem(i);
Any way of fixing it? I have no idea what to do. I guess my code would work, but inside activity. Possible to make it work inside fragment?
I also faced the same issue. Please use FragmentStatePagerAdapter instead of FragmentPagerAdapter which will solve this issue.
Solution:
What I did is instead of implementing FragmentPagerAdapter, I implemented PagerAdapter then I overriden the instantiateItem(ViewGroup container, int position) method and destroyItem() method.
Link, that helped me: http://developer.android.com/samples/SlidingTabsBasic/src/com.example.android.slidingtabsbasic/SlidingTabsBasicFragment.html
Can you try to change constructor of HomePagerAdapter to:
public HomePagerAdapter(SupportFragmentManager fm)
{
super(fm);
}
When you begin transaction instead of suuportfragmanager change it to childfragmentmanager
I'm looking for some ideas on how to do a simple listview with images while inside a SherlockFragment class; Which is then loaded into a ViewPager fragment. I've tried several examples listed below, each one I cannot get to successfully work. I'm not asking for someone to do it for me, I'm looking for advice on how to build one from someones experience to direct me for resources.
Examples i have tried
android fragment example
android sdk fragments
I assume that the listview with images is no problem.
To show fragments in a viewpager you should work with the support-library and go like this:
you create a layout with a viewpager:
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
create a fragment that inflates your layout in 1., your on onCreateView Method should conatin the following lines:
View v = inflater.inflate(R.layout.view_pager_layout, parent, false);
MyFragmentAdapter mMyPagerAdapter =
new MyFragmentAdapter (getChildFragmentManager());
// ... set data to pager
mMyPagerAdapter.setCountOfFragments(mCountOfFragments);
mViewPager = (ViewPager) v.findViewById(R.id.view_pager);
mViewPager.setAdapter(mMyPagerAdapter);
3.create a class that extends from FragmentStatePagerAdapter. This should look like this:
public class MyFragmentAdapter extends FragmentStatePagerAdapter{
public MyFragmentAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int position) {
// create here you listfragment that should be shown in your viewPager
Fragment fragment = new ...;
// you also can set data to this listFragment
Bundle bundle = new Bundle();
fragment.setArguments(bundle);
return fragment;
}
#Override
public int getCount() {
return mCountOfFragments;
}
}
These Links were helpfull for me:
http://developer.android.com/training/animation/screen-slide.html
http://developer.android.com/reference/android/support/v4/view/ViewPager.html
And also the sample of effectivenavigation (https://android.googlesource.com/platform/development/+/d80ee02/samples/training/EffectiveNavigation) is really helpful.
In my application i have a tabhost with a tabwidget that have several Tabs.
Now i need a tab that show me inside the tabcontent a schedule grid that allow swipe to right and left to move through the months. But i need that the tab stay fixes and only the schedule swipes.
The navigation type (Fixed tabs + Swipe) allow me that? From what I understand this navigation allow the swipe but the tab don't stay the same.
What i need is possible?
Thanks for your help and attention.
I would say possible, stay with your codes on tabhost and tabwidget.
So I would assume one of those tabs are calling an Activity which probably named Schedule.class, by default tabhost does not allow any swiping to change tab feature, that is fine.
So in your Schedule Activity, you would be using ViewPager, I learnt how to use it from this article: http://thepseudocoder.wordpress.com/2011/10/05/android-page-swiping-using-viewpager/
Which is pretty easy to be understood. You may try to use it, hope I answered your question
Update: Here's a sample
Schedule.class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.schedule);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
List<Fragment> fragments = new ArrayList<Fragment>();
fragments.add(Fragment.instantiate(this, Fragment1.class.getName()));
fragments.add(Fragment.instantiate(this, Fragment2.class.getName()));
fragments.add(Fragment.instantiate(this, Fragment3.class.getName()));
MyFragmentAdapter miscFragmentAdapter = new MyFragmentAdapter(getSupportFragmentManager(), fragments);
viewPager.setAdapter(miscFragmentAdapter);
}
MyFragmentAdapter.class
public class MyFragmentAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MiscFragmentAdapter(FragmentManager fragmentManager, List<Fragment> fragments) {
super(fragmentManager);
this.fragments = fragments;
}
#Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
#Override
public int getCount() {
return this.fragments.size();
}
}
schedule.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Fragment1.class or Fragment2.class or Fragment3.class
public class Fragment1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
// We have different layouts, and in one of them this
// fragment's containing frame doesn't exist. The fragment
// may still be created from its saved state, but there is
// no reason to try to create its view hierarchy because it
// won't be displayed. Note this is not needed -- we could
// just run the code below, where we would create and return
// the view hierarchy; it would just never be used.
return null;
}
return (LinearLayout) inflater.inflate(R.layout.fragment1, container, false);
}
}
fragment1 is a simple layout with whatever you want inside it.