天哥视频笔记
Fragment和Activity的通信
1. 在AFragment中定义一个IOnMessageClick接口
2. 在ContainerActivity中实现接口
java 代码:AFragment.class
public class AFragment extends Fragment {
private TextView mTvTitle;
private Button btn_change;
private Button btn_reset;
private BFragment bFragment;
private Button btn_message;
private IOnMessageClick listener;
public static AFragment newInstance(String title){
AFragment fragment=new AFragment();
Bundle bundle=new Bundle();
bundle.putString("title",title);
fragment.setArguments(bundle);
return fragment;
}
public interface IOnMessageClick{
void onClick(String text);
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
try {
listener=(IOnMessageClick) context;
}catch(ClassCastException e){
throw new ClassCastException("Activity必须实现IOnMessageClick接口");
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_a,container,false);
Log.d("AFragment","-------onCreateView-------");
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mTvTitle=(TextView)view.findViewById(R.id.tv_title);
btn_change = (Button)view.findViewById(R.id.btn_change);
btn_reset = (Button) view.findViewById(R.id.btn_reset);
btn_message = (Button) view.findViewById(R.id.btn_message);
btn_message.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//通过getActivity转成ContainerActivity,然后调用ContainerActivity里面的setData方法
// ((ContainerActivity)getActivity()).setData("你好");
listener.onClick("你好");
}
});
btn_change.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(bFragment == null){
bFragment=new BFragment();
}
Fragment fragment=getFragmentManager().findFragmentByTag("a");
if(fragment!=null){
getFragmentManager().beginTransaction().hide(fragment).add(R.id.fl_container,bFragment).addToBackStack(null).commitAllowingStateLoss();
}else{
getFragmentManager().beginTransaction().replace(R.id.fl_container,bFragment).addToBackStack(null).commitAllowingStateLoss();
}
}
});
btn_reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mTvTitle.setText("我是新文字");
}
});
if(getArguments()!=null){
mTvTitle.setText(getArguments().getString("title"));
}
}
}
关键代码1
1. 定义一个IOnMessageClick接口,实现点击传值
2. 在onAttach中,把context强制转换成IOnMessage接口,如果转换失败代表context的那个activity就有没有实现这个接口,这时就抛出异常
public interface IOnMessageClick{
void onClick(String text);
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
try {
listener=(IOnMessageClick) context;
}catch(ClassCastException e){
throw new ClassCastException("Activity必须实现IOnMessageClick接口");
}
}
关键代码2
1. 点击按钮时就能调用接口的onClick方法,传递数据传到activity中
listener.onClick("你好");
xml 代码:fragment_a.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<Button
android:id="@+id/btn_message"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="给Activity传递消息"
android:textAllCaps="false"/>
<Button
android:id="@+id/btn_change"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="更换为BFragment"
android:textAllCaps="false"/>
<Button
android:id="@+id/btn_reset"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="更换为TextView的文字内容"
android:textAllCaps="false"/>
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="20sp"
android:text="我是AFragment"
android:gravity="center"/>
</LinearLayout>
java 代码:ContentActivity.class
public class ContainerActivity extends AppCompatActivity implements AFragment.IOnMessageClick{
private AFragment aFragment;
private TextView tv_title;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_container);
tv_title = (TextView) findViewById(R.id.tv_title);
aFragment=AFragment.newInstance("我是参数");
getSupportFragmentManager().beginTransaction().add(R.id.fl_container,aFragment,"a").commitAllowingStateLoss();
}
@Override
public void onClick(String text) {
tv_title.setText(text);
}
}
关键代码1
1. 实现IOnMessageClick接口
public class ContainerActivity extends AppCompatActivity implements AFragment.IOnMessageClick{
@Override
public void onClick(String text) {
tv_title.setText(text);
}
}
xml 代码:content_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000"
android:text="Hello"/>
<FrameLayout
android:id="@+id/fl_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tv_title"
/>
</RelativeLayout>