• <bdo id='glnIa'></bdo><ul id='glnIa'></ul>
        <tfoot id='glnIa'></tfoot>

        <small id='glnIa'></small><noframes id='glnIa'>

        <i id='glnIa'><tr id='glnIa'><dt id='glnIa'><q id='glnIa'><span id='glnIa'><b id='glnIa'><form id='glnIa'><ins id='glnIa'></ins><ul id='glnIa'></ul><sub id='glnIa'></sub></form><legend id='glnIa'></legend><bdo id='glnIa'><pre id='glnIa'><center id='glnIa'></center></pre></bdo></b><th id='glnIa'></th></span></q></dt></tr></i><div id='glnIa'><tfoot id='glnIa'></tfoot><dl id='glnIa'><fieldset id='glnIa'></fieldset></dl></div>

      1. <legend id='glnIa'><style id='glnIa'><dir id='glnIa'><q id='glnIa'></q></dir></style></legend>

        如何在片段中使用 ViewPager

        时间:2023-10-04

      2. <i id='A5Cbw'><tr id='A5Cbw'><dt id='A5Cbw'><q id='A5Cbw'><span id='A5Cbw'><b id='A5Cbw'><form id='A5Cbw'><ins id='A5Cbw'></ins><ul id='A5Cbw'></ul><sub id='A5Cbw'></sub></form><legend id='A5Cbw'></legend><bdo id='A5Cbw'><pre id='A5Cbw'><center id='A5Cbw'></center></pre></bdo></b><th id='A5Cbw'></th></span></q></dt></tr></i><div id='A5Cbw'><tfoot id='A5Cbw'></tfoot><dl id='A5Cbw'><fieldset id='A5Cbw'></fieldset></dl></div>
                <tbody id='A5Cbw'></tbody>

              • <bdo id='A5Cbw'></bdo><ul id='A5Cbw'></ul>
                <legend id='A5Cbw'><style id='A5Cbw'><dir id='A5Cbw'><q id='A5Cbw'></q></dir></style></legend>

                <small id='A5Cbw'></small><noframes id='A5Cbw'>

                  <tfoot id='A5Cbw'></tfoot>
                  本文介绍了如何在片段中使用 ViewPager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我下载了一个关于 ViewPager 和 CirclePageIndicator 的项目.它在我的平板电脑上运行良好,其代码是:

                  I download a project about ViewPager and CirclePageIndicator. It works well on my tablet and its code is :

                  Test1Activity.java:

                  Test1Activity.java:

                  TestFragmentAdapter mAdapter;
                  ViewPager mPager;
                  @Override
                  protected void onCreate(Bundle savedInstanceState) {
                      super.onCreate(savedInstanceState);
                  
                  
                      //The look of this sample is set via a style in the manifest
                      setContentView(R.layout.simple_circles);
                  
                      mAdapter = new TestFragmentAdapter(getSupportFragmentManager());
                  
                      mPager = (ViewPager)findViewById(R.id.pager);
                      mPager.setAdapter(mAdapter);
                  
                      CirclePageIndicator indicator = (CirclePageIndicator)findViewById(R.id.indicator);
                      indicator.setViewPager(mPager);
                  }   
                  

                  TestFragmentAdapter.java:

                  TestFragmentAdapter.java:

                  class TestFragmentAdapter extends FragmentPagerAdapter {
                  protected static final String[] CONTENT = new String[] { "Page1", "Page2", "Page3", "Page4", };
                  
                  private int mCount = CONTENT.length;
                  
                  public TestFragmentAdapter(FragmentManager fragmentManager) {
                      super(fragmentManager);
                  }
                  
                  @Override
                  public Fragment getItem(int position) {
                      return TestFragment.newInstance(CONTENT[position % CONTENT.length]);
                  }
                  
                  @Override
                  public int getCount() {
                      return mCount;
                  }
                  
                  public void setCount(int count) {
                      if (count > 0 && count <= 10) {
                          mCount = count;
                          notifyDataSetChanged();
                      }
                  }}
                  

                  测试片段:

                  public final class TestFragment extends Fragment {
                  private static final String KEY_CONTENT = "TestFragment:Content";
                  
                  public static TestFragment newInstance(String content) {
                      TestFragment fragment = new TestFragment();
                  
                      fragment.mContent = content;
                  
                      return fragment;
                  }
                  
                  private String mContent = "???";
                  
                  @Override
                  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                      if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
                          mContent = savedInstanceState.getString(KEY_CONTENT);
                      }
                  
                      TextView text = new TextView(getActivity());
                      text.setGravity(Gravity.CENTER);
                      text.setText(mContent);
                      text.setTextSize(20 * getResources().getDisplayMetrics().density);
                      text.setPadding(20, 20, 20, 20);
                  
                      LinearLayout layout = new LinearLayout(getActivity());
                      layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
                      layout.setGravity(Gravity.CENTER);
                      layout.addView(text);
                  
                      return layout;
                  }
                  
                  @Override
                  public void onSaveInstanceState(Bundle outState) {
                      super.onSaveInstanceState(outState);
                      outState.putString(KEY_CONTENT, mContent);
                  }}
                  

                  现在我想在 Fragment 而不是 Activity 中使用这个 ViewPager,但我很困惑.TestFragmentAdapter 的构造函数接受类型为 android.support.v4.app.FragmentManager 的参数,但在片段中我只能使用 getFragmentManager() 来获取 android.app.FragmentManager 类型,因此

                  now I want to use this ViewPager in a Fragment instead of Activity, but I am puzzled. The constructor of TestFragmentAdapter accept a parameter with type android.support.v4.app.FragmentManager, but in a fragment I can only use getFragmentManager() to get an android.app.FragmentManager type, thus the

                  mAdapter = new TestFragmentAdapter(getSupportFragmentManager());
                  

                  总是错的.我必须在几天内完成我的项目,你能告诉我如何修改我的代码以在片段中实现相同的功能吗?

                  is always wrong. I must finish my project within days, so can u tell me how to modify my code to achieve same function in a fragment?

                  谢谢!!

                  推荐答案

                  把这段代码放到你的MainActivity中.下面的代码将在您的布局中加载片段.

                  Put this code in your MainActivity. The below code will load the Fragment in your Layout.

                  第 1 步

                  Fragment fragment = new PageSlider();
                  FragmentManager manager = getSupportFragmentManager();
                  manager.beginTransaction().replace(R.id.content, fragment).commit();
                  

                  接下来我们需要实现 ViewPager

                  Next we need to implement the ViewPager

                  第 2 步

                  public class PageSlider extends Fragment {
                  public PageSlider() {
                  // Required empty public constructor
                  }
                  @Override
                  public void onCreate(Bundle savedInstanceState) {
                      super.onCreate(savedInstanceState);
                  }
                  @Override
                  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                  Bundle savedInstanceState) {
                  // Inflate the layout for this fragment
                      return inflater.inflate(R.layout.page_slider, container, false);
                  }
                  @Override
                  public void onViewCreated(View view, Bundle savedInstanceState) {
                  // TODO Auto-generated method stub
                    super.onViewCreated(view, savedInstanceState);
                    PagerSlidingTabStrip tab = (PagerSlidingTabStrip) view
                  .findViewById(com.akshay.protocol10.asplayer.R.id.tabs);
                  
                   ViewPager pager=(ViewPager)view  .findViewById(com.akshay.protocol10.asplayer.R.id.view_pager);
                  MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager());
                  pager.setAdapter(adapter);
                  tab.setViewPager(pager);
                  }
                  // TODO: Rename method, update argument and hook method into UI event
                  public void onButtonPressed(Uri uri) {
                  }
                  @Override
                  public void onAttach(Activity activity) {
                  super.onAttach(activity);
                  }
                  @Override
                  public void onDetach() {
                  super.onDetach();
                  }
                  public interface OnFragmentInteractionListener {
                  // TODO: Update argument type and name
                  public void onFragmentInteraction(Uri uri);
                  }
                  }
                  

                  第 3 步:创建适配器

                      public class MyPagerAdapter extends FragmentPagerAdapter {
                          private final String[] TITLES = { "Album", "Artist", "Songs", "Genre" };
                          public MyPagerAdapter(FragmentManager childFragmentManager) {
                        // TODO Auto-generated constructor stub
                      super(childFragmentManager);
                      }
                      @Override
                      public CharSequence getPageTitle(int position) {
                      // TODO Auto-generated method stub
                      return TITLES[position];
                      }
                      @Override
                      public Fragment getItem(int index) {
                      // TODO Auto-generated method stub
                         Fragment fragment = null;
                         if (index == 0) {
                         fragment = new Albums();
                         } else if (index == 1) {
                         fragment = new ArtistFragment();
                         } else if (index == 2) {
                          fragment = new TracksFragment();
                         } else if (index == 3) {
                         fragment = new GenreFragment();
                        }
                       return fragment;
                      }
                  
                      @Override
                      public int getCount() {
                      // TODO Auto-generated method stub
                      return TITLES.length;
                      }
                     }
                  

                  这篇关于如何在片段中使用 ViewPager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:android ViewPager xml膨胀错误 下一篇:viewPager中的按钮onClick?

                  相关文章

                1. <small id='VsZUl'></small><noframes id='VsZUl'>

                  1. <legend id='VsZUl'><style id='VsZUl'><dir id='VsZUl'><q id='VsZUl'></q></dir></style></legend>
                    <tfoot id='VsZUl'></tfoot>
                    1. <i id='VsZUl'><tr id='VsZUl'><dt id='VsZUl'><q id='VsZUl'><span id='VsZUl'><b id='VsZUl'><form id='VsZUl'><ins id='VsZUl'></ins><ul id='VsZUl'></ul><sub id='VsZUl'></sub></form><legend id='VsZUl'></legend><bdo id='VsZUl'><pre id='VsZUl'><center id='VsZUl'></center></pre></bdo></b><th id='VsZUl'></th></span></q></dt></tr></i><div id='VsZUl'><tfoot id='VsZUl'></tfoot><dl id='VsZUl'><fieldset id='VsZUl'></fieldset></dl></div>
                        <bdo id='VsZUl'></bdo><ul id='VsZUl'></ul>