基础传值
Activity给Fragment传值
Activity的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edt"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edbut"
android:onClick="click"
android:text="传值"
/>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fl">
</FrameLayout>
</LinearLayout>
Fragment的布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BlankFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/asd"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
Activity的逻辑代码
package com.example.day04;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
public class MainActivity extends AppCompatActivity {
private EditText edt;
private Button edbut;
private FrameLayout fl;
private FragmentManager supportFragmentManager;
private FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fl = (FrameLayout) findViewById(R.id.fl);
edt = (EditText) findViewById(R.id.edt);
edbut = (Button) findViewById(R.id.edbut);
supportFragmentManager = getSupportFragmentManager();
fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fl,new BlankFragment());
fragmentTransaction.commit();
}
public void click(View view) {
String string = edt.getText().toString();
BlankFragment blankFragment = new BlankFragment();
Bundle bundle = new Bundle();
bundle.putString("key",string);
blankFragment.setArguments(bundle);
supportFragmentManager = getSupportFragmentManager();
fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fl,blankFragment);
fragmentTransaction.commit();
}
}
Fragment的逻辑代码
package com.example.day04;
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 BlankFragment extends Fragment {
private TextView asd;
public BlankFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_blank, container, false);
asd = inflate.findViewById(R.id.asd);
Bundle arguments = getArguments();
if(arguments != null){
String key = arguments.getString("key");
asd.setText(key);
}
return inflate;
}
}
Fragment给Activity传值
Activity的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".F2AActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/f2a_tv_id"
android:textSize="30sp"
android:hint="提示而已"
/>
<fragment
android:name="com.example.day004.fragment.ShowTitleFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/f2a_fm_id">
</fragment>
</LinearLayout>
Fragment的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".fragment.ShowTitleFragment">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fm_title_et_id"
android:hint="整的啥吧"
/>
<Button
android:id="@+id/fm_title_button_id"
android:text="发送了啊"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Activity的逻辑代码
package com.example.day004;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.example.day004.fragment.ShowTitleFragment;
public class F2AActivity extends AppCompatActivity implements ShowTitleFragment.MyListener {
private TextView textView;
@Override
public void sendMessage(String string) {
textView.setText(string);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_f_2_a);
textView = findViewById(R.id.f2a_tv_id);
}
}
Fragment的逻辑代码
package com.example.day004.fragment;
import android.content.Context;
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.Button;
import android.widget.EditText;
import com.example.day004.R;
public class ShowTitleFragment extends Fragment {
private EditText editText;
private Button button;
private MyListener listener;
public ShowTitleFragment() {
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
listener = (MyListener) getActivity();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_show_title, container, false);
editText = inflate.findViewById(R.id.fm_title_et_id);
button = inflate.findViewById(R.id.fm_title_button_id);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String string = editText.getText().toString();
if(string != null){
listener.sendMessage(string);
}
}
});
return inflate;
}
public interface MyListener{
void sendMessage(String string);
}
}
Fragment给Fragment传值
Activity的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/lffl"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/rigfl"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
</LinearLayout>
Activity的逻辑代码
package com.example.month12;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager supportFragmentManager2 = getSupportFragmentManager();
FragmentTransaction fragmentTransaction2 = supportFragmentManager2.beginTransaction();
fragmentTransaction2.add(R.id.rigfl,new rightFragment(),"right");
fragmentTransaction2.commit();
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.lffl,new leftFragment(),"left");
fragmentTransaction.commit();
}
}
LeftFragment的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".leftFragment">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ET"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/but"
android:text="传输"
/>
</LinearLayout>
LeftFragment的逻辑代码
package com.example.month12;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class leftFragment extends Fragment {
private EditText ET;
private Button but;
private Myfrag myfrag;
public leftFragment() {
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
myfrag = (Myfrag) getActivity().getSupportFragmentManager().findFragmentByTag("right");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_left, container, false);
ET = (EditText) inflate.findViewById(R.id.ET);
but = (Button) inflate.findViewById(R.id.but);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String string = ET.getText().toString();
if(string != null){
myfrag.myfrag(string);
}
}
});
return inflate;
}
public interface Myfrag{
public void myfrag(String s);
}
}
RightFragment的布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".rightFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/righttv"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
RightFragment的逻辑代码
package com.example.month12;
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.FrameLayout;
import android.widget.TextView;
public class rightFragment extends Fragment implements leftFragment.Myfrag {
private TextView righttv;
public rightFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_right, container, false);
righttv = (TextView) inflate.findViewById(R.id.righttv);
return inflate;
}
@Override
public void myfrag(String s) {
righttv.setText(s);
}
}