如何在android活动中设置viewpager,用抽屉扩展BaseActivity

时间:2015-11-03 16:56:12

标签: android android-fragments android-viewpager android-navigation-drawer

我有一个带BaseActivity的android项目,可以为其他活动实现抽屉。每件事都很好但我的问题是,当我需要在一个扩展活动中添加一个viewpager时,viewpager不起作用,tabhost也不显示! 我没有BaseActivity使用相同的代码,一切正常,但在这种情况下它不起作用。我想当我将FragmentManager活动传递给viewpager适配器时会出现问题。 有谁可以帮助我?

P.S:bellow是我的代码的一部分:

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" />

1 个答案:

答案 0 :(得分:0)

对于那些有同样问题的人,我找到了如下工作示例:

BaseActivity类:

return super(MyAdmin, self).get_some_results(request, input_string)

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):

<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布局:

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);
}
P.S:我不记得是什么问题(因为它是过去的),所以只需为那些有相同问题的人添加这个经过测试的答案。