android 标签不工作

android tabs not working

我正在学习 android 中有关选项卡的教程,并且所做的一切都与视频中的完全一样。有一堆代码,但是我在其中一行 tab1.setTabListener(new ActionBar.TabListener(fragment1)); 上遇到错误它说 'TabListener' 是抽象的;无法实例化。我该如何解决这个问题?

这是 activity 的完整代码,其中有错误:

import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ProjectCreateScreen extends ActionBarActivity {

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.secondary_layout1);

    Toolbar toolbar = (Toolbar) findViewById(R.id.AwesomeBar);
    setSupportActionBar(toolbar);

    ActionBar.Tab tab1, tab2, tab3;

    // Fragments that will load when the tabs are clicked
    Fragment fragment1 = new TabFragment1();
    Fragment fragment2 = new TabFragment2();
    Fragment fragment3 = new TabFragment3();

    ActionBar actionBar = getActionBar();

    // Set the current navigation mode to tabs
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Add titles to tabs
    tab1 = actionBar.newTab().setText("Tab1");
    tab2 = actionBar.newTab().setText("Tab2");
    tab3 = actionBar.newTab().setText("Tab3");

    // Set tab listeners which provide callbacks for tab events
    // A callback informs another class when an action occurs
    tab1.setTabListener(new ActionBar.TabListener(fragment1));
    tab2.setTabListener(new ActionBar.TabListener(fragment2));
    tab3.setTabListener(new ActionBar.TabListener(fragment3));

    // Adds tabs to the actionbar
    actionBar.addTab(tab1);
    actionBar.addTab(tab2);
    actionBar.addTab(tab3);
}
}

创建一个实现 ActionBar.TabListener 的 class 并在 setTabListener 中调用 class。

有两种方法可以解决这个问题:

  1. 创建一个实现 ActionBar.TabListener 的新 class。例如:

    protected class MyTabListener implements ActionBar.TabListener {  
        private Fragment fragment;
    
        public MyTabListener(Fragment fragment){
            this.fragment = fragment;
        }
        public void onTabSelected(Tab tab, FragmentTransaction ft){
            ft.add(R.id.layout, fragment, null);
        }
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
        }
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            ft.remove(fragment);
        }
    }
    

    然后设置TabListener:

    tab1.setTabListener(new MyTabListener(fragment1));
    
  2. 让您的 MainActivity 实现 ActionBar.TabListener 并覆盖所需的方法。例如:

    public class MainActivity extends FragmentActivity implements ActionBar.TabListener { 
    
        @Override
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
            // When the given tab is selected, show the tab contents in the
            // container view.
            Fragment fragment = new Fragment();
            getFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
        }
    
        @Override
        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        }
    
        @Override
        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        }
    }
    

您必须创建一个实现 TabListener 接口的class

protected class MyTabsListener implements ActionBar.TabListener{
    private Fragment fragment;

    public MyTabsListener(Fragment fragment){
        this.fragment = fragment;
    }
    public void onTabSelected(Tab tab, FragmentTransaction ft){
        ft.add(R.id.layout2, fragment, null);
    }
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        ft.remove(fragment);
    }
}

然后设置tabListener

tab1.setTabListener(new MyTabsListener(fragment1));