我正在使用片段和查看器。我希望每页有不同的布局和不同的功能。我怎样才能做到这一点?目前我对每个页面都有不同的布局,但我缺乏将功能放在它后面。你能帮我解决这个问题吗?
我的片段
public class MyFragment extends Fragment{
int mCurrentPage;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** Getting the arguments to the Bundle object */
Bundle data = getArguments();
/** Getting integer data of the key current_page from the bundle */
mCurrentPage = data.getInt("current_page", 0);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = null; //= inflater.inflate(R.layout.myfragment_layout, container,false);
//TextView tv = (TextView ) v.findViewById(R.id.tv);
//tv.setText("You are viewing the page #" + mCurrentPage + "\n\n" + "Swipe Horizontally left / right");
//changeing the layout per page here
switch(mCurrentPage){
case 1:
v = inflater.inflate(R.layout.main_layout, container,false);
break;
case 2:
v = inflater.inflate(R.layout.main_favorite, container,false);
break;
case 3:
v = inflater.inflate(R.layout.main_settings, container,false);
break;
}
return v;
}
}
MyFragmenterPagerAdapter
public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
final int PAGE_COUNT = 3;
/** Constructor of the class */
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
/** This method will be invoked when a page is requested to create */
@Override
public Fragment getItem(int arg0) {
MyFragment myFragment = new MyFragment();
Bundle data = new Bundle();
data.putInt("current_page", arg0+1);
myFragment.setArguments(data);
return myFragment;
}
/** Returns the number of pages */
@Override
public int getCount() {
return PAGE_COUNT;
}
}