1.MainActivity传Fragment
public class MainActivity extends AppCompatActivity {
private LinearLayout ll;
private EditText edTx;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll = (LinearLayout) findViewById(R.id.ll);
edTx = (EditText) findViewById(R.id.ed_tx);
btn = (Button) findViewById(R.id.btn);
}
public void btn(View view) {
String s = edTx.getText().toString();
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
ContentFragment contentFragment = new ContentFragment();
Bundle bundle = new Bundle();
bundle.putString("name",s);
contentFragment.setArguments(bundle);
fragmentTransaction.replace(R.id.ll,contentFragment);
fragmentTransaction.commit();
}
}
------------------------------
public class ContentFragment extends Fragment {
private TextView fgView;
public ContentFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_content, container, false);
fgView = (TextView) inflate.findViewById(R.id.fg_view);
Bundle arguments = getArguments();
if (arguments!=null){
String name = arguments.getString("name");
fgView.setText(name);
}
return inflate;
}
}
Main布局
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ed_tx">
</EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:onClick="btn"
android:text="send">
</Button>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ll"
android:orientation="vertical">
</LinearLayout>
Fragment布局
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fg_view"
android:text="@string/hello_blank_fragment" />
2.Fragment传MainActivity
Main页面
@Override
public void send(String s) {
tv1.setText(s);
}
Fragment
interface MyListener{
void send(String s);
}
private MyListener myListener;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_send, container, false);
fraEdit = (EditText) inflate.findViewById(R.id.fra_edit);
btn = (Button)inflate.findViewById(R.id.btn);
myListener = (MyListener) getActivity();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = fraEdit.getText().toString();
myListener.send(s);
}
});
return inflate;
}
3. listViewMain传值Fragment
Main页面----------
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String food_str = food.getData().get(position).getFood_str();
String title = food.getData().get(position).getTitle();
String collect_num = food.getData().get(position).getCollect_num();
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
ListFragment listFragment = new ListFragment();
Bundle bundle = new Bundle();
bundle.putString("key",food_str);
bundle.putString("key1",title);
bundle.putString("key2",collect_num+"");
listFragment.setArguments(bundle);
fragmentTransaction.replace(R.id.ll,listFragment);
fragmentTransaction.commit();
}
});
Fragment页面-------------------
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_list, container, false);
fraTv = inflate.findViewById(R.id.fra_tv);
fraTv1 = inflate.findViewById(R.id.fra_tv1);
fraTv2 = inflate.findViewById(R.id.fra_tv2);
Bundle arguments = getArguments();
if (arguments!=null){
String key = arguments.getString("key");
String key1 = arguments.getString("key1");
String key2 = arguments.getString("key2");
fraTv1.setText(key1);
fraTv.setText(key);
fraTv2.setText(key2);
}
return inflate;
}
Frament.xml -------------------
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:id="@+id/fra_tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/hello_blank_fragment" />
<TextView
android:id="@+id/fra_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/hello_blank_fragment" />
<TextView
android:id="@+id/fra_tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/hello_blank_fragment" />
</LinearLayout>
Main页面-----------
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="6">
</ListView>
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_weight="1"
android:orientation="vertical">
</LinearLayout>