android actionbar的问题 求解!!!

图片说明

3个回答

 package com.user.activity;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

import com.usernewsapp.R;

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


public class MainActivity extends FragmentActivity {
    private ViewPager mViewPager;
    private ActionBar mActionBar;
    private MyPagerAdapter mPagerAdapter;
    private List<String> titlelist = new ArrayList<String>();
    private ArrayList<Fragment> fragmentList;
    private List<ActionBar.Tab> tablist = new ArrayList<ActionBar.Tab>();
    private NewsFragment1 fragment1;
    private NewsFragment2 fragment2;
    private NewsFragment3 fragment3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    @SuppressLint("NewApi")
    public void initView() {
        titlelist.add("热点");
        titlelist.add("健康");
        titlelist.add("教育");
        fragment1 = new NewsFragment1();
        fragment2 = new NewsFragment2();
        fragment3 = new NewsFragment3();

        fragmentList = new ArrayList<Fragment>();
        fragmentList.add(fragment1);
        fragmentList.add(fragment2);
        fragmentList.add(fragment3);

        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(),
                titlelist,fragmentList);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setOnPageChangeListener(mPageChangeListener);
//         actionbar
        mActionBar = getActionBar();
        mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        mActionBar.setDisplayShowTitleEnabled(false);
        mActionBar.setDisplayShowHomeEnabled(false);

        for (int i = 0; i != titlelist.size(); i++) {
            tablist.add(mActionBar.newTab().setText(titlelist.get(i))
                    .setTabListener(mTabListener));
            mActionBar.addTab(tablist.get(i));
        }

    }

    @SuppressLint("NewApi")
    private ViewPager.OnPageChangeListener mPageChangeListener = new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int arg0) {
            mActionBar.setSelectedNavigationItem(arg0);
        }

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

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }
    };

    @SuppressLint("NewApi")
    private ActionBar.TabListener mTabListener = new ActionBar.TabListener() {

        @Override
        public void onTabSelected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
            if (tab == tablist.get(0)) {
                mViewPager.setCurrentItem(0);
            } else if (tab == tablist.get(1)) {
                mViewPager.setCurrentItem(1);
            } else if (tab == tablist.get(2)) {
                mViewPager.setCurrentItem(2);
            }

        }

        @Override
        public void onTabUnselected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTabReselected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
            // TODO Auto-generated method stub

        }
    };
}



apply plugin: 'com.android.application'

android {
useLibrary 'org.apache.http.legacy'
compileOptions.encoding = "GBK"
aaptOptions.cruncherEnabled = false
aaptOptions.useNewCruncher = false
compileSdkVersion 26
buildToolsVersion "26.0.1"

defaultConfig {
    minSdkVersion 19
    targetSdkVersion 26
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.0.0-alpha1'

}

actionbar可以用 toolbar取代 我代码中的方法吗 。。 求解~

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐