如何在 android activity 中设置 viewpager,它使用抽屉扩展 BaseActivity
how to set viewpager in android activity which extend BaseActivity with drawer
我有一个带有 BaseActivity
的 android 项目,它为其他活动实现抽屉。一切都很好,但我的问题是,当我需要在一个扩展 activity 中添加一个 viewpager 时,viewpager 不起作用并且 tabhost 不显示!
我使用了没有 BaseActivity
的相同代码并且一切正常,但在这种情况下它不起作用。我想当我将 activity 的 FragmentManager
传递给 viewpager 适配器时出了点问题。
谁能帮帮我?
P.S:波纹管是我的代码的一部分:
基础活动Class:
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 Class(有 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" />
对于那些有同样问题的人,我找到了如下工作示例:
BaseActivity Class:
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 Class(使用 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>
P.S: 我不记得是什么问题了(因为它是过去的),所以为那些有同样问题的人添加这个经过测试的答案。
我有一个带有 BaseActivity
的 android 项目,它为其他活动实现抽屉。一切都很好,但我的问题是,当我需要在一个扩展 activity 中添加一个 viewpager 时,viewpager 不起作用并且 tabhost 不显示!
我使用了没有 BaseActivity
的相同代码并且一切正常,但在这种情况下它不起作用。我想当我将 activity 的 FragmentManager
传递给 viewpager 适配器时出了点问题。
谁能帮帮我?
P.S:波纹管是我的代码的一部分:
基础活动Class:
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 Class(有 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" />
对于那些有同样问题的人,我找到了如下工作示例:
BaseActivity Class:
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 Class(使用 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>
P.S: 我不记得是什么问题了(因为它是过去的),所以为那些有同样问题的人添加这个经过测试的答案。