0

我有一個Android項目,其中BaseActivity爲其他活動實現抽屜。每一件事情都是okey,但我的問題是,當我需要在一個擴展活動中添加viewpager時,viewpager不起作用,並且tabhost不顯示! 我使用了相同的代碼,沒有BaseActivity,一切正常,但在這種情況下,它不起作用。我猜想當我將活動的FragmentManager傳遞給viewpager適配器時,出現了問題。 任何人都可以幫助我嗎?如何在Android活動中設置ViewPager使用抽屜擴展BaseActivity

PS:波紋管是我的代碼部分:

BaseActivity類:

public abstract class BaseActivity extends ActionBarActivity { 

private DrawerLayout mDrawer; 
private RecyclerView mRecycler; 
private RecyclerView.Adapter mAdapter; 
private Toolbar toolbar; 
private RelativeLayout FullLayout; 
private FrameLayout ContentLayout; 

@Override 
public void setContentView(int layoutResID) { 
    FullLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.activity_base, null); 
    ContentLayout = (FrameLayout) FullLayout.findViewById(R.id.content_frame); 
    getLayoutInflater().inflate(layoutResID, ContentLayout, true); 
    super.setContentView(FullLayout); 

    //Drawer Initialization 
    InitilizeDrawer(); 
} 
}  

MainActivity類別(已ViewPager):

public class MainActivity extends BaseActivity { 

private StaticMaterialTabs tabHost; 
private ViewPager pager; 
private MainTabbarAdapter pagerAdapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    //Init View Pager 
    tabHost = (StaticMaterialTabs) findViewById(R.id.tabHost); 
    pager = (ViewPager) findViewById(R.id.pager); 
    pagerAdapter = new MainTabbarAdapter(getSupportFragmentManager()); 
    pager.setAdapter(pagerAdapter); 
    tabHost.setViewPager(pager); 

} 
} 

BaseActivity佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

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

<android.support.v4.widget.DrawerLayout 
    android:id="@+id/DrawerLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:elevation="7dp"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <include 
      android:id="@+id/toolbar" 
      layout="@layout/toolbar"></include> 

    </LinearLayout> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/RecyclerView" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="right" 
     android:background="@color/White" 
     android:scrollbars="vertical"></android.support.v7.widget.RecyclerView> 
</android.support.v4.widget.DrawerLayout> 

MainActivity佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="right" 
android:orientation="vertical"> 

<ir.MaterialTab.StaticMaterialTabs 
    android:id="@+id/tabHost" 
    android:layout_width="match_parent" 
    android:layout_height="48dp" 
    android:background="@color/ColorPrimary" 
    android:textColor="@color/Gray" 
    app:mtIndicatorColor="@color/ScrollColor" 
    app:mtMrlRippleColor="@color/ScrollColor" 
    app:mtPaddingMiddle="false" 
    app:mtSameWeightTabs="false" 
    app:mtTextColorSelected="@color/White" /> 

<android.support.v4.view.ViewPager 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/tabHost" /> 

+0

究竟出了什麼問題?它會崩潰嗎?意見只是不可見?擴展BaseActivity時有其他視圖嗎? – HannahMitt

+0

@HannahMitt沒有崩潰,viewpager(tabhost)不顯示,只是它們的第一個片段變得可見!是的其他與抽屜活動是okey。 – Evil

+0

這聽起來像你的tabhost可能只是沒有正確設置。 – HannahMitt

回答

0

對於那些誰有同樣的問題,我發現下面的工作示例:

BaseActivity類:

public abstract class BaseActivity extends ActionBarActivity { 

private DrawerLayout mDrawer; 
private RecyclerView mRecycler; 
private RecyclerView.Adapter mAdapter; 
private Toolbar toolbar; 
private RelativeLayout FullLayout; 
private FrameLayout ContentLayout; 

@Override 
public void setContentView(int layoutResID) { 
    FullLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.activity_base, null); 
    ContentLayout = (FrameLayout) FullLayout.findViewById(R.id.content_frame); 
    getLayoutInflater().inflate(layoutResID, ContentLayout, true); 
    super.setContentView(FullLayout); 

    //Drawer Initialization 
    InitilizeDrawer(); 
} 

BaseActivity佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<android.support.v4.widget.DrawerLayout 
    android:id="@+id/DrawerLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:elevation="7dp"> 

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

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/RecyclerView" 
     android:layout_width="280dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="right" 
     android:background="@color/White" 
     android:scrollbars="vertical"></android.support.v7.widget.RecyclerView> 

</android.support.v4.widget.DrawerLayout> 

MainActivity類別(帶ViewPager):

public class MainActivity extends BaseActivity { 

private StaticMaterialTabs tabHost; 
private ViewPager pager; 
private MainTabbarAdapter pagerAdapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    //Init View Pager 
    tabHost = (StaticMaterialTabs) findViewById(R.id.tabHost); 
    pager = (ViewPager) findViewById(R.id.pager); 
    pagerAdapter = new MainTabbarAdapter(getSupportFragmentManager()); 
    pager.setAdapter(pagerAdapter); 
    tabHost.setViewPager(pager); 
} 

MainActivity佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="right" 
android:orientation="vertical"> 

<include 
    android:id="@+id/toolbar" 
    layout="@layout/toolbar"></include> 

<ir.MaterialTab.StaticMaterialTabs 
    android:id="@+id/tabHost" 
    android:layout_width="match_parent" 
    android:layout_height="48dp" 
    android:background="@color/ColorPrimary" 
    android:textColor="@color/Gray" 
    app:mtIndicatorColor="@color/ScrollColor" 
    app:mtMrlRippleColor="@color/ScrollColor" 
    app:mtPaddingMiddle="false" 
    app:mtSameWeightTabs="false" 
    app:mtTextColorSelected="@color/White" /> 

<android.support.v4.view.ViewPager 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/tabHost" /> 

</LinearLayout> 

PS:我不記得是什麼問題(因爲它是過去),所以只需爲具有相同問題的人添加此測試答案。

+0

感謝您的回覆@魔鬼。將試試這個,讓你現在 – Bhavana

+0

@Bhavana你的歡迎,請讓我現在,如果這不起作用。 – Evil

相關問題