我正在尝试添加 ViewPager,但在添加新片段时遇到问题。我按照教程创建了它,在每次滑动时都使用相同的片段,片段中只有一个 TextView 发生变化。
有效的片段称为 MyFragment。(我不知道为什么这行得通,而其他的不行。
我不想使用这个片段,因为我已经制作了不同的片段(工作正常)。例如,我想添加一个名为 ReminderFragment 的片段
我尝试在 getItem() 方法中添加开关和大小写,但我收到一条错误消息:“类型不匹配:无法从片段名称转换为片段”。
这是我的 FragmentActivity 的代码 - 它在其中包含 FragmentPagerAdapter(我不知道这是否是一个好方法):
// Removed imports, package name
public class PageViewActivity extends FragmentActivity {
MyPageAdapter pageAdapter;
private ViewPager pager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_pager_activity); // Sets the layout to an xml which includes a viewpager
List<Fragment> fragments = getFragments();
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
pager = (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
pager.setCurrentItem(1);
pager.setPageTransformer(true, new ZoomOutPageTransformer());
}
@Override
public void onBackPressed() {
if (pager.getCurrentItem() == 1) {
super.onBackPressed();
} else if (pager.getCurrentItem() == 0){
pager.setCurrentItem(pager.getCurrentItem() + 1);
}
else if (pager.getCurrentItem() == 2){
pager.setCurrentItem(pager.getCurrentItem() - 1);
}
}
private List<Fragment> getFragments(){
List<Fragment> fList = new ArrayList<Fragment>();
// I don't know how to change this to work with my fragments
fList.add(MyFragment.newInstance("Page 0")); // MyFragment is the fragment I don't need, but it is the only fragment that works!
fList.add(MyFragment.newInstance("Page 1"));
fList.add(MyFragment.newInstance("Page 2"));
return fList;
}
private class MyPageAdapter extends FragmentPagerAdapter { // Sets what MyPagerAdapter is
private List<Fragment> fragments; // Defines a list of fragments called fragments
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) { // Sets what is inside the MyPagerAdapter
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int index) {
// return this.fragments.get(index);
switch (index){
case 0: return new ReminderFragment(); // This is where I get errors
case 1: return new QuickNoteFragment();
}
}
@Override
public int getCount() {
return this.fragments.size();
}
}
}
这是 ReminderFragment 的代码。这不起作用,因为我收到上面的错误
// Removed stuff
public class ReminderFragment extends Fragment implements OnClickListener{
public static final String ARG_nOTERACTIVITY_NUMBER = "noter_activity";
// Removed stuff
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View remView = inflater.inflate(R.layout.reminder, container, false);
int i = getArguments().getInt(ARG_nOTERACTIVITY_NUMBER);
String noter_activity = getResources().getStringArray(
R.array.noter_array)[i];
// Removed stuff
getActivity().setTitle(noter_activity); // For navigation drawer(?)
return remView;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.***: // Does stuff
case R.id.***:
break;
}
}
}
public static Fragment newInstance() { // Is this necessary
// TODO Auto-generated method stub
ReminderFragment remFrag = new ReminderFragment();
return remFrag;
}
}
我知道这里有两种添加片段的方法 - 我只是想展示我尝试过的内容:-)。我很困扰!
谢谢