(我是新手,如果我说的有些话没有多大意义,请见谅)
我已经使用 actionbarsherlock 和 Fragments 实现了刷卡。经过测试,我注意到有三个滑动,但只有两个选项卡。我很确定我只创建了两个片段。这几乎就像一个标签以某种方式加倍,我不知道为什么。我已按照此处的一些说明执行此操作。
这是我的主要课程:
public class DashboardActivity extends SherlockFragmentActivity {
com.actionbarsherlock.app.ActionBar mActionBar;
ViewPager mPager;
com.actionbarsherlock.app.ActionBar.Tab tab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
// Activate the navigation mode tabs
mActionBar = getSupportActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Locate the view pager in dashboard.xml
mPager = (ViewPager) findViewById(R.id.pager);
// Activate the fragment manager
FragmentManager fm = getSupportFragmentManager();
// Capture view pager swipes
ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
// Find the view pager position
mActionBar.setSelectedNavigationItem(position);
}
};
mPager.setOnPageChangeListener(ViewPagerListener);
// Locate the adapter class
ViewPagerAdapter viewpageradapter = new ViewPagerAdapter(fm);
// Set the view pager adapter in ViewPager
mPager.setAdapter(viewpageradapter);
// Capture the button clicks API ERROR
TabListener tabListener = new TabListener() {
@Override
public void onTabSelected(
com.actionbarsherlock.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabUnselected(
com.actionbarsherlock.app.ActionBar.Tab tab,
FragmentTransaction ft) {
mPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(
com.actionbarsherlock.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
};
// Create the first tab
tab = mActionBar.newTab().setText("Support Tab")
.setTabListener(tabListener);
mActionBar.addTab(tab);
// Create the second tab
tab = mActionBar.newTab().setText("Students Tab")
.setTabListener(tabListener);
mActionBar.addTab(tab);
}
}
这是一个片段:(另一个是相同的)
public class StudentsTab extends SherlockListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String[] studentItems = getResources().getStringArray(
R.array.studentItems);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity()
.getBaseContext(), android.R.layout.simple_list_item_1,
studentItems);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
}
这是 ViewPagerAdapter:
公共类 ViewPagerAdapter 扩展 FragmentPagerAdapter {
// Declare the number of tabs
final int PAGE_COUNT = 2;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
Bundle data = new Bundle();
switch (arg0) {
// Open support tab
case 0:
SupportTab supportTab = new SupportTab();
data.putInt("current_page", arg0 + 1);
supportTab.setArguments(data);
return supportTab;
case 1:
StudentsTab studentTab = new StudentsTab();
data.putInt("current_page", arg0 + 1);
studentTab.setArguments(data);
return studentTab;
}
return null;
}
@Override
public int getCount() {
return PAGE_COUNT;
}
}
我的主要xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
如果可以,请帮助,我找不到发生了什么。谢谢!