大家好,这是我的应用
我想将2个滑动选项卡Accounts Activity和Chart添加到fragment_accounts.xml布局中。 我也有导航抽屉菜单。
这是fragment_accounts.xml:
<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"></android.support.v4.view.ViewPager>
这是AccountsFragment.java: 我收到此错误
错误:(44,33)错误:不兼容的类型:android.app.ActionBar不能 转换为android.support.v7.app.ActionBar
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.rastari.salar.mymetarialbank.R;
import com.rastari.salar.mymetarialbank.adapter.TabsPagerAdapter;
public class AccountsFragment extends FragmentActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = {"Accounts Acctivity", "Chart Activity"};
public AccountsFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_accounts, container, false);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(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.setOnPageChangeListener(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) {
}
});
// Inflate the layout for this fragment
return rootView;
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}
这是TabsPagerAdapter:我收到此错误:
错误:(25,24)错误:不兼容的类型:AccountsActivityFragment 无法转换为Fragment
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import com.rastari.salar.mymetarialbank.activity.AccountsActivityFragment;
import com.rastari.salar.mymetarialbank.activity.ChartActivity;
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new AccountsActivityFragment();
case 1:
return new ChartActivity();
}
return null;
}
@Override
public int getCount() {
// get item count - equal to number of tabs
return 2;
}
}
我在整个互联网上搜索了如何使用导航抽屉菜单和滑动标签教程创建应用程序,但我一无所获。
请帮助这对我来说非常重要。 谢谢。
答案 0 :(得分:0)
为你的actionBar
您应该拨打getSupportActionBar
()而不是getActionBar()
对于您的片段,请确保它们都延伸android.support.v4.Fragment
而不是android.app.Fragment