Activity传数据给fragment

来源:互联网 发布:ipad能开淘宝店吗 编辑:程序博客网 时间:2024/06/07 09:35

1、

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"
    />

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.FrameLayout;
public class MainActivity extends FragmentActivity {
FrameLayout content_frame;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
content_frame = (FrameLayout) findViewById(R.id.content_frame);
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, new FrameLayoutFragment("传参数"))
.commit();
}
}

2、

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/frag_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是fragment" />
</LinearLayout>

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FrameLayoutFragment extends Fragment{
View view;
TextView  frag_text;
String aaaa;
public FrameLayoutFragment(String string) {
aaaa  = string;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.frame_layout, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
initView();
super.onActivityCreated(savedInstanceState);
}
private void initView() {
frag_text = (TextView) view.findViewById(R.id.frag_text);
frag_text.setText(aaaa + "aaa");
}
}

原创粉丝点击