查看寻呼机 - 获取每页的寻呼机号码
View Pager - get pager number for each page
我有一个包含视图寻呼机的片段。
view pager 的每一页都包含一个片段。
是否有可能获得当前页面的索引号
在内容片段中?
或者我是否必须在创建时传递每个片段的编号?
或者我是否必须使用片段的静态 "newInstance" 方法将数据传递给它?
您可以通过为您的片段设置标签或为您的片段的任何视图对象设置标签并进入您的片段来做到这一点
使用静态工厂方法
实例化您的视图pager/child片段
private String fragmentId= "";
/**
* The argument keys for creating a fragment.
*/
private static final String ARG_ONE = "arg1";
/**
* Factory method for this fragment class. Constructs a new fragment with the given arguments.
*/
public static MyFragment create(String fragmentId) {
MyFragment tab = new MyFragment();
Bundle bundle = new Bundle(1);
bundle.putString(ARG_ONE, fragmentId);
tab.setArguments(bundle);
return tab;
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.tab_home_screen, container, false);
this.fragmentId=getArguments().getString(ARG_ONE);
return view;
}
以后可以使用fragmentId
了解片段
我有一个包含视图寻呼机的片段。 view pager 的每一页都包含一个片段。
是否有可能获得当前页面的索引号 在内容片段中?
或者我是否必须在创建时传递每个片段的编号?
或者我是否必须使用片段的静态 "newInstance" 方法将数据传递给它?
您可以通过为您的片段设置标签或为您的片段的任何视图对象设置标签并进入您的片段来做到这一点
使用静态工厂方法
实例化您的视图pager/child片段 private String fragmentId= "";
/**
* The argument keys for creating a fragment.
*/
private static final String ARG_ONE = "arg1";
/**
* Factory method for this fragment class. Constructs a new fragment with the given arguments.
*/
public static MyFragment create(String fragmentId) {
MyFragment tab = new MyFragment();
Bundle bundle = new Bundle(1);
bundle.putString(ARG_ONE, fragmentId);
tab.setArguments(bundle);
return tab;
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.tab_home_screen, container, false);
this.fragmentId=getArguments().getString(ARG_ONE);
return view;
}
以后可以使用fragmentId
了解片段