繁体   English   中英

如何用片段交换ViewPager

[英]How to swap ViewPager with fragment

我有带有ViewPager和一些列表的选项卡栏,我可以单击它并将我的ViewPager放在fragmant后面。 我通过使用replace更改带有片段的android.support.v4.view.ViewPager时遇到一些问题。 这是我包含它们的布局:

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="6">
            <fragment
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/orders_fragment"
                android:name="fragments.OrderFragment1"
                tools:layout="@layout/fragment_2" />


            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/primaryColor"/>



        </RelativeLayout>

我正在尝试通过这种方式:

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            FragmentManager manager = getSupportFragmentManager();
            FragmentTransaction transaction=manager.beginTransaction();
            Fragment f1 = manager.findFragmentById(R.id.pager);
            transaction.remove(f1);
            transaction.commit();
        }
    });

我错了吗? 我可以这样吗? 还是有一些特殊的方式来做这种事情? 这是我的新手,因此,如果有人举一个例子或某些东西,那就不胜感激。

ViewPager应该是布局的fisrt元素。 现在,该片段可能无法点击且无法聚焦。 尝试这个:

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="6">

            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/primaryColor"/>

             <fragment
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/orders_fragment"
                android:name="fragments.OrderFragment1"
                tools:layout="@layout/fragment_2" />


        </RelativeLayout>

另外,这里有一个viewPager示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM