我在viewPager中进行了一些实验 一个viewpager与不同的FragmentAdapter 目前代码工作正常,但我不知道它是否是正确的方法。
我的活动就是这个。
public class Navigation extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener, View.OnClickListener {
private DrawerLayout drawer;
private ViewPager pager;
public static TabHost tabHost;
public static TabWidget tabWidget;
public static FloatingActionMenu fab;
private boolean doubleBackToExitPressedOnce = false;
private String currentTab;
private Button mButton;
private LinearLayout mLinearLayout;
private PagerSlidingTabStrip mTabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().getAttributes().windowAnimations = 0;
setContentView(R.layout.activity_navigation);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
fab = (FloatingActionMenu) findViewById(R.id.fabMenu);
fab.setVisibility(View.GONE);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
tabHost = (TabHost) findViewById(R.id.tabHost);
tabHost.setup();
tabHost.setOnTabChangedListener(this);
tabWidget = (TabWidget) findViewById(android.R.id.tabs);
setNewTab(tabHost, "tab1", R.string.textTabTitle1, R.drawable.icon_search, R.id.tab1);
setNewTab(tabHost, "tab2", R.string.textTabTitle1, R.drawable.icon_comment, R.id.tab2);
setNewTab(tabHost, "tab3", R.string.textTabTitle1, R.drawable.icon_car, R.id.tab3);
mTabLayout = (PagerSlidingTabStrip) findViewById(R.id.tab);
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new FragmentAdapterSearch(getSupportFragmentManager()));
mTabLayout.setViewPager(pager);
pager.addOnPageChangeListener(this);
mButton = (Button) findViewById(R.id.btnSearch);
mLinearLayout = (LinearLayout) findViewById(R.id.btnSelectLocation);
mButton.setOnClickListener(this);
mLinearLayout.setOnClickListener(this);
TabIcon.setColor(0, mTabLayout, iconSearchOpac, iconSearch);
}
private void setNewTab(TabHost tabHost, String tag, int title, int icon, int contentID) {
TabHost.TabSpec tabSpec = tabHost.newTabSpec(tag);
tabSpec.setIndicator(getTabIndicator(tabHost.getContext(), title, icon));
tabSpec.setContent(contentID);
tabHost.addTab(tabSpec);
}
private View getTabIndicator(Context context, int title, int icon) {
View view = LayoutInflater.from(context).inflate(R.layout.tab_layout, null);
final ImageView iv = (ImageView) view.findViewById(R.id.imageView);
iv.setImageResource(icon);
TextView tv = (TextView) view.findViewById(R.id.textView);
tv.setText(title);
return view;
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else if (!drawer.isDrawerOpen(GravityCompat.START)) {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Tap Again to Exit Toweelo", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2000);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.navigation, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
if (drawer.isDrawerOpen(Gravity.LEFT)) {
drawer.closeDrawer(Gravity.LEFT);
} else {
drawer.openDrawer(Gravity.LEFT);
}
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_camera) {
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onTabChanged(String tabId) {
currentTab = tabId;
if (currentTab.equalsIgnoreCase("tab1")) {
if (pager != null) {
pager.setAdapter(new FragmentAdapterSearch(getSupportFragmentManager()));
mButton.setVisibility(View.VISIBLE);
TabIcon.setColor(0, mTabLayout, iconSearchOpac, iconSearch);
}
}
if (currentTab.equalsIgnoreCase("tab2")) {
if (pager != null) {
pager.setAdapter(new FragmentAdapterComments(getSupportFragmentManager()));
mButton.setVisibility(View.INVISIBLE);
TabIcon.setColor(0, mTabLayout, iconCommentOpac, iconComment);
}
}
if (currentTab.equalsIgnoreCase("tab3")) {
if (pager != null) {
pager.setAdapter(new FragmentAdapterCars(getSupportFragmentManager()));
mButton.setVisibility(View.VISIBLE);
TabIcon.setColor(0, mTabLayout, iconCarOpac, iconCar);
}
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (currentTab.equalsIgnoreCase("tab1")) {
TabIcon.setColor(position, mTabLayout, iconSearchOpac, iconSearch);
}
if (currentTab.equalsIgnoreCase("tab2")) {
TabIcon.setColor(position, mTabLayout, iconCommentOpac, iconComment);
}
if (currentTab.equalsIgnoreCase("tab3")) {
TabIcon.setColor(position, mTabLayout, iconCarOpac, iconCar);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSearch:
startActivity(new Intent(Navigation.this, SelectCategory.class));
break;
case R.id.btnSelectLocation:
startActivity(new Intent(Navigation.this, SelectLocation.class));
}
}
}
而我的XML就是这样。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
android:fitsSystemWindows="true"
tools:context="com.toweelo.Navigation">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:weightSum="10">
<LinearLayout android:id="@+id/btnSelectLocation"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5.5"
android:background="@drawable/ripple_effect"
android:clickable="true"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:paddingLeft="8dp">
<com.toweelo.custom.CustomTextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="Quezon City"
android:textColor="@color/colorWhite"
android:textSize="@dimen/title"/>
<ImageView
android:layout_width="24dp"
android:layout_height="match_parent"
android:src="@drawable/ic_down_arrow"/>
</LinearLayout>
<RelativeLayout android:layout_width="0dp"
android:layout_height="36dp"
android:layout_marginLeft="10dp"
android:layout_weight="4.5">
<Button android:id="@+id/btnSearch"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/custom_search_button"
android:drawableRight="@drawable/ic_search"
android:text="Search"
android:textColor="@color/colorWhite"
android:textSize="14dp"/>
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.Toolbar>
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@color/colorPrimary"
app:pstsDividerColor="@color/colorPrimary"
app:pstsIndicatorColor="@color/colorWhite"
app:pstsIndicatorHeight="3dp"
app:pstsShouldExpand="true"
app:pstsTextAllCaps="false"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
<com.github.clans.fab.FloatingActionMenu
android:id="@+id/fabMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
fab:menu_animationDelayPerItem="50"
fab:menu_backgroundColor="#B3FFFFFF"
fab:menu_buttonSpacing="0dp"
fab:menu_colorNormal="#DA4336"
fab:menu_colorPressed="#E75043"
fab:menu_colorRipple="#99FFFFFF"
fab:menu_fab_size="normal"
fab:menu_icon="@drawable/fab_add"
fab:menu_labels_colorNormal="@color/colorWhite"
fab:menu_labels_cornerRadius="3dp"
fab:menu_labels_ellipsize="none"
fab:menu_labels_margin="0dp"
fab:menu_labels_maxLines="-1"
fab:menu_labels_padding="8dp"
fab:menu_labels_paddingBottom="4dp"
fab:menu_labels_paddingLeft="8dp"
fab:menu_labels_paddingRight="8dp"
fab:menu_labels_paddingTop="4dp"
fab:menu_labels_position="left"
fab:menu_labels_showShadow="true"
fab:menu_labels_singleLine="false"
fab:menu_labels_textColor="@color/colorBlack"
fab:menu_labels_textSize="14sp"
fab:menu_openDirection="up"
fab:menu_shadowColor="#66000000"
fab:menu_shadowRadius="4dp"
fab:menu_shadowXOffset="1dp"
fab:menu_shadowYOffset="3dp"
fab:menu_showShadow="true"
>
<com.github.clans.fab.FloatingActionButton
android:id="@+id/menuIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_premium"
fab:fab_colorNormal="@color/colorPremium"
fab:fab_colorPressed="@color/colorPremiumDark"
fab:fab_colorRipple="@color/colorPremiumAccent"
fab:fab_label="Premium"
fab:fab_size="mini"
/>
<com.github.clans.fab.FloatingActionButton
android:id="@+id/menuOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_saves"
fab:fab_colorNormal="@color/colorSaves"
fab:fab_colorPressed="@color/colorSavesDark"
fab:fab_colorRipple="@color/colorSavesAccent"
fab:fab_label="Saves"
fab:fab_size="mini"/>
<com.github.clans.fab.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_edit"
fab:fab_colorNormal="@color/colorPrimary"
fab:fab_colorPressed="@color/colorPrimaryDark"
fab:fab_colorRipple="@color/colorAccent"
fab:fab_label="Write"
fab:fab_size="mini"/>
</com.github.clans.fab.FloatingActionMenu>
<TabHost android:id="@+id/tabHost"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorWhite"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorWhite"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorWhite"
android:orientation="vertical">
</LinearLayout>
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="52dp"
android:layout_weight="0"
android:showDividers="none"/>
</LinearLayout>
</TabHost>
</android.support.design.widget.CoordinatorLayout>
所以我想知道我的代码在将来的使用中是否会出现错误?
我的意思是,它是否会出现内存不足问题,因为我只使用一个viewpager用于不同的适配器。
对于带有pagerTabStrip的标签我是否正确地执行它? 感谢。
答案 0 :(得分:0)
因此,您要在每个FragmentPagerAdapter
上设置新的TabHost
,从而在用户单击另一个选项卡时在运行时创建3个新片段。这可能会给用户造成混淆,如果这些内容加载了大量资源,更大的内存消耗和进一步的用户烦恼。