Fragment详解(一)
Fragment详解(二)
Fragment详解(三)
- Fragment与Activity之间的通信
一种回调接口的方式实现通信,接口写在Fragment
ContainerActivity
package com.example.test0508.fragment;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.example.test0508.R;
public class ContainerActivity extends AppCompatActivity implements AFragment.IOnMessageClick {
private AFragment aFragment;
private TextView mTvContainerTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_container);
mTvContainerTitle = findViewById(R.id.tv_container_title);
//实例化AFragment
aFragment = AFragment.newInstance("我是修改后的标题");
//把AFragment添加到Activity中
getSupportFragmentManager().beginTransaction().add(R.id.fl_container,aFragment,"a").commitAllowingStateLoss();
}
/**
* 不推荐
* @param text
*/
public void setData(String text){
mTvContainerTitle.setText(text);
}
/**
* 通过回调接口实现数据的传递
*/
@Override
public void onClick(String text) {
mTvContainerTitle.setText(text);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10dp"
>
<TextView
android:id="@+id/tv_container_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="@color/colorBlack"
android:text="Hello"
/>
<FrameLayout
android:layout_below="@+id/tv_container_title"
android:id="@+id/fl_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
AFragment(里面写接口,里面attach回调)
package com.example.test0508.fragment;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.test0508.R;
public class AFragment extends Fragment {
private TextView mTvTitle;
private Button mBtnChange;
private Button mBtnReset;
private BFragment bFragment;
private Button mBtnMessage;
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;
}
/**
* 设置布局文件
*/
@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;
}
/**
* findViewById 就可以做其他事情
*/
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mTvTitle = view.findViewById(R.id.tv_fragment_a);
mBtnChange = view.findViewById(R.id.btn_change);
mBtnReset = view.findViewById(R.id.btn_reset);
mBtnMessage = view.findViewById(R.id.btn_message);
//点击它之后,把Fragment替换掉
mBtnChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
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 {
//replace会导致前一个视图不会被保存下来
getFragmentManager().beginTransaction().replace(R.id.fl_container,bFragment).addToBackStack(null).commitAllowingStateLoss();
}
}
});
mBtnReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mTvTitle.setText("这是点击之后修改的新文字");
}
});
if (getArguments() != null){
mTvTitle.setText(getArguments().getString("title"));
}
mBtnMessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 不推荐 ((ContainerActivity)getActivity()).setData("我不是之前那个Hello");
listener.onClick("我不是之前那个Hello");
}
});
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
try {
listener = (IOnMessageClick) context;
}catch (ClassCastException e){
throw new ClassCastException("Activity必须实现这个接口");
}
}
/**
* 通过回调接口实现数据的传递,让Activity来实现他
*/
public interface IOnMessageClick{
void onClick(String text);
}
}
<?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_fragment_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="我是Fragment A"
android:gravity="center"
/>
</LinearLayout>