Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
131 views
in Technique[技术] by (71.8m points)

android - Actionbar Tab in API 10

I have an app with 4 actionbar's tabs. Although I imported android.support.v7.app. ActionBar library, it's only displayed in API 16 device but not in API 10 device. What's the solution here? Main Activity:

import android.app.ActionBar;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBar.TabListener;
import android.support.v7.app.ActionBarActivity;


public class MainActivity extends ActionBarActivity {
public android.support.v7.app.ActionBar actionbar;
public ViewPager viewpager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    actionbar = getSupportActionBar();
    actionbar.setDisplayShowHomeEnabled(false);
    actionbar.setDisplayShowTitleEnabled(false);
    actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    viewpager = (ViewPager) findViewById(R.id.pager);
    FragmentManager fm = getSupportFragmentManager();
    addPageChangeListener();
    MyFragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(fm);
    viewpager.setAdapter(fragmentPagerAdapter);
    addActionBarListener();

}

public void addPageChangeListener() {
    ViewPager.OnPageChangeListener pageChangeListener = new OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            actionbar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }
    };
    viewpager.setOnPageChangeListener(pageChangeListener);
}

public void addActionBarListener() {
    android.support.v7.app.ActionBar.TabListener listener = new TabListener() {

        @Override
        public void onTabUnselected(
                android.support.v7.app.ActionBar.Tab arg0,
                android.support.v4.app.FragmentTransaction arg1) {
        }

        @Override
        public void onTabSelected(android.support.v7.app.ActionBar.Tab tab,
                android.support.v4.app.FragmentTransaction arg1) {
            viewpager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabReselected(
                android.support.v7.app.ActionBar.Tab arg0,
                android.support.v4.app.FragmentTransaction arg1) {
        }
    };
    android.support.v7.app.ActionBar.Tab tab = actionbar.newTab()
            .setTabListener(listener);
    tab.setCustomView(R.layout.search);
    actionbar.addTab(tab);
    tab = actionbar.newTab().setTabListener(listener);
    tab.setCustomView(R.layout.filter);
    actionbar.addTab(tab);
    tab = actionbar.newTab().setTabListener(listener);
    tab.setCustomView(R.layout.favorite);
    actionbar.addTab(tab);
    tab = actionbar.newTab().setTabListener(listener);
    tab.setCustomView(R.layout.info);
    actionbar.addTab(tab);
}

}

MyPagerFragmentAdapter:

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

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

public final int PAGE_COUNT = 4;

public MyFragmentPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int i) {
    // TODO Auto-generated method stub
    Bundle data = new Bundle();
    switch (i) {
    case 0:
        SearchFragment searchFragment = new SearchFragment();
        return searchFragment;
    case 1:
        FilterFragment filterFragment = new FilterFragment();
        return filterFragment;
    case 2:
        FavoriteFragment favoriteFragment = new FavoriteFragment();
        return favoriteFragment;
    case 3:
        InfoFragment infoFragment = new InfoFragment();

        return infoFragment;

    }
    return null;
}

@Override
public int getCount() {
    return PAGE_COUNT;
}

}

Main Activity Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try with replacing import android.app.ActionBar;

with this one import android.support.v7.app.ActionBar;.

Also check your application theme should be android:theme="@style/Theme.AppCompat" in your AndroidManifest.xml file

Refer below link for more details...

http://wptrafficanalyzer.in/blog/swipable-navigation-tabs-using-actionbarcompat-library/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...