android标签不起作用

时间:2015-04-28 20:00:53

标签: java android tabs listener

我正在关注有关android中的标签的教程,并完成了与视频完全相同的一切。有一堆代码,但我在其中一行tab1.setTabListener(new ActionBar.TabListener(fragment1))上收到错误;它说'TabListener'是抽象的;无法实例化。我该如何解决这个问题?

以下是活动的完整代码,其中包含错误:

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);
}
}

2 个答案:

答案 0 :(得分:1)

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

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

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

    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) {
        }
    }
    

答案 1 :(得分:1)

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

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));