Fragment高级
Fragment:hide+show切换
//fragment切换
private void showFragment(Fragment fragment) {
if (currentFragment != fragment) {
// 判断传入的fragment是不是当前的currentFragmentgit
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.hide(currentFragment);// 不是则隐藏
currentFragment = fragment; //
// 然后将传入的fragment赋值给currentFragment
if (!fragment.isAdded()) {
// 判断传入的fragment是否已经被add()过
transaction.add(R.id.frameLayout, fragment).show(fragment).commit();
} else {
transaction.show(fragment).commit();
}
}
}
Fragment回退栈
adioGroupId.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.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());
SelectActivity.this.fragmentTransaction.commit();
break;
}
}
});
}
回退栈的意义在于点击back的时候,不会退出activity,而会返回到之前的fragment界面
Fragment传值
之前我们写过关于Activity之间的值传递,在fragment中我们也有大量的需求需要值在每个fragment,activity之间传递
Activity给Fragment传递值
完成activity部分
java代码:
class MainActivity extends AppCompatActivity {
private EditText etInput;
private Button btnSend;
private FragmentManager manager;
private FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etInput = (EditText) findViewById(R.id.et_input);
btnSend = (Button) findViewById(R.id.btn_send);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取到值
String string = etInput.getText().toString();
BlankFragment blankFragment = new BlankFragment();
//创建bundle对象
Bundle bundle = new Bundle();
bundle.putString("val",string);
//给fragment赋值
blankFragment.setArguments(bundle);
//给fragment对象赋值
manager = getSupportFragmentManager();
fragmentTransaction = manager.beginTransaction();
fragmentTransaction.replace(R.id.show,blankFragment);
fragmentTransaction.commit();
}
});
}
}
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/et_input"
android:layout_width="match_parent"
android:layout_height="50dp" />
<Button
android:id="@+id/btn_send"
android:text="发送"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="horizontal"
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
接下来需要完成Fragment
java代码:
public class BlankFragment extends Fragment {
private TextView tvShow;
public BlankFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_blank, container, false);
tvShow = view.findViewById(R.id.tv_show);
Bundle arguments = getArguments();
if (arguments!=null){
String val = arguments.getString("val");
tvShow.setText(val);
}
return view;
}
}
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=".BlankFragment">
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
Fragment给Activity传递值
第一种:
在Activity中调用getFragmentManager()得到fragmentManager,,调用findFragmentByTag(tag)或者通过findFragmentById(id)
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = fragmentManager.findFragmentByTag(tag);
第二种:
通过回调的方式,定义一个接口(可以在Fragment类中定义),接口中有一个空的方法,在fragment中需要的时候调用接口的方法,值可以作为参数放在这个方法中,然后让Activity实现这个接口,必然会重写这个方法,这样值就传到了Activity中.
还是先上代码:
Activity部分代码
java:
public class Main2Activity extends AppCompatActivity implements MyFragment.MyListnenr {
private TextView textShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textShow = (TextView) findViewById(R.id.text_show);
}
@Override
public void sendMessage(String s) {
textShow.setText(s);
}
}
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=".Main2Activity">
<TextView
android:id="@+id/text_show"
android:layout_width="match_parent"
android:layout_height="50dp" />
<!-- 静态加载fragment -->
<fragment
android:id="@+id/fg"
android:name="com.example.highday5_2_25.MyFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"></fragment>
</LinearLayout>
fragment部分代码
java:
public class MyFragment extends Fragment {
private EditText etIn;
private Button btn;
//声明成员属性
private MyListnenr myListnenr;
@Override
public void onAttach(@NonNull Context context) {
myListnenr = (MyListnenr) getActivity();
super.onAttach(context);
}
public MyFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my, container, false);
etIn = (EditText) view.findViewById(R.id.et_in);
btn = (Button) view.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String string = etIn.getText().toString();
if (string!=null){
//发送数据到Activity
myListnenr.sendMessage(string);
}
}
});
return view;
}
//自定义接口
public interface MyListnenr{
void sendMessage(String s);
}
}
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=".MyFragment">
<EditText
android:id="@+id/et_in"
android:layout_width="match_parent"
android:layout_height="50dp" />
<Button
android:id="@+id/btn"
android:layout_marginTop="50dp"
android:text="F发送给A"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
*这里我们用到了一种开发技巧叫接口回调
接口回调
首先需要用户自定义一个接口,该接口可以是只为这一个类提供,也可以被其它类去继承
//自定义接口
public interface MyListnenr{
void sendMessage(String s);
}
我们希望使用这个接口去完成我们的传值所以要完成该接口的初始化
//声明成员属性
private MyListnenr myListnenr;
@Override
public void onAttach(@NonNull Context context) {
//拿到与当前fragment关联的Activity.
myListnenr = (MyListnenr) getActivity();
super.onAttach(context);
}
后面我们需要去实现这个接口
public class Main2Activity extends AppCompatActivity implements MyFragment.MyListnenr
重新方法
@Override
public void sendMessage(String s) {
textShow.setText(s);
}
这就是最基本的接口回调实现
fragment 给 fragment 传值
首先还是先写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="horizontal"
tools:context=".Main3Activity">
<fragment
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/left"
android:name="com.example.highday5_2_25.LeftFragment"/>
<fragment
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/right"
android:name="com.example.highday5_2_25.RightFragment"/>
</LinearLayout>
接下来需要两个fragment完成传值
LeftFragment:
java:
public class LeftFragment extends Fragment {
private EditText leftInput;
private Button leftBtn;
public LeftFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_left, container, false);
leftInput = (EditText) view.findViewById(R.id.left_input);
leftBtn = (Button) view.findViewById(R.id.left_btn);
leftBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String string = leftInput.getText().toString();
FragmentManager fragmentManager = getFragmentManager();
RightFragment rightFragment = (RightFragment) fragmentManager.findFragmentById(R.id.right);
rightFragment.setRightText(string);
}
});
return view;
}
}
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=".LeftFragment">
<EditText
android:id="@+id/left_input"
android:layout_width="match_parent"
android:layout_height="40dp" />
<Button
android:id="@+id/left_btn"
android:layout_marginTop="40dp"
android:text="发送"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
RightFragment
java:
public class RightFragment extends Fragment {
private TextView rightText;
public RightFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_right, container, false);
rightText = (TextView) view.findViewById(R.id.right_text);
// rightText.setText();
return view;
}
public void setRightText(String s){
rightText.setText(s);
}
}
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=".RightFragment">
<TextView
android:id="@+id/right_text"
android:text="Welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
简单的案例(底部按钮控制fragment切换)
<?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=".Main2Activity">
<FrameLayout
android:id="@+id/fl_show"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="12"></FrameLayout>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1">
<RadioButton
android:id="@+id/btn_list"
android:layout_width="0dp"
android:onClick="onClick"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:textSize="20sp"
android:checked="true"
android:textColor="@drawable/text_color"
android:text="消息"
/>
<RadioButton
android:id="@+id/btn_2"
android:layout_width="0dp"
android:onClick="onClick"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:textSize="20sp"
android:textColor="@drawable/text_color"
android:text="联系人"
/>
<RadioButton
android:id="@+id/btn_3"
android:layout_width="0dp"
android:onClick="onClick"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:textSize="20sp"
android:textColor="@drawable/text_color"
android:text="空间"
/>
</RadioGroup>
</LinearLayout>
private RadioButton btnList;
private RadioButton btn_2;
private RadioButton btn_3;
private Fragment1 fragment1;
private Fragment2 fragment2;
private Fragment3 fragment3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
btnList = (RadioButton) findViewById(R.id.btn_list);
btnList = (RadioButton) findViewById(R.id.btn_2);
btnList = (RadioButton) findViewById(R.id.btn_3);
//创建fragment对象
fragment1 = new Fragment1();
fragment2 = new Fragment2();
fragment3 = new Fragment3();
//添加默认布局
addFragment(fragment1);
}
private void addFragment(Fragment fragment) {
//创建管理者
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fl_show,fragment);
fragmentTransaction.commit();
}
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_list:
addFragment(fragment1);
break;
case R.id.btn_2:
addFragment(fragment2);
break;
case R.id.btn_3:
addFragment(fragment3);
break;
}
}