0

我想在我的仪表板活动中创建选项卡,允许用户左右滑动以在片段(我的其他类)之间切换。但我注意到在我的应用程序中我有这个:(请注意下面的评论以查看图片)

为什么我TabHost没有显示我的标签?我正在关注教程(请注意评论以查看教程)这是我的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="#3b3b3b">

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

<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <TabWidget
            android:id="@android:id/tabs"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"/>

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            />
    </LinearLayout>
</TabHost>

</LinearLayout>

如果您需要查看更多编码,请告诉我,我很乐意更新问题。

以下是java类:

MyFragment.java:

package com.example.loginandregistration;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class MyFragment extends Fragment {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";


public static final MyFragment newInstance(String message)
{
    MyFragment f = new MyFragment();
    Bundle bdl = new Bundle(1);
    bdl.putString(EXTRA_MESSAGE, message);
    f.setArguments(bdl);
    return f;
}


@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {
    String message = getArguments().getString(EXTRA_MESSAGE);
    View v = inflater.inflate(R.layout.myfragment_layout, container, false);
    TextView messageTextView = (TextView)v.findViewById(R.id.TextView);
    messageTextView.setText(message);


    return v;
}
}

MyPageAdapter.java:

package com.example.loginandregistration;

import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;


public class MyPageAdapter extends FragmentPagerAdapter { 

private List<Fragment> fragments; 

 public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) { 

super(fm); 

  this.fragments = fragments; 

  } 


 @Override

 public Fragment getItem(int position) {
 return this.fragments.get(position);
 }

  @Override 

public int getCount() { 

return this.fragments.size(); 
} 
} 

PageViewActivity.java:

package com.example.loginandregistration;

import java.util.ArrayList;
import java.util.List;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;


public class PageViewActivity extends FragmentActivity {
MyPageAdapter pageAdapter;
   @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dashboard);

    List<Fragment> fragments = getFragments();

    pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);

    ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
    pager.setAdapter(pageAdapter);

   }

   private List<Fragment> getFragments(){
    List<Fragment> fList = new ArrayList<Fragment>();

    fList.add(MyFragment.newInstance("Create Pic"));
    fList.add(MyFragment.newInstance("Create Folder"));
    fList.add(MyFragment.newInstance("Send Pic"));

    return fList;
   }


  private class MyPageAdapter extends FragmentPagerAdapter {
    private List<Fragment> fragments;


    public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }
    @Override
    public Fragment getItem(int position) {
        return this.fragments.get(position);
    }

    @Override
    public int getCount() {
        return this.fragments.size();
    }
}
}

TabsViewPagerFragmentActivity.java:

package com.example.loginandregistration;

import java.util.HashMap;
import java.util.List;
import java.util.Vector;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;


/**
 * The <code>TabsViewPagerFragmentActivity</code> class implements the Fragment activity that maintains a TabHost using a ViewPager.
 * @author mwho
 */
public class TabsViewPagerFragmentActivity extends FragmentActivity implements     TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {

private TabHost mTabHost;
private ViewPager mViewPager;
private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, TabsViewPagerFragmentActivity.TabInfo>();
private MyPageAdapter mPagerAdapter;
/**
 *
 * @author mwho
 * Maintains extrinsic info of a tab's construct
 */
private class TabInfo {
     private String tag;
     private Class<?> clss;
     private Bundle args;
     private Fragment fragment;
     TabInfo(String tag, Class<?> clazz, Bundle args) {
         this.tag = tag;
         this.clss = clazz;
         this.args = args;
     }

}
/**
 * A simple factory that returns dummy views to the Tabhost
 * @author mwho
 */
class TabFactory implements TabContentFactory {

    private final Context mContext;

    /**
     * @param context
     */
    public TabFactory(Context context) {
        mContext = context;
    }

    /** (non-Javadoc)
     * @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
     */
    public View createTabContent(String tag) {
        View v = new View(mContext);
        v.setMinimumWidth(0);
        v.setMinimumHeight(0);
        return v;
    }

  }
   /** (non-Javadoc)
   * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
     */
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Inflate the layout
    setContentView(R.layout.dashboard);
    // Initialise the TabHost
    this.initialiseTabHost(savedInstanceState);
    if (savedInstanceState != null) {
        mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state
    }
    // Intialise ViewPager
    this.intialiseViewPager();
}

/** (non-Javadoc)
 * @see android.support.v4.app.FragmentActivity#onSaveInstanceState(android.os.Bundle)
 */
protected void onSaveInstanceState(Bundle outState) {
    outState.putString("tab", mTabHost.getCurrentTabTag()); //save the tab selected
    super.onSaveInstanceState(outState);
}

/**
 * Initialise ViewPager
 */
private void intialiseViewPager() {

    List<Fragment> fragments = new Vector<Fragment>();
    fragments.add(Fragment.instantiate(this, CreatePic.class.getName()));
    fragments.add(Fragment.instantiate(this, CreateFolder.class.getName()));
    fragments.add(Fragment.instantiate(this, SendPic.class.getName()));
    this.mPagerAdapter  = new MyPageAdapter(super.getSupportFragmentManager(), fragments);
    //
    this.mViewPager = (ViewPager)super.findViewById(R.id.viewpager);
    this.mViewPager.setAdapter(this.mPagerAdapter);
    this.mViewPager.setOnPageChangeListener(this);
}

/**
 * Initialise the Tab Host
 */
private void initialiseTabHost(Bundle args) {
    mTabHost = (TabHost)findViewById(android.R.id.tabhost);
    mTabHost.setup();
    TabInfo tabInfo = null;
    TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab1").setIndicator("Tab 1"), ( tabInfo = new TabInfo("Tab1", CreatePic.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);
    TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab2").setIndicator("Tab 2"), ( tabInfo = new TabInfo("Tab2", CreateFolder.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);
    TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab3").setIndicator("Tab 3"), ( tabInfo = new TabInfo("Tab3", SendPic.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);
    // Default to first tab
    //this.onTabChanged("Tab1");
    //
    mTabHost.setOnTabChangedListener(this);
}

     /**
     * Add Tab content to the Tabhost
     * @param activity
     * @param tabHost
     * @param tabSpec
     * @param clss
     * @param args
     */
    private static void AddTab(TabsViewPagerFragmentActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
    // Attach a Tab view factory to the spec
    tabSpec.setContent(activity.new TabFactory(activity));
    tabHost.addTab(tabSpec);
}

/** (non-Javadoc)
 * @see android.widget.TabHost.OnTabChangeListener#onTabChanged(java.lang.String)
 */
public void onTabChanged(String tag) {
    //TabInfo newTab = this.mapTabInfo.get(tag);
    int pos = this.mTabHost.getCurrentTab();
    this.mViewPager.setCurrentItem(pos);
}

/* (non-Javadoc)
 * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrolled(int, float, int)
 */
@Override
public void onPageScrolled(int position, float positionOffset,
        int positionOffsetPixels) {
    // TODO Auto-generated method stub

 }

/* (non-Javadoc)
 * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageSelected(int)
 */
@Override
public void onPageSelected(int position) {
    // TODO Auto-generated method stub
    this.mTabHost.setCurrentTab(position);
}

/* (non-Javadoc)
 * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrollStateChanged(int)
 */
@Override
public void onPageScrollStateChanged(int state) {
    // TODO Auto-generated method stub

}
}

仪表板活动.java:

package com.example.loginandregistration;


import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import library.DatabaseHandler;
import library.UserFunctions;

public class DashboardActivity extends Activity {
    UserFunctions userFunctions;
    Button btnLogout;
    DatabaseHandler dbHandler;
    private HashMap<String, String> user;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        userFunctions = new UserFunctions();
        if(userFunctions.isUserLoggedIn(getApplicationContext())){
       // user already logged in show databoard
            setContentView(R.layout.dashboard);
            btnLogout = (Button) findViewById(R.id.btnLogout);

            dbHandler = new DatabaseHandler(getApplicationContext());
            user = dbHandler.getUserDetails();
            TextView emailTextView = (TextView) findViewById(R.id.emailTextView);
            emailTextView.setText(user.get("email"));


            btnLogout.setOnClickListener(new View.OnClickListener() {

                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    userFunctions.logoutUser(getApplicationContext());
                    Intent login = new Intent(getApplicationContext(), LoginActivity.class);
                    login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(login);
                    // Closing dashboard screen
                    finish();
                }
            });

        }else{
            // user is not logged in show login screen
            Intent login = new Intent(getApplicationContext(), LoginActivity.class);
            login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(login);
            // Closing dashboard screen
            finish();
        }
    }
}

Myfragmentlayout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >


<TextView
    android:id="@+id/TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:textAppearance="?android:attr/textAppearanceLarge" />


</RelativeLayout>
4

0 回答 0