package com.example.app5;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.example.app5.fragment.Context1Fragment;
import com.example.app5.fragment.ContextFragment;
public class MainActivity extends AppCompatActivity {
private LinearLayout linearLayout;
private RadioButton radioButton1;
private RadioButton radioButton2;
private RadioGroup radioGroup;
private Context1Fragment context1Fragment;
private ContextFragment contextFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = findViewById(R.id.ll);
radioButton1 = findViewById(R.id.rb1);
radioButton2 = findViewById(R.id.rb2);
radioGroup = findViewById(R.id.rg1);
FragmentManager supportFragmentManager = getSupportFragmentManager();
final FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
contextFragment = new ContextFragment();
fragmentTransaction.add(R.id.ll,contextFragment);
context1Fragment = new Context1Fragment();
fragmentTransaction.add(R.id.ll,context1Fragment);
fragmentTransaction.hide(context1Fragment);
fragmentTransaction.commit();
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radioButton = findViewById(radioGroup.getCheckedRadioButtonId());
if (radioButton.equals(radioButton1)){
FragmentManager supportFragmentManager1 = getSupportFragmentManager();
FragmentTransaction fragmentTransaction1 = supportFragmentManager1.beginTransaction();
fragmentTransaction1.hide(context1Fragment);
fragmentTransaction1.show(contextFragment);
fragmentTransaction1.commit();
}
if (radioButton.equals(radioButton2)){
FragmentManager supportFragmentManager2 = getSupportFragmentManager();
FragmentTransaction fragmentTransaction2 = supportFragmentManager2.beginTransaction();
fragmentTransaction2.hide(contextFragment);
fragmentTransaction2.show(context1Fragment);
fragmentTransaction2.commit();
}
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="9">
<!-- <androidx.viewpager.widget.ViewPager-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="match_parent">-->
<!-- </androidx.viewpager.widget.ViewPager>-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1">
<RadioGroup
android:id="@+id/rg1"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<RadioButton
android:id="@+id/rb1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:button="@null"
android:gravity="center"
android:text="One"
/>
<RadioButton
android:id="@+id/rb2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:gravity="center"
android:button="@null"
android:text="Two"
/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.Context1Fragment">
<!-- TODO: Update blank fragment layout -->
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Two"></Button>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.Context1Fragment">
<!-- TODO: Update blank fragment layout -->
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Two"></Button>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".fragment.ContextFragment">
<!-- TODO: Update blank fragment layout -->
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="One" />
</FrameLayout>
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import com.example.app5.R;
/**
* A simple {@link Fragment} subclass.
*/
public class ContextFragment extends Fragment {
private Button button;
public ContextFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_context, container, false);
button = inflate.findViewById(R.id.b1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "One", Toast.LENGTH_SHORT).show();
}
});
return inflate;
}
}```