我需要在已创建的Fragment
内的Activity
内实现水平可滚动选项卡。但是Inventory
类自然会延伸Fragment
,如果我尝试从FragmentActivity
扩展Inventory
,它就无法工作。
在这里面:
这样的东西:
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Inventory extends FragmentActivity {
ViewPager viewPager = null;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.inventory);
viewPager = (ViewPager) findViewById(R.id.pager);
FragmentManager fragmentManager = getSupportFragmentManager();
viewPager.setAdapter(new MyAdapter(fragmentManager));
}
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
android.support.v4.app.Fragment fragment = null;
if (position==0){
fragment = new Fa();
}
if (position==1){
fragment = new Fb();
}
if (position==2){
fragment = new Fc();
}
return fragment;
}
@Override
public int getCount() {
return 0;
}
}
另外,我正在使用this tutorial来实现标签。