ViewPager和Fragment Pager适配器实现

时间:2012-09-30 03:21:50

标签: android-viewpager fragmentpageradapter

所以我试图使用viewpager和fragment pager adapter实现滑动视图/片段。 convert_home是我的主要xml文件,它有android.support.v4.view.PagerTitleStrip和

temperature.xml和velocity.xml是我的另外两个视图。我不知道我哪里出错了。应用程序崩溃(负载)

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

    }

4 个答案:

答案 0 :(得分:1)

嗯一切似乎都没问题,你面临的问题究竟是什么?你能更具体一点吗?

编辑: 哦,我想我知道你的问题是什么我没有第一次看到它。在ConvertTo类中,在初始化之前设置mViewPager的适配器。因此,将OnCreate方法更改为:(如果这是问题,请告诉我)

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

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    setContentView(R.layout.converto_home);  

    // Initialize it first otherwise it stays null
    mViewPager = (ViewPager) findViewById(R.id.pager); 

    //Then you set the adapter
    mViewPager.setAdapter(mSectionsPagerAdapter);  


}

答案 1 :(得分:1)

我不确定这对任何人是否有帮助,但我一直在玩上述示例(作为Android编程的菜鸟)并添加了3个片段部分并重新启用了菜单有6种选择。我还没有测试自己的片段并放置内容,但我希望如果其他人在我所在的地方,它也可能会受益,因为似乎很少有关于工作流程的文档。 SDK示例。这意味着理解体验需要更长的时间。

您需要为菜单名称设置一些蜇声,并且您的布局也已准备就绪。

package uk.co.letscommunicate.manx.central;
// http://stackoverflow.com/questions/12658365/viewpager-and-fragment-pager-adapter-implementation

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Context;
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.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the
     * sections. We use a {@link android.support.v4.app.FragmentPagerAdapter} derivative, which will
     * keep every loaded fragment in memory. If this becomes too memory intensive, it may be best
     * to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Create the adapter that will return a fragment for each of the three primary sections
        // of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());


        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

    }



    //unknown may be tab swipe
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
   }
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }
    // unknown    





    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
     * sections of the app.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

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

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

            case 2:
                Fragment3 fragment3 = new Fragment3();  
                return fragment3;    

            case 3:
                Fragment4 fragment4 = new Fragment4();  
                return fragment4;    

            case 4:
                Fragment5 fragment5 = new Fragment5();  
                return fragment5; 

            case 5:
                Fragment6 fragment6 = new Fragment6();  
                return fragment6;
            }            
            DefaultFragment DefaultFragment = new DefaultFragment();  
            return DefaultFragment;
        }



        @Override
        public int getCount() {
            return 6;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0: return getString(R.string.title_section1);
                case 1: return getString(R.string.title_section2);
                case 2: return getString(R.string.title_section3);
                case 3: return getString(R.string.title_section4);
                case 4: return getString(R.string.title_section5);
                case 5: return getString(R.string.title_section6);
            }
            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) {





                 View view = inflater.inflate(R.layout.detailframent, container, false);  
                 //TextView textView = (TextView) view.findViewById(R.id.pager_title_strip);  
                 //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) {



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

        }

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

             @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.detailframent, container, false);  
                 //TextView textView = (TextView) view.findViewById(R.id.pager_title_strip);  
                 //textView.setText(getArguments().getString("title"));
                 return view;
             }

        }

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

             @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.detailframent, container, false);  
                 //TextView textView = (TextView) view.findViewById(R.id.pager_title_strip);  
                 //textView.setText(getArguments().getString("title"));
                 return view;
             }

        }        

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

             @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.detailframent, container, false);  
                 //TextView textView = (TextView) view.findViewById(R.id.pager_title_strip);  
                 //textView.setText(getArguments().getString("title"));
                 return view;
             }

        }

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

             @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.detailframent, container, false);  
                 //TextView textView = (TextView) view.findViewById(R.id.pager_title_strip);  
                 //textView.setText(getArguments().getString("title"));
                 return view;
             }

        }

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

             @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.detailframent, container, false);  
                 //TextView textView = (TextView) view.findViewById(R.id.pager_title_strip);  
                 //textView.setText(getArguments().getString("title"));
                 return view;
             }
             //end on create

        }//end default fragment

    }

答案 2 :(得分:0)

textview导致应用程序崩溃。我没有正确宣布它。

答案 3 :(得分:0)

mViewPager.setAdapter(mSectionsPagerAdapter);        
mViewPager = (ViewPager) findViewById(R.id.pager);  

交换上面的两行,看起来像是:

mViewPager = (ViewPager) findViewById(R.id.pager); 
mViewPager.setAdapter(mSectionsPagerAdapter);