当我尝试在 setCurrentItem(arg0.getPosition())
方法中使用 onTabSelected()
时,我的应用会崩溃,同时实现我点击标签{{{下面的1}}也应该随之改变.....它的另一种方式就是当我轻扫时,fragment
随之改变。
代码如下:
Tab
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class AboutUs extends FragmentActivity implements TabListener {
private ActionBar actionBar;
ViewPager viewPager;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.aboutus);
intialization();
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void intialization() {
// TODO Auto-generated method stub
actionBar = getActionBar();
//this is the line where the application crashes
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = actionBar.newTab();
tab1.setText("MUMTAZ ANWAR");
tab1.setTabListener(this);
ActionBar.Tab tab2 = actionBar.newTab();
tab2.setText("CREDIENTIALS");
tab2.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
viewPager = (ViewPager) findViewById(R.id.pagger);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
actionBar.setSelectedNavigationItem(arg0);
}
@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
}
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
//Log.d("HAMZA", "onTabReselected at " + "position" + tab.getPosition()
// + "name" + tab.getText());
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
//Log.d("HAMZA", "onTabselected at " + "position" + tab.getPosition()
// + "name" + tab.getText());
//this is the line where the application crashes
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
//Log.d("HAMZA", "onTabUnselected at " + "position" + tab.getPosition()
// + "name" + tab.getText());
}
}
:
MyAdapter.java
答案 0 :(得分:0)
您需要做的就是在添加选项卡之前初始化View寻呼机,即重新排序视图寻呼机初始化语句并在初始化()中添加制表符语句。见下文
viewPager = (ViewPager) findViewById(R.id.pagger);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
actionBar.setSelectedNavigationItem(arg0);
}
@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
}
});
ActionBar.Tab tab1 = actionBar.newTab();
tab1.setText("MUMTAZ ANWAR");
tab1.setTabListener(this);
ActionBar.Tab tab2 = actionBar.newTab();
tab2.setText("CREDIENTIALS");
tab2.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
希望这有帮助。
答案 1 :(得分:0)
我相信,正如humblerookie所说,这是一个有序的问题。按照下面的代码中的顺序。它应该可以帮到你。
//Create View Pager first
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getSupportActionBar();
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Add Tabs next
ActionBar.Tab tab1 = actionBar.newTab();
tab1.setText("MUMTAZ ANWAR");
tab1.setTabListener(this);
ActionBar.Tab tab2 = actionBar.newTab();
tab2.setText("CREDIENTIALS");
tab2.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
// finally, set the pagelistener : 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) {
}
});