Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Fragments with Tabs and Viewpager

enter image description here

We are building an App as shown above that has nesting of Fragments.

  1. Tabs Featuring - Details Tab and MAps Tab
  2. Details Tab will have a slideshow - like the View Page Slider and information below that which will be scrollable.
  3. Maps Tab which will display the maps.

enter image description here

I have implmented the tabs and maps as well as the Slider as seen above. Now i am confused how i can add content below the Slider which will make the Details Tab scrollable.

What i have tried ?

On CLicking the Details tab the Fragment will try to inflate two Fragment layouts inside it.

AndroidTabLayoutActivity.java

package com.mink7.abs;

import com.viewpagerindicator.CirclePageIndicator;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import java.util.Random;
import android.support.v4.app.FragmentTabHost;
import com.viewpagerindicator.PageIndicator;

import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class AndroidTabLayoutActivity extends TabActivity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                // FragmentTabHost tabHost;

                setContentView(R.layout.main);
                // tabHost = (FragmentTabHost) findViewById(R.id.tabMode);

                TabHost tabHost = getTabHost();

                /*
                 * mAdapter = new TestFragmentAdapter(getSupportFragmentManager());
                 *
                 * mPager = (ViewPager) findViewById(R.id.pager);
                 * mPager.setAdapter(mAdapter);
                 *
                 * mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
                 * mIndicator.setViewPager(mPager);
                 */

                // Tab for Photos
                TabSpec photospec = tabHost.newTabSpec("Details");
                photospec.setIndicator("Details",
                                getResources().getDrawable(R.drawable.icon_photos_tab));
                Intent photosIntent = new Intent(this, DetailsActivity.class);
                photospec.setContent(photosIntent);

                // Tab for Songs
                TabSpec songspec = tabHost.newTabSpec("Maps");
                // setting Title and Icon for the Tab
                songspec.setIndicator("Maps",
                                getResources().getDrawable(R.drawable.icon_songs_tab));
                Intent songsIntent = new Intent(this, MapsActivity.class);
                songspec.setContent(songsIntent);

                // Tab for Videos
                /*
                 * TabSpec videospec = tabHost.newTabSpec("Videos");
                 * videospec.setIndicator("Videos",
                 * getResources().getDrawable(R.drawable.icon_videos_tab)); Intent
                 * videosIntent = new Intent(this, VideosActivity.class);
                 * videospec.setContent(videosIntent);
                 */

                // Adding all TabSpec to TabHost
                tabHost.addTab(photospec); // Adding photos tab
                tabHost.addTab(songspec); // Adding songs tab
                // tabHost.addTab(videospec); // Adding videos tab
        }

}

DetailsActivity.java

package com.mink7.abs;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import com.viewpagerindicator.CirclePageIndicator;

public class DetailsActivity extends BaseSampleActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.place_details_layout);

        mAdapter = new TestFragmentAdapter(getSupportFragmentManager());

        mPager = (ViewPager)findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

        mIndicator = (CirclePageIndicator)findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);


    }
}

BaseSampleActivity.java

package com.mink7.abs;

import java.util.Random;

import com.viewpagerindicator.PageIndicator;

import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public abstract class BaseSampleActivity extends FragmentActivity {
    private static final Random RANDOM = new Random();

    TestFragmentAdapter mAdapter;
    ViewPager mPager;
    PageIndicator mIndicator;
    //FragmentTabHost mTabHost;


    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.random:
                final int page = RANDOM.nextInt(mAdapter.getCount());
                Toast.makeText(this, "Changing to page " + page, Toast.LENGTH_SHORT);
                mPager.setCurrentItem(page);
                return true;

            case R.id.add_page:
                if (mAdapter.getCount() < 10) {
                    mAdapter.setCount(mAdapter.getCount() + 1);
                    mIndicator.notifyDataSetChanged();
                }
                return true;

            case R.id.remove_page:
                if (mAdapter.getCount() > 1) {
                    mAdapter.setCount(mAdapter.getCount() - 1);
                    mIndicator.notifyDataSetChanged();
                }
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
like image 266
Harsha M V Avatar asked Nov 29 '12 11:11

Harsha M V


2 Answers

Be aware Fragments nesting is supported since Android 4.2 or latest compatibility libraries. Previously it was simply not supported. As for content below - just put them it all in one more container

like image 142
Marcin Orlowski Avatar answered Oct 07 '22 21:10

Marcin Orlowski


*I have successfully implemented a similar project using nested Fragments(4.2).

For the nested fragments, instead of using getSupportFragmentManager(), use getChildFragmentManager().

e.g. to use viewpagerFragment inside a tabFragment, sample code is :

public class TabFragment extends Fragment 
{
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                                      Bundle savedInstanceState)
{
  ..
  ..
  ..
  mTabHost.setup(this.getActivity().getApplicationContext(),          
                                     getSupportFragmentManager(),R.layout.*);
  mTabHost.setOnTabChangedListener(tabChangeListener);
  mTabHost.addTab(mTabHost.newTabSpec("label").setIndicator("tag"),
                    ViewPagerFragment.class, arg);
}

public class ViewPagerFragment extends Fragment
{

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle         
                         savedInstanceState) 
{
    return inflater.inflate(.......);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState)
{

    mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
    mViewPager.setAdapter(new MyAdapter(getChildFragmentManager()));


}
public class MyAdapter extends FragmentPagerAdapter
{
    public MyAdapter(FragmentManager fm)
    {
        super(fm);
    }
    @Override
    public int getCount() 
    {
        return size;

    }

    @Override
    public Fragment getItem(int position) 
    {

    }

    @Override
    public CharSequence getPageTitle(int position) 
    {

    }

}*
like image 38
sumit Avatar answered Oct 07 '22 21:10

sumit