1

我创建了使用 viewPager 并将每个选项卡显示为片段的选项卡,但是当我运行模拟器时,我的选项卡不会显示为此处所示。这是我的编码: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 移动到页面顶部时,它会使我的所有其他视图不可见(留下空白页)

4

0 回答 0