fragment hint show 方法 怎么刷新

 当Fragment 从隐藏切换至显示的时候,不会调用onResume(),Fragment 的onResume()是Fragment 和它的宿主在切换时才会调用的,而Fragment 之前切换时,不会调用onResume()方法,仔细查看发现Fragment 之前切换,Fragment 从隐藏切换至显示,会调用onHiddenChanged(boolean hidden)方法

@Override
public void onHiddenChanged(boolean hidden) {
   super.onHiddenChanged(hidden);
   if (!hidden){
      initData();
   }
}

参考了这个博文 

Fragment hide,show方法后,会调用什么方法

好的,下面是一个简单的 Android 项目示例,其中包含三个页面布局、listview 控件和 Fragment: 首先,创建一个名为 "MyApp" 的新 Android 项目,然后在 res/layout 文件夹中创建三个 XML 布局文件和一个 Fragment 类。 1. activity_main.xml 布局文件 该布局文件包含一个文本框、一个编辑框、一个单选按钮和一个复选按钮。 ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enter Your Name:" /> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Name" /> <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/maleRadio" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Male" /> <RadioButton android:id="@+id/femaleRadio" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Female" /> </RadioGroup> <CheckBox android:id="@+id/checkBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Agree to terms and conditions" /> <Button android:id="@+id/nextButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Next" /> </LinearLayout> ``` 2. listview_layout.xml 布局文件 该布局文件包含一个 ListView 控件。 ```xml <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 3. fragment_layout.xml 布局文件 该布局文件包含一个文本框。 ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/textViewFragment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is a fragment" /> </LinearLayout> ``` 4. MyFragment.java Fragment 类 该类包含一个 onCreateView 方法,该方法返回 fragment_layout.xml 布局文件的根视图。 ```java public class MyFragment extends Fragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_layout, container, false); return rootView; } } ``` 最后,在 MainActivity.java 文件中实现整个应用程序逻辑。MainActivity 类继承了 AppCompatActivity 类,并且包含了三个页面布局和一个 Fragment。 ```java public class MainActivity extends AppCompatActivity { private EditText nameEditText; private RadioGroup genderRadioGroup; private CheckBox agreeCheckBox; private ListView listView; private Button nextButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取视图控件 nameEditText = findViewById(R.id.editText); genderRadioGroup = findViewById(R.id.radioGroup); agreeCheckBox = findViewById(R.id.checkBox); nextButton = findViewById(R.id.nextButton); listView = findViewById(R.id.listView); // 设置 ListView 适配器 String[] items = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"}; ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items); listView.setAdapter(adapter); // 设置 Next 按钮点击事件 nextButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 获取用户输入 String name = nameEditText.getText().toString(); boolean isMale = genderRadioGroup.getCheckedRadioButtonId() == R.id.maleRadio; boolean agreed = agreeCheckBox.isChecked(); // 检查用户输入是否有效 if (name.isEmpty()) { Toast.makeText(MainActivity.this, "Please enter your name", Toast.LENGTH_SHORT).show(); return; } if (!agreed) { Toast.makeText(MainActivity.this, "Please agree to terms and conditions", Toast.LENGTH_SHORT).show(); return; } // 启动下一个活动 Intent intent = new Intent(MainActivity.this, SecondActivity.class); intent.putExtra("name", name); intent.putExtra("isMale", isMale); startActivity(intent); } }); // 设置 ListView 点击事件 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // 创建并显示 Fragment FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); MyFragment fragment = new MyFragment(); fragmentTransaction.replace(R.id.fragment_container, fragment); fragmentTransaction.commit(); } }); } } ``` 这样,我们就完成了一个简单的 Android 应用程序,其中包含三个页面布局、listview 控件和 Fragment
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值