ViewPager and Fragment Pager adapter implementation

Posted by Rohit Deshmukh on Stack Overflow See other posts from Stack Overflow or by Rohit Deshmukh
Published on 2012-09-30T03:21:49Z Indexed on 2012/09/30 3:37 UTC
Read the original article Hit count: 276

So I am trying to implement sliding views/fragments using viewpager and fragment pager adapter. convert_home is my main xml file that has android.support.v4.view.PagerTitleStrip and

temperature.xml and velocity.xml are my two other views. I have no clue where I am going wrong.

package app.converto;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ConverTo extends FragmentActivity {


    SectionsPagerAdapter mSectionsPagerAdapter;


    ViewPager mViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mSectionsPagerAdapter);
        setContentView(R.layout.converto_home);  
        mViewPager = (ViewPager) findViewById(R.id.pager);  

    }

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





    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            switch(i){
            case 0:
                Fragment1 fragment = new Fragment1();  

                return fragment;

            case 1:
                        Fragment2 fragment2 = new Fragment2();  
                return fragment2;


            }
            defaultFragment fragment3 = new defaultFragment();  
            return fragment3;
        }

        @Override
        public int getCount() {
            return 2;
        }
//
//        @Override
//        public CharSequence getPageTitle(int position) {
//            switch (position) {
//                case 0: return getString(R.string.velocity);
//                case 1: return getString(R.string.temperature);
//                case 2: return getString(R.string.distance);
//            }
//            return null;
//        }
    }

        public static class Fragment1 extends Fragment{
            public Fragment1(){
            }
             @Override  
             public void onCreate(Bundle savedInstanceState) {  
                 super.onCreate(savedInstanceState);  
             } 

             @Override
             public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
                 //return inflater.inflate(R.layout.temperature, container, false);

                 View view = inflater.inflate(R.layout.temperature, container, false);  
                 TextView textView = (TextView) view.findViewById(R.id.sample);  
                 textView.setText(getArguments().getString("title"));
                 return view;
             }

        }

        public static class Fragment2 extends Fragment{
            public Fragment2(){
            }

             @Override  
             public void onCreate(Bundle savedInstanceState) {  
                 super.onCreate(savedInstanceState);  
             } 
             @Override
             public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
                 //return inflater.inflate(R.layout.velocity, container, false);

                 View view = inflater.inflate(R.layout.temperature, container, false);  
                 TextView textView = (TextView) view.findViewById(R.id.sample);  
                 textView.setText(getArguments().getString("title"));
                 return view;
             }

        }

        public static class defaultFragment extends Fragment{
            public defaultFragment(){
            }//end constructor

             @Override  
             public void onCreate(Bundle savedInstanceState) {  
                 super.onCreate(savedInstanceState);  
             } 

             @Override
             public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
                // return inflater.inflate(R.layout.temperature, container, false);




                 View view = inflater.inflate(R.layout.temperature, container, false);  
                 TextView textView = (TextView) view.findViewById(R.id.sample);  
                 textView.setText(getArguments().getString("title"));
                 return view;
             }//end oncreate




        }//end default fragment

    }

© Stack Overflow or respective owner

Related posts about android-viewpager

Related posts about fragmentpageradapter