私はViewPager
それに3つのフラグメントを持っています。2人ですべてうまくいきます。問題は 3 番目のページャTabHost
にあります。これは で、各タブに 1 つのフラグメントがあります。Tabhost
しかし、フラグメント内に配置することはできないと読みました。ネストされたフラグメントは禁止されています。
私の問題を解決するために何ができるか、誰にも分かりますか? 代替案はありますか?
私はViewPager
それに3つのフラグメントを持っています。2人ですべてうまくいきます。問題は 3 番目のページャTabHost
にあります。これは で、各タブに 1 つのフラグメントがあります。Tabhost
しかし、フラグメント内に配置することはできないと読みました。ネストされたフラグメントは禁止されています。
私の問題を解決するために何ができるか、誰にも分かりますか? 代替案はありますか?
これは私の問題を解決するために私がしたことです。
私は1つの「カスタム」タブホストを実行しました。タブホストではないため「カスタム」と言いますが、それはただのようです。ViewPager内のフラグメントの1つで、フラグメントの上に4つの画像があります。フラグメントの残りは1つの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;
}
}
私は自分自身をうまく説明したことを望みます。誰かがより多くの情報を必要とするならば、ただ教えてください。
この問題に記載されているコードを参照すると、設計どおりに機能します。見てください