fragment.setarguments
时间: 2023-05-02 08:05:38 浏览: 67
b'fragment.setarguments'是一个用于为Fragment设置参数的方法。通过使用这个方法,我们可以将一些数据或变量传递给Fragment,以便在Fragment中进行使用和操作。这些参数可以在Fragment的生命周期中都可以获取和操作。
相关问题
public static MainFragment newInstance1() { MainFragment fragment = new MainFragment(); Bundle args = new Bundle(); view.findViewById(R.id.f3).setVisibility(View.GONE); fragment.setArguments(args); return fragment; }代码优化修改
这段代码可以优化为:
public static MainFragment newInstance1() {
MainFragment fragment = new MainFragment();
Bundle args = new Bundle();
args.putInt("viewVisibility", View.GONE);
fragment.setArguments(args);
return fragment;
}
然后在 MainFragment 类的 onCreate 方法中,通过 getArguments 方法获取传入的参数,并根据参数值设置视图的可见性。这样可以提高代码的可读性和可维护性。
请问以下代码函数的内容应该写什么: public static Fragment newInstance(String from) { Fragment fragment = new Fragment(); // View view = inflater.inflate(R.layout.frag,null); // TextView textView = (TextView) view.findViewById(R.id.textview); // textView.setText(from); Bundle args = new Bundle(); args.putString("index", from); fragment.setArguments(args); return fragment; }
这段代码是用于创建一个Fragment实例的静态方法。其中需要传入一个字符串参数from,表示创建该Fragment的来源。函数的具体内容应该包括以下几步:
1. 创建一个新的Fragment实例。
2. 创建一个Bundle对象,并将参数from作为"index"键的值存储在Bundle中。
3. 将Bundle设置为Fragment实例的参数。
4. 返回Fragment实例。
注释掉的代码是用于初始化Fragment视图的,如果需要在Fragment中显示布局和数据,则需要在onCreateView方法中进行相应的操作。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)