Android ViewPager和fragment设计

发布于 2024-12-10 00:33:20 字数 181 浏览 1 评论 0原文

我有一个 ViewPager,里面有 3 个片段。 他们两个的一切都很好。 问题出在第三个寻呼机上。这是一个 TabHost,其中每个选项卡中有一个片段。 但我读到不可能将 Tabhost 放入片段中。禁止嵌套片段。

任何人都知道我可以做些什么来解决我的问题? 还有其他选择吗?

I have a ViewPager with 3 fragments into it.
Everything works fine with two of them.
The problem is with the third pager This one is a TabHost, in which there is one fragment in each tab.
But I've read that it's is not possible to put a Tabhost inside a Fragment. Nested fragments are forbidden.

Anyone has any idea of what can I do for resolving my problem?
Any alternatives?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流云如水 2024-12-17 00:33:20

您可以参考此问题中提到的代码,它将按照您的设计工作。请参阅

You can refer the code as mentioned in this problem it will work as per your design.Plese see

陪我终i 2024-12-17 00:33:20

这就是我为解决我的问题所做的事情。

我已经做了一个“自定义”Tabhost”。我说“自定义”是因为它不是 Tabhost,似乎只是。
在 ViewPager 内的一个片段中,片段顶部有 4 个图像。片段的其余部分是一个 FrameLayout。
每次我触摸图像时,我都会替换相应片段的帧中的片段。

我添加了一些代码:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content"
android:orientation="horizontal">
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow >
<LinearLayout android:id="@+id/button1"
android:orientation="vertical"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="button1Click"
>
<ImageView android:id="@+id/iv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic1"/>
<TextView android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/text1"/>
</LinearLayout>
<LinearLayout android:id="@+id/button2"
android:orientation="vertical"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="button2Click"
>
<ImageView android:id="@+id/iv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic2"/>
<TextView android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/text2"/>
</LinearLayout>
<LinearLayout android:id="@+id/button3"
android:orientation="vertical"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="button3Click"
>
<ImageView android:id="@+id/iv3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic3"/>
<TextView android:id="@+id/tv3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/text3"/>
</LinearLayout>
<LinearLayout android:id="@+id/button4"
android:orientation="vertical"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="button4Click"
>
<ImageView android:id="@+id/iv4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic4"/>
<TextView android:id="@+id/tv4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/text4"/>
</LinearLayout>

</TableRow>
</TableLayout>
</LinearLayout>


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

</FrameLayout>

</LinearLayout>

在 FragmentActivity 中:

public void boton1Click(View v){
    mFragmentInViewPager.changeFragment(BUTTON1);
}

public void boton2Click(View v){
    mFragmentInViewPager.changeFragment(BUTTON2);
}

public void boton3Click(View v){
    mFragmentInViewPager.changeFragment(BUTTON3);
}
public void boton4Click(View v){
    mFragmentInViewPager.changeFragment(BUTTON4);
}

最后在 FragmentInViewPager (包含其他 4 个片段的代码)中,

public void changeFragment(int fragmentId)
{
if(mCurrentFragmentId != fragmentId) {

switch(fragmentId) {
case BUTTON1:

    mFTransaction = mFManager.beginTransaction();
    mFTransaction.replace(R.id.layout_actual, mFragment1);
    mFTransaction.commit();
    mIv1.setImageResource(R.drawable.ic1_sel);
    mIv2.setImageResource(R.drawable.ic2);
    mIv3.setImageResource(R.drawable.ic3);
    mIv4.setImageResource(R.drawable.ic4);
    break;

case BUTTON2:
    mFTransaction = mFManager.beginTransaction();
    mFTransaction.replace(R.id.layout_actual, mFragment2);
    mFTransaction.commit();
    mIv1.setImageResource(R.drawable.ic1);
    mIv2.setImageResource(R.drawable.ic2_sel);
    mIv3.setImageResource(R.drawable.ic3);
    mIv4.setImageResource(R.drawable.ic4);
    break;

case BUTTON3:
    mFTransaction = mFManager.beginTransaction();
    mFTransaction.replace(R.id.layout_actual, mFragment3);
    mFTransaction.commit();
    mIv1.setImageResource(R.drawable.ic1);
    mIv2.setImageResource(R.drawable.ic2);
    mIv3.setImageResource(R.drawable.ic3_sel);
    mIv4.setImageResource(R.drawable.ic4);
    break;

case BUTTON4:
    mFTransaction = mFManager.beginTransaction();
    mFTransaction.replace(R.id.layout_actual, mFragment4);
    mFTransaction.commit();
    mIv1.setImageResource(R.drawable.ic1);
    mIv2.setImageResource(R.drawable.ic2);
    mIv3.setImageResource(R.drawable.ic3);
    mIv4.setImageResource(R.drawable.ic4_sel);
    break;

}

mCurrentFragmentId = fragmentId;
}

}

我希望能够很好地解释自己。如果有人需要更多信息,请告诉我。

This is what i've done for resolving my problem.

I've done one "custom" Tabhost". I say "custom" because it is not a Tabhost, it seems to only.
In one of my fragements inside the ViewPager, i've 4 images on top of the fragment. And the rest of the fragment is one FrameLayout.
Each time i touch on the images, i replace the fragment which is in the frame for the respective fragemnt.

I add some code:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content"
android:orientation="horizontal">
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow >
<LinearLayout android:id="@+id/button1"
android:orientation="vertical"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="button1Click"
>
<ImageView android:id="@+id/iv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic1"/>
<TextView android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/text1"/>
</LinearLayout>
<LinearLayout android:id="@+id/button2"
android:orientation="vertical"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="button2Click"
>
<ImageView android:id="@+id/iv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic2"/>
<TextView android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/text2"/>
</LinearLayout>
<LinearLayout android:id="@+id/button3"
android:orientation="vertical"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="button3Click"
>
<ImageView android:id="@+id/iv3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic3"/>
<TextView android:id="@+id/tv3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/text3"/>
</LinearLayout>
<LinearLayout android:id="@+id/button4"
android:orientation="vertical"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="button4Click"
>
<ImageView android:id="@+id/iv4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic4"/>
<TextView android:id="@+id/tv4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/text4"/>
</LinearLayout>

</TableRow>
</TableLayout>
</LinearLayout>


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

</FrameLayout>

</LinearLayout>

In the FragmentActivity:

public void boton1Click(View v){
    mFragmentInViewPager.changeFragment(BUTTON1);
}

public void boton2Click(View v){
    mFragmentInViewPager.changeFragment(BUTTON2);
}

public void boton3Click(View v){
    mFragmentInViewPager.changeFragment(BUTTON3);
}
public void boton4Click(View v){
    mFragmentInViewPager.changeFragment(BUTTON4);
}

And finally in the FragmentInViewPager (the one which contains the other 4 fragments)

public void changeFragment(int fragmentId)
{
if(mCurrentFragmentId != fragmentId) {

switch(fragmentId) {
case BUTTON1:

    mFTransaction = mFManager.beginTransaction();
    mFTransaction.replace(R.id.layout_actual, mFragment1);
    mFTransaction.commit();
    mIv1.setImageResource(R.drawable.ic1_sel);
    mIv2.setImageResource(R.drawable.ic2);
    mIv3.setImageResource(R.drawable.ic3);
    mIv4.setImageResource(R.drawable.ic4);
    break;

case BUTTON2:
    mFTransaction = mFManager.beginTransaction();
    mFTransaction.replace(R.id.layout_actual, mFragment2);
    mFTransaction.commit();
    mIv1.setImageResource(R.drawable.ic1);
    mIv2.setImageResource(R.drawable.ic2_sel);
    mIv3.setImageResource(R.drawable.ic3);
    mIv4.setImageResource(R.drawable.ic4);
    break;

case BUTTON3:
    mFTransaction = mFManager.beginTransaction();
    mFTransaction.replace(R.id.layout_actual, mFragment3);
    mFTransaction.commit();
    mIv1.setImageResource(R.drawable.ic1);
    mIv2.setImageResource(R.drawable.ic2);
    mIv3.setImageResource(R.drawable.ic3_sel);
    mIv4.setImageResource(R.drawable.ic4);
    break;

case BUTTON4:
    mFTransaction = mFManager.beginTransaction();
    mFTransaction.replace(R.id.layout_actual, mFragment4);
    mFTransaction.commit();
    mIv1.setImageResource(R.drawable.ic1);
    mIv2.setImageResource(R.drawable.ic2);
    mIv3.setImageResource(R.drawable.ic3);
    mIv4.setImageResource(R.drawable.ic4_sel);
    break;

}

mCurrentFragmentId = fragmentId;
}

}

I hope of have explained myself well. If anyone needs more info, just tell me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文