3

之间的变化我有一个导航抽屉和content_frame内的ViewPagerIndicator。我想在导航抽屉的片段之间切换,但问题是在标签ViewPager之间切换的作品,但当我切换到另一个选项,你必须删除ViewPager并把一个新的片段放在这个片段下ViewPager片段导航抽屉viewpagerindicator

附加照片,所以你可以看到。

1)在这里,你已经可以看到下面ViewPager.It新片段可以通过显示的文本区分开来是Prueba

enter image description here

附代码

activity_main.xml中

<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android"  
android:id="@+id/drawer_layout" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

<FrameLayout   
    android:id="@+id/content_frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:id="@+id/content_linear" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <com.viewpagerindicator.TabPageIndicator 
      android:id="@+id/indicator" 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent"/> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/pager" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1"/> 
    </LinearLayout> 

</FrameLayout> 

<ListView 
android:id="@+id/left_drawer" 
android:layout_width="240dp" 
android:layout_height="match_parent" 
android:layout_gravity="start" 
android:choiceMode="singleChoice" 
android:divider="@android:color/transparent" 
android:dividerHeight="0dp" 
android:background="#111"/> 

在片段之间切换的方法。

/** Main content by replacing fragments 
* @param position 
*/ 
private void selectItem(int position) { 
    //Code 
    Fragment fragment = new TryFragment(); 
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 

    switch (position) { 
    case 0: 
     mPager.setCurrentItem(0); 
     break; 

    case 1: 
     mPager.setCurrentItem(1); 
     break; 

    case 2: 

     ft.replace(R.id.content_frame, fragment); 

     break; 

    }   
    ft.commit(); 
    mAdapter.notifyDataSetChanged(); 
    //Close drawer 
    mDrawerLayout.closeDrawer(mDrawerList); 
} 

类TryFragment

class TryFragment extends Fragment 
{ 
    public static final String ARG_PLANET_NUMBER = "planet_number"; 

    public TryFragment() 
    { 
     // Empty constructor required for fragment subclasses 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
      TextView text = new TextView(getActivity()); 
      text.setGravity(Gravity.CENTER); 
      text.setText("Prueba"); 
      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); 

      Log.d("TestFragment", "Estoy en el fragment"); 

      return layout; 
    } 
} 

我不知道如果我要摧毁viewpager以显示新的片段或层级中的XML的一个问题。

回答

1

你的ViewPager必须是Fragment,它应该放置在layout.content_frame中,所以当你调用ft.replace(R.id.content_frame,fragment);您的ViewPager片段将被替换。

+0

我不了解你。 – TeRRo

+0

据我所知,您需要替换ViewPager时,从导航抽屉选择另一个项目。因此,您需要将ViewPager分开,并在需要时将其分配给contant_frame。 –

+0

我有3个选项,第一个和第二个标签是ViewPager,第三个是我必须删除ViewPager显示该片段。 – TeRRo

0

您不应该同时使用ViewPager和NavigationDrawer。你可以看到罗马Nurik在这个G +后回答: https://plus.google.com/u/1/+DavidTaSmoochibi/posts/8dWEkFcbTFX

“你不应该使用与操作栏中的标签导航抽屉如果你瞄准了类似的UI谷歌Play音乐,你应该实现标签(并且要小心平板电脑的外观 - 您不希望在平板电脑上使用全宽标签栏)另外,请务必查看今年Google I/O的Android App Design会话中的结构,通过可用于公开应用层次结构的各种接口模式。“

+0

问题是,如果我使用操作栏选项卡显示在选项卡上,这显示在选项卡下面。唯一没有发生在我身上的事情是ViewPager。 – TeRRo