0

我想在我的应用程序中使用 JakeWharton 库。我对示例中的示例 SampleTabsDefault 很感兴趣:在此处输入图像描述 现在,我被卡住了。在这个示例中只更改文本,我想更改片段(每个片段都有自己的布局,每个布局都有不同的控件,如按钮、复选框等)我想我必须在从 FragmentPagerAdapter 扩展的类中更改此方法:

public Fragment getItem(int position) {
            return TestFragment.newInstance(CONTENT[position % CONTENT.length]);
        }

我想根据特定位置创建新片段(具有自己的布局)。

我已经实现了这段代码:

public class MainActivity extends FragmentActivity {
    private static final String[] CONTENT = new String[] {"My Profile", "Statistics"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentPagerAdapter adapter = new GoogleMusicAdapter(getSupportFragmentManager());

        ViewPager pager = (ViewPager)findViewById(R.id.pager);
        pager.setAdapter(adapter);

        TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);
        indicator.setViewPager(pager);



        }


    class GoogleMusicAdapter extends FragmentPagerAdapter {
        public GoogleMusicAdapter(FragmentManager fm) {
            super(fm);
        }


        public Fragment getItem(int position) {
            if (position==0)
            return MyProfile.newInstance(CONTENT[position % CONTENT.length]);
            else if (position==1)
            return Statistics.newInstance(CONTENT[position % CONTENT.length]);
            return null;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return CONTENT[position % CONTENT.length].toUpperCase();
        }

        @Override
        public int getCount() {
          return CONTENT.length;
        }
    }
}

public class MyProfile extends Fragment {
      private static final String KEY_CONTENT = "TestFragment:Content";


        public static MyProfile newInstance(String content) {
            MyProfile fragment = new MyProfile();

            fragment.mContent = content;

            return fragment;
        }

        private String mContent = "???";

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

            if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
                mContent = savedInstanceState.getString(KEY_CONTENT);
            }


        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_basic, container, false);
            Button button = (Button) view.findViewById(R.id.fragment_button);

            button.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    Activity activity = getActivity();

                    if (activity != null) {
                        Toast.makeText(activity, "fragmen1", Toast.LENGTH_SHORT).show();
                    }
                }

            });

            return view;
        }
           public void onSaveInstanceState(Bundle outState) {
                super.onSaveInstanceState(outState);
                outState.putString(KEY_CONTENT, mContent);
            }
}

还有这个:

public class Statistics extends Fragment {
      private static final String KEY_CONTENT = "TestFragment:Content";


        public static Statistics newInstance(String content) {
            Statistics fragment = new Statistics();

            fragment.mContent = content;

            return fragment;
        }

        private String mContent = "???";

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

            if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
                mContent = savedInstanceState.getString(KEY_CONTENT);
            }


        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_basic2, container, false);
            Button button = (Button) view.findViewById(R.id.fragment_button);

            button.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    Activity activity = getActivity();

                    if (activity != null) {
                        Toast.makeText(activity, "fragmen2", Toast.LENGTH_SHORT).show();
                    }
                }

            });

            return view;
        }

           @Override
            public void onSaveInstanceState(Bundle outState) {
                super.onSaveInstanceState(outState);
                outState.putString(KEY_CONTENT, mContent);
            }
}

它不起作用...我无法切换页面,并且我的片段布局不显示...

4

1 回答 1

2

在您的getItem()方法中,您FragmentPagerAdapter可以根据位置返回其他片段。例如:

 public Fragment getItem(int position) {
 switch(position)
       {
               case 0:
                    TestFragment fragment = new TestFragment();  
                    return fragment;

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

         }

                    DefaultFragment fragment3 = new DefaultFragment();  
                    return fragment3;

         }
于 2012-12-05T21:20:44.773 回答