在片段、导航抽屉和视图页面指示器之间切换

3
我有一个导航抽屉和一个ViewPagerIndicator放在content_frame内部。我希望能够通过导航抽屉之间切换fragment,但问题在于切换tabs ViewPager可行,但当我切换到另一个选项时,必须移除ViewPager并放置一个新的fragment,而这个新的fragment会被放置在ViewPager下方。
附上图片以供参考。
1)您可以在此处看到位于ViewPager下方的新fragment。它的标识是显示为Prueba的文本。
附上代码 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以显示新的Fragment,还是XML文件中存在层次结构问题。

2个回答

1
您的ViewPager必须是Fragment,并且应放置在layout.content_frame中,因此当您调用ft.replace(R.id.content_frame, fragment);时,您的ViewPager Fragment将被替换。

我不明白你。 - joselufo
据我理解,您需要在从导航抽屉中选择另一个项目时替换ViewPager。因此,您需要将ViewPager放置到单独的Fragment中,并在需要时将其放置到contant_frame中。 - Kalyaganov Alexey
我有三个选项,第一个和第二个选项卡是ViewPager,第三个选项是我必须删除ViewPager以显示该片段。 - joselufo
然后,当选择选项3时,您需要隐藏content_linear。 - Kalyaganov Alexey
findViewById(R.id.content_linear).setVisibility(View.GONE); ft.replace(R.id.content_frame, fragment); - Kalyaganov Alexey
显示剩余2条评论

0

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

“不应该使用带操作栏标签的导航抽屉。如果您的目标是实现与Google Play音乐类似的UI,您应该手动实现选项卡(并注意在平板电脑上的外观 - 您不希望在平板电脑上有全宽度的选项卡栏)。还要确保查看今年Google I/O中的Android应用设计结构会话,以详细了解可用于公开应用程序层次结构的各种界面模式。”


问题在于,如果我使用ActionBar选项卡,它会显示在选项卡上方,而不是下方。唯一没有发生这种情况的是ViewPager。 - joselufo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接