在我的 Android 应用程序中我正在使用 viewpager 和 FragmentActivity 。但它没有显示内容。 我的FragmentActivity是
public class DropPickup extends FragmentActivity {
private DPAdapter myAdapter;
private ViewPager myPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myAdapter = new DPAdapter(getSupportFragmentManager());
myPager = (ViewPager) findViewById(R.id.dp_viewpager);
myPager.setAdapter(myAdapter);
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return 3;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new PickUpFragment();
case 1:
return new DropFragment();
default:
return null;
}
}
}
}
和我的页面适配器如下。
public DPAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
switch (position) {
case 0:
return new PickUpFragment();
case 1:
return new DropFragment();
default:
return null;
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
}
这是我的一个片段
public class DropFragment extends Fragment {
TableLayout DTbl;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View viewx=inflater.inflate(R.layout.fragment_drop, container, false);
DTbl = (TableLayout) viewx.findViewById(R.id.dpp_tbl_prs_tbl);
//return super.onCreateView(inflater, container, savedInstanceState);
return viewx;
}
}
这是我的布局视图寻呼机
<android.support.v4.view.ViewPager
android:id="@+id/dp_viewpager"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
有人可以帮助我找出问题所在。谢谢!!!!
答案 0 :(得分:0)
写两个单独的课程 MyAdapter &amp; DPAdapter 当它们相同时,丢弃其中一个。否则你的代码看起来很好。
<强>更新强>
使用此代码并保留MyAdapter类
myAdapter = new MyAdapter(getSupportFragmentManager());
myPager = (ViewPager) findViewById(R.id.dp_viewpager);
myPager.setAdapter(myAdapter);
以下是我使用viewpager的代码
public class Book extends FragmentActivity {
private static final String[] CONTENT = new String[] { "A", "B", "C"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.book);
FragmentStatePagerAdapter adapter = new MyFragmentPageAdapter(getSupportFragmentManager());
ViewPager pager = (ViewPager)findViewById(R.id.pager);
pager.setAdapter(adapter);
pager.setCurrentItem(CONTENT.length);
}
class MyFragmentPageAdapter extends FragmentStatePagerAdapter {
public MyFragmentPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if(CONTENT[position].equals("A"))
{
return Page1.newInstance(CONTENT[position]);
} else{
return Page2.newInstance(CONTENT[position]);
}
@Override
public int getCount() {
return CONTENT.length;
}
}
}
其中Page1和Page2是片段