我是Android开发的新手,并且我在此代码中遇到了已弃用的错误,请有人告诉我如何修复此代码而不会出错。
不推荐使用
setNavigationMode(int)
类型的方法ActionBar
。不推荐使用
addTab(ActionBar.Tab)
类型的方法ActionBar
。不推荐使用
setSelectedNavigationItem(int)
类型的方法ActionBar
。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
String mBaseFolderPath = android.os.Environment.getExternalStorageDirectory() + File.separator + "Folder2"
+ File.separator;
if (!new File(mBaseFolderPath).exists()) {
new File(mBaseFolderPath).mkdir();
}
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new FBTabsAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
*/
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
适配器。
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class VDTabsAdapter extends FragmentPagerAdapter {
public VDTabsAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new VDFragment();
case 1:
return new ADFragment();
}
return null;
}
@Override
public int getCount() {
// get item count - equal to number of tabs
return 2;
}
}
代码2。
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
if (tab.getPosition() == 1) {
Log.e("UPDATED", " SELECTED");
final DownloadsFragment fragment = ((VDFragment) getSupportFragmentManager()
.findFragmentByTag("android:switcher:" + R.id.pager + ":1"));
fragment.updateVD();
ERROR。
- 不推荐使用
getPosition()
类型的方法ActionBar.Tab