0

我有一個與ViewPager 3個片段進去。 一切工作正常,其中兩人。 問題在於第三個尋呼機這個是TabHost,每個選項卡中有一個片段。 但我讀過,它是不可能的Tabhost在一個片段。嵌套的碎片是禁止的。的Android ViewPager和片段設計

任何人有什麼我能爲解決我的問題做任何想法? 任何替代品?

+1

參閱這篇文章在[視圖尋呼機](http://android-developers.blogspot.com/2011/08/horizo​​ntal-view-swiping-with-viewpager.html)和這個職位超過[片段](HTTP: //developer.android.com/guide/topics/fundamentals/fragments.html)和這個[fragment tab pager](http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/ supportv4 /應用/ FragmentTabsPager.html)。 –

+0

謝謝Vineet,但我已經看過他們。我想我會改變我的設計,但我不知道如何。無論如何,再次感謝。我推薦這些文章來學習如何使用Fragments和ViewPagers。 – gutiory

回答

0

這就是我解決我的問題做。

我已經做了一個「自定義」的Tabhost「。我說」自定義「,因爲它不是一個Tabhost,它似乎只。 在ViewPager內我的分歧之一,我有4圖像的頂部。該片段和該片段的剩餘部分是一個的FrameLayout 我每次撫摸上的圖像,我替換這是在幀用於相應fragemnt片段

我添加一些代碼:

<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; 
} 

} 

我希望有自己的解釋很好。如果有人需要更多信息,請告訴我。

0

您可以參考在這個問題它會工作,按您的設計中提到的代碼。 Plese see