Android Fragment选项卡使用 - android.support.v4.app.Fragment

时间:2014-06-24 09:14:41

标签: android android-activity android-fragments fragment android-actionbaractivity

我正在尝试创建一个包含3个标签碎片的应用,但我想在Android中使用新的android.support.v4.app.Fragment。但我无法让它发挥作用。

我尝试了这个例子implementing-fragment-tabs-in-android。它有效,但问题是android.app.Fragment;仅适用于API 11及更高版本。我希望以API 8及更高版本为目标。

这是我的代码:

import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {

    public static final String TAG = MainActivity.class.getSimpleName();

    // Declare Tab Variable
    ActionBar.Tab Tab1, Tab2, Tab3;
    Fragment fragmentTab1 = new FragmentTab1();//ERROR = Type mismatch: cannot convert from FragmentTab1 to Fragment
    Fragment fragmentTab2 = new FragmentTab2();//ERROR = Type mismatch: cannot convert from FragmentTab1 to Fragment
    Fragment fragmentTab3 = new FragmentTab3();//ERROR = Type mismatch: cannot convert from FragmentTab1 to Fragment


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Hide Action Bar
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();


        // Hide Actionbar Icon
        actionBar.setDisplayShowHomeEnabled(false);
        // Hide Actionbar Title
        actionBar.setDisplayShowTitleEnabled(false);
        // Create Actionbar Tabs
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Set Tab Icon and Titles
        Tab1 = actionBar.newTab().setText("Tab1");//.setIcon(R.drawable.tab1);
        Tab2 = actionBar.newTab().setText("Tab2");
        Tab3 = actionBar.newTab().setText("Tab3");

        // Set Tab Listeners
        Tab1.setTabListener(new TabListener(fragmentTab1));
        Tab2.setTabListener(new TabListener(fragmentTab2));
        Tab3.setTabListener(new TabListener(fragmentTab3));

        // Add tabs to actionbar
        actionBar.addTab(Tab1);
        actionBar.addTab(Tab2);
        actionBar.addTab(Tab3);


    }//-----end onCreate




    //implements ActionBar.TabListener --------------------------
    @Override
    public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }
    @Override
    public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }
    @Override
    public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }


    //Action bar of AppCombat ---------------------
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}//--end body

感谢您的帮助。 :)

1 个答案:

答案 0 :(得分:1)

import android.app.Fragment;

您的导入必须一致。如果使用支持库,则所有相关导入都应来自支持库。