fragment静态动态加载和与activity传输数据
程序员文章站
2022-06-17 10:14:53
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.helloworld">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.HelloWorld">
<activity android:name="com.example.helloworld.MyfragmentActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:background="@color/black"
android:id="@+id/parent_textview_id"
android:text="Fragment111">
</TextView>
<?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">
<fragment
android:layout_width="600dp"
android:layout_height="300dp"
android:id="@+id/myfragment"
android:name="com.example.helloworld.MyFragment"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rl_fragment"
android:orientation="horizontal" />
</LinearLayout>
public class MyFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.expandlista, container, false);
return view;
}
}
public class MyFragment2 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
TextView view = (TextView)inflater.inflate(R.layout.expandlista, container, false);
String name = getArguments().getString("name");
view.setText(name);
return view;
}
public void setCallback(Callback callback){
String msg="传回去:"+System.currentTimeMillis();
callback.getResult(msg);
}
public interface Callback{
void getResult(String msg);
}
}
public class MyfragmentActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myfragment);
init1();
}
public void init1() {
MyFragment2 f=new MyFragment2();
Bundle b=new Bundle();
b.putString("name","张三");
f.setArguments(b);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
//把f add到R.id.rl_fragment,可以add多个不同的fragment
ft.add(R.id.rl_fragment,f);
//ft.replace(R.id.rl_fragment,f1); //替换fragment
//设置ft.addToBackStack(null),按回退会推到动态fragment未加入的状态,否则直接退出activity
ft.addToBackStack(null);
ft.commit();
View v = findViewById(R.id.myfragment);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
f.setCallback(new MyFragment2.Callback() {
@Override
public void getResult(String msg) {
Log.e("tttt",msg);
}
});
}
});
}
}
MyFragment:静态加载
MyFragment2 :动态加载
本文地址:https://blog.csdn.net/weixin_43292547/article/details/110537657
上一篇: 骁龙775g对比麒麟985哪个更好
下一篇: django项目中使用手机号登录