Fragment 回退栈
radioGroupId.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.person_id:
manager = getSupportFragmentManager();
fragmentTransaction = manager.beginTransaction();
//add(R.id.frame_layout_id,new FristFragment());
//入栈
fragmentTransaction.addToBackStack("11");
fragmentTransaction.commit();
break;
case R.id.message_id:
manager = getSupportFragmentManager();
//出栈
manager.popBackStack();
SelectActivity.this.fragmentTransaction = manager.beginTransaction();
SelectActivity.this.fragmentTransaction.replace(R.id.frame_layout_id,new SecondFragment());
// fragmentTransaction.addToBackStack(null);
SelectActivity.this.fragmentTransaction.commit();
break;
default:
break;
}
}
});
case R.id.message_id:
manager = getSupportFragmentManager();
// manager.popBackStack();
fragmentTransaction = manager.beginTransaction();
fragmentTransaction.replace(R.id.frame_layout_id,new SecondFragment());
//添加了返回栈,点击back的时候,不会退出activity,而会返回到之前的fragment界面.
// fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
break;
Fragment 传值介绍
不同页面间传值是最基本的要求.(这是页面间的联动,交互)
activity 给 fragment传值
主要涉及到一个方法是getArguments()和setArguments().
一个设置属性值,一个去取属性值.
步骤: 要传的值,放到bundle对象里; 在Activity中创建该Fragment的对象fragment,通过调用
fragment.setArguments()传递到fragment中; 然后更新fragment.
在该Fragment中通过调用getArguments()得到bundle对象,就能得到里面的值。
fragment_first.xml的布局
<?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.FirstFragment">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
FirstFragment.java代码
public class FirstFragment extends Fragment {
private TextView textView;
public FirstFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_first, container, false);
textView = inflate.findViewById(R.id.textView);
Bundle arguments = getArguments();
if (arguments!=null) {
String key = arguments.getString("key");
textView.setText(key);
}
return inflate;
}
}
activity_main.xml布局
<?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">
<EditText
android:id="@+id/edit_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/edit_text"/>
<Button
android:id="@+id/button_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/select"
android:onClick="click"
android:text="发 送"/>
<LinearLayout
android:id="@+id/linear_id"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="1"></LinearLayout>
</LinearLayout>
MainActivity.java代码
public class MainActivity extends AppCompatActivity {
private EditText editId;
private Button buttonId;
private LinearLayout linearId;
private FragmentManager manager;
private FragmentTransaction beginTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editId = (EditText) findViewById(R.id.edit_id);
buttonId = (Button) findViewById(R.id.button_id);
linearId = (LinearLayout) findViewById(R.id.linear_id);
manager = getSupportFragmentManager();
beginTransaction = manager.beginTransaction();
beginTransaction.add(R.id.linear_id,new FirstFragment());
beginTransaction.commit();
}
public void click(View view) {
String string = editId.getText().toString();
FirstFragment firstFragment = new FirstFragment();
Bundle bundle = new Bundle();
bundle.putString("key",string);
firstFragment.setArguments(bundle);
manager = getSupportFragmentManager();
beginTransaction = manager.beginTransaction();
beginTransaction.replace(R.id.linear_id,firstFragment);
beginTransaction.commit();
}
}
fragment 给 activity传值
第一种:
在Activity中调用getFragmentManager()得到fragmentManager,,调用findFragmentByTag(tag)或者通过findFragmentById(id)
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = fragmentManager.findFragmentByTag(tag);
第二种:
通过回调的方式,定义一个接口(可以在Fragment类中定义),接口中有一个空的方法,在fragment中需要的时候调用接口的方法,值可以作为参数放在这个方法中,然后让Activity实现这个接口,必然会重写这个方法,这样值就传到了Activity中.
大白话,就是java的父类引用指向了子类对象
fragment的xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.SecoundFragment">
<EditText
android:id="@+id/editView_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/edit_text"
android:hint="请输入内容"/>
<Button
android:id="@+id/button_two_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发 送"
android:background="@drawable/select"/>
</LinearLayout>
fragment中的代码
public class SecoundFragment extends Fragment {
private Button mButton;
private EditText mEditText;
private MyListener mMyListener;
@Override
public void onAttach(Context context) {
super.onAttach(context);
mMyListener=(MyListener) getActivity();
}
public SecoundFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_secound, container, false);
mEditText= inflate.findViewById(R.id.editView_id);
mButton= inflate.findViewById(R.id.button_two_id);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String string = mEditText.getText().toString();
mMyListener.sendMessage(string);
}
});
return inflate;
}
public interface MyListener{
void sendMessage(String string);
}
}
Activity中的xml文件
<?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=".activitys.Main2Activity">
<TextView
android:id="@+id/textView_id"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="换掉我吧"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/lin"
android:orientation="vertical"
android:layout_weight="9">
</LinearLayout>
</LinearLayout>
Activity中的代码
public class Main2Activity extends AppCompatActivity implements SecoundFragment.MyListener {
private TextView textViewId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textViewId = (TextView) findViewById(R.id.textView_id);
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction beginTransaction = supportFragmentManager.beginTransaction();
beginTransaction.add(R.id.lin,new SecoundFragment());
beginTransaction.commit();
}
@Override
public void sendMessage(String string) {
textViewId.setText(string);
}
}
fragment 给 fragment 传值
第一种:
动态创建的fragment通过findFragmentByTag得到另一个的Fragment的对象,这样就可以调用另一个的方法了。
静态创建的fragment通过findFragmentById得到另一个的Fragment的对象,这样就可以调用另一个的方法了。
public class LeftFragment extends Fragment {
private EditText editText;
private Button send_button;
public LeftFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_left, container, false);
editText = inflate.findViewById(R.id.left_textView_id);
send_button = inflate.findViewById(R.id.left_button_id);
send_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String string = editText.getText().toString();
**了解一种即可,不重点**
//1 fragment传值方式1
// 通过id找的指定fragment,调用他的方法,给他的组件赋值.
FragmentManager fragmentManager = getFragmentManager(); //注意,这个manager是v4包下的.
RightFragment rightFragment=(RightFragment) fragmentManager.findFragmentById(R.id.right_fragment_id);
rightFragment.setTextView(string);
//2 通过id找的指定fragment,得到他所有的view.在通过findViewById 找到组件.赋值
TextView textView = getFragmentManager().findFragmentById(R.id.right_fragment_id).getView().findViewById(R.id.right_textview_id);
textView.setText(string);
//3
//因为两个fragment在一个activity里面,所有可以通过拿到activity里面所有的指定组件.
TextView textView = (TextView) getActivity().findViewById(R.id.right_textview_id);
textView.setText(string);
}
});
return inflate;
}
}
第二种(重点)
通过接口回调的方式。
与上面的接口回调用法相同
第三种:
通过setArguments,getArguments的方式。
fragment 多层嵌套
FragmentManager childFragmentManager = getChildFragmentManager();
FragmentTransaction fragmentTransaction = childFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.left_fragment_id,new RightFragment());
fragmentTransaction.commit();
案例(微信底部按钮与fragment实现界面切换)
Activity的Java代码
public class SelectActivity extends AppCompatActivity {
private FrameLayout frameLayoutId;
private RadioGroup radioGroupId;
private RadioButton personId;
private RadioButton messageId;
private static final String TAG = "SelectActivity";
private FragmentManager manager;
private FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select);
frameLayoutId = findViewById(R.id.frame_layout_id);
radioGroupId = findViewById(R.id.radio_group_id);
radioGroupId.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.person_id:
manager = getSupportFragmentManager();
fragmentTransaction = manager.beginTransaction();
// fragmentTransaction.replace(R.id.frame_layout_id,new FristFragment());
fragmentTransaction.add(R.id.frame_layout_id,new FristFragment());
fragmentTransaction.addToBackStack("11");
Log.i(TAG, "onCheckedChanged: "+SelectActivity.class.getName());
fragmentTransaction.commit();
break;
case R.id.message_id:
manager = getSupportFragmentManager();
manager.popBackStack();
SelectActivity.this.fragmentTransaction = manager.beginTransaction();
SelectActivity.this.fragmentTransaction.replace(R.id.frame_layout_id,new SecondFragment());
// fragmentTransaction.addToBackStack(null);
SelectActivity.this.fragmentTransaction.commit();
break;
default:
break;
}
}
});
}
}
activity的xml文件
<?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=".activities.SelectActivity">
<FrameLayout
android:id="@+id/frame_layout_id"
android:layout_weight="9"
android:layout_width="match_parent"
android:layout_height="0dp">
</FrameLayout>
<RadioGroup
android:id="@+id/radio_group_id"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp">
<RadioButton
android:layout_weight="1"
android:id="@+id/person_id"
android:button="@null"
android:text="联系人"
android:gravity="center"
android:checked="true"
android:drawableTop="@drawable/select_pserson"
android:textColor="@drawable/select_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
/>
<RadioButton
android:layout_weight="1"
android:id="@+id/message_id"
android:button="@null"
android:gravity="center"
android:drawableTop="@drawable/select_message"
android:textColor="@drawable/select_text"
android:text="信息"
android:layout_width="0dp"
android:layout_height="wrap_content"
/>
</RadioGroup>
</LinearLayout>
说明
android:drawableTop="@drawable/select_message"
android:textColor="@drawable/select_text"
这两个属性,参照下面链接创建
https://blog.youkuaiyun.com/qq_34178710/article/details/91411003
java代码中的fragment,右键创建即可.