嗨,我有一项活动可以扩展ActionBarActivity
。这个,有一个ViewPager
使用PagerSlidingTabStrip
库。我正在使用 AppCompat v21,所以我需要扩展 ActionBarActivity 才能使用工具栏。顺便说一句.. 我有两个 PreferenceFragment 需要放在这个 viewpager 中。
import com.astuetz.PagerSlidingTabStrip;
import com.zst.app.multiwindowsidebar.preference.*;
import com.zst.app.multiwindowsidebar.sidebar.SidebarService;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.transition.Explode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.annotation.SuppressLint;
import android.content.Intent;
//import android.support.v13.app.FragmentPagerAdapter;
public class MainActivity extends FragmentActivity {
public static final int MENU_TOGGLE = 1;
public enum PAGES {
MAIN(0),
APPEARENCE(1);
final int position;
PAGES(int pos) {
position = pos;
}
};
MyPagerAdapter mPageAdapter;
// Add
Toolbar toolbar;
public static PagerSlidingTabStrip tabs;
public ViewPager pager;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
getWindow().setEnterTransition( new Explode() );
getWindow().setExitTransition( new Explode() );
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabbed);
toolbar = (Toolbar) findViewById(R.id.toolbar);
mPageAdapter = new MyPagerAdapter(getSupportFragmentManager());
pager = (ViewPager)findViewById(R.id.pager);
tabs = (PagerSlidingTabStrip)findViewById(R.id.tabs);
pager.setAdapter(mPageAdapter);
tabs.setViewPager(pager);
pager.setOffscreenPageLimit(2);
mPageAdapter.notifyDataSetChanged();
pager.invalidate();
}
// My adapter
public class MyPagerAdapter extends FragmentPagerAdapter {
private final String[] TITLES = { "Home", "Interfaccia" };
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
@Override
public int getCount() {
return TITLES.length;
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new MainPrefFragment();
case 1:
return new AppearencePrefFragment();
}
return null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, MENU_TOGGLE, 0, R.string.pref_toggle_service_title)
.setIcon(SidebarService.isRunning ?
R.drawable.ic_menu_toggle_off : R.drawable.ic_menu_toggle_on)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_TOGGLE:
if (SidebarService.isRunning) {
SidebarService.stopSidebar(this);
item.setIcon(R.drawable.ic_menu_toggle_on);
} else {
startService(new Intent(this, SidebarService.class));
item.setIcon(R.drawable.ic_menu_toggle_off);
}
}
return super.onOptionsItemSelected(item);
}
}
但我得到如下错误:Type mismatch: cannot convert from AppearencePrefFragment to Fragment
......我该怎么办?
ps:我尝试导入 support-v13 但似乎与 v7 冲突