我创建了使用viewPager的Tabs并将每个标签显示为片段但是当我运行模拟器时,我的标签不会显示为here。 这是我的编码: Dashboard.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/WELCOME"
android:textSize="40dip"
android:gravity="center"
android:layout_marginTop="20dip"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/emailTextView"/>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/Logout_Me"
android:textSize="20dip"
android:textColor="#21dbd4"
android:textStyle="bold"
android:id="@+id/btnLogout"
android:layout_marginTop="80dip"
android:background="@null"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
</LinearLayout>
MyPagerAdapter.java:
package com.example.loginandregistration;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyPageAdapter extends FragmentPagerAdapter {
// Declare the number of ViewPager pages
final int PAGE_COUNT = 3;
public MyPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
switch (arg0) {
// Open FragmentTab1.java
case 0:
CreateFolder createfolder = new CreateFolder();
return createfolder;
// Open FragmentTab2.java
case 1:
SendPic sendpic = new SendPic();
return sendpic;
// Open FragmentTab3.java
case 2:
MyPics mypics = new MyPics();
return mypics;
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return PAGE_COUNT;
}
}
TabsViewPagerFragmentActivity:
package com.example.loginandregistration;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.LocalBroadcastManager;
import com.example.loginandregistration.MyPageAdapter;
import android.support.v7.app.ActionBar;
class TabsViewPagerFragmentActivity extends ActionBarActivity {
// Declare Variables
ActionBar actionBar;
ViewPager mPager;
Tab tab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from dashboard.xml
setContentView(R.layout.dashboard);
// Activate Navigation Mode Tabs
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Locate ViewPager in dashboard.xml
mPager = (ViewPager) findViewById(R.id.pager);
// Capture ViewPager page swipes
ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
// Find the ViewPager Position
actionBar.setSelectedNavigationItem(position);
}
};
mPager.setOnPageChangeListener(ViewPagerListener);
// Locate the adapter class called ViewPagerAdapter.java
MyPageAdapter viewpageradapter = new MyPageAdapter(getSupportFragmentManager());
// Set the View Pager Adapter into ViewPager
mPager.setAdapter(viewpageradapter);
// Capture tab button clicks
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Pass the position on tab click to ViewPager
mPager.setCurrentItem(tab.getPosition());
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
};
// Create first Tab
tab = actionBar.newTab().setText("Create Folder").setTabListener(tabListener);
actionBar.addTab(tab);
// Create second Tab
tab = actionBar.newTab().setText("Send Pic").setTabListener(tabListener);
actionBar.addTab(tab);
// Create third Tab
tab = actionBar.newTab().setText("Create Folder").setTabListener(tabListener);
actionBar.addTab(tab);
}
}
我已经创建了所有扩展Fragment的“SendPic.java”,“CreateFolder.java”和“MyPic.java”。我已经有这个问题了一段时间,它没有出现在模拟器上,我无法弄清楚为什么。任何帮助都会得到帮助!
注意:当我将dashboard.xml文件中的viewPager移动到页面顶部时,它会使我的所有其他视图都不可见(留下空白页面)