我使用android.support.v4.app.Fragment,在我的Fragment子类中,我想获得片段位置,比如:
public abstract class MyFragment extends Fragment {
public void process () {
.....
getPosition();
问题是安卓系统中没有任何与getPosition等效的功能。如何获得位置(与下面的片段相同)?
public class MyPageAdapter extends FragmentPagerAdapter {
public Fragment getItem(int position) {
.....
当您从PagerAdapter:创建片段时,您可以将位置作为参数传递给它
public class MyPageAdapter extends FragmentPagerAdapter {
public Fragment getItem(int position) {
return MyFragment.newInstance(position);
}
//...
}
和:
public class MyFragment extends Fragment {
int position;
public static MyFragment newInstance(int position) {
MyFragment f = new MyFragment();
Bundle args = new Bundle();
args.putInt("Position", position);
f.setArguments(args);
return f;
}
public void onCreate(Bundle savedInstanceState) {
position = getArguments().getInt("Position");
}
}