我試圖在我的應用程序中創建navigation drawer
。在我的navigation drawer item
,View
之一中,我希望它有tabs
,swipe view
功能。我收到錯誤Viewview is not abstract and does not override abstract method onTabReselected
。如何解決不抽象,不覆蓋抽象方法onTabReselected
MainActivity //用於導航抽屜項
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
private void selectItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment=new Information();
break;
case 1:
fragment=new Claims1();
Bundle bundle=new Bundle();
bundle.putLong("ab",WorkDetailsTable.ab);
fragment.setArguments(bundle);
break;
case 2:
fragment=new ViewView();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(mNavigationDrawerItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
}
}
ViewView
import android.support.v4.app.Fragment;
import android.os.Bundle;
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.ViewGroup;
import android.widget.AdapterView;
public class ViewView extends Fragment implements ActionBar.TabListener {
private ViewPager viewPager;
private TabsFragmentPagerAdapter mAdapter;
private ActionBar actionBar;
private String[] tabs = {"Information", "receipt"};
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View View1 = inflater.inflate(R.layout.viewview1, container, false);
viewPager = (ViewPager) View1.findViewById(R.id.pager);
mAdapter = new TabsFragmentPagerAdapter(getActivity().getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (int i = 0; i < 2; i++) {
actionBar.addTab(actionBar.newTab().setText(tabs[i]).setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int arg) {
// TODO Auto-generated method stub
actionBar.setSelectedNavigationItem(arg);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
BuildList();
return View1;
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getActivity().getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void onTabReselected(ActionBar.Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction arg1) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
public void onTabUnselected(ActionBar.Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
}
TabsFragmentPagerAdapter
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class TabsFragmentPagerAdapter extends FragmentPagerAdapter {
public TabsFragmentPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int index) {
// TODO Auto-generated method stub
switch(index) {
case 0:
return new UpdatePage2();
case 1:
return new Receipt();
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
}
錯誤
Error:(50, 47) error: incompatible types: android.app.ActionBar cannot be converted to android.support.v7.app.ActionBar
雅,我需要。你是否在我的'onCreateView'方法中添加'ViewPager.OnPageChangeListener()'? – Tony
如何將TabListener直接添加到Viewview? – Tony
@Tony'ViewView'不包含'onTabReselcted'的實現。如果你希望它實現'TabListener',它必須包含這樣的實現。 – Eran