这是一个碎片高级场---传值
碎片传值的三种用法
activity传fragment
package com.example.day004;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
/**
-
activity 向 fragment 传值
*/
public class A2FActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a_2_f);
}//点击事件的方法
public void click(View view) {
}
}
这是修完以后的他的xml的布局文件
<?xml version="1.0" encoding="utf-8"?><EditText
android:id="@+id/et_id"
android:hint="来啊,传点啥吧!"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点我试试"
android:onClick="click"
/>
<!--用来显示内容的,是个容器,里面可以放fragment-->
<LinearLayout
android:orientation="horizontal"
android:id="@+id/linear_layout_id"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
创建fragment(右键一键完事,带布局)
package com.example.day004.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.day004.R;
/**
*用来接受传值的
*/
public class ShowContextFragment extends Fragment {
public ShowContextFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_show_context, container, false);
}
}
修改后的fragment的xml布局文件
<?xml version="1.0" encoding="utf-8"?><!--只加了一个Id,其他可有可无-->
<TextView
android:id="@+id/context_tv_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/colorAccent"
android:textSize="30sp"
android:text="@string/hello_blank_fragment" />
完整版的fragment
package com.example.day004.fragment;
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;
import com.example.day004.R;
/**
*用来接受传值的
*/
public class ShowContextFragment extends Fragment {
private TextView textView;
public ShowContextFragment() {
// Required empty public constructor
}
//现在text里面的值是"hello_blank_fragment",思路就是拿到控件,重新赋值即可.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_show_context, container, false);
//1 拿控件
textView = inflate.findViewById(R.id.context_tv_id);
//2 给控件赋值
Bundle arguments = getArguments();
if(arguments != null){ //第一次启动一定为null,所以要判断一下
String key = arguments.getString("key");
textView.setText(key);
}
return inflate;
}
}
完整版的fragment
package com.example.day004.fragment;
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;
import com.example.day004.R;
/**
*用来接受传值的
*/
public class ShowContextFragment extends Fragment {
private TextView textView;
public ShowContextFragment() {
// Required empty public constructor
}
//现在text里面的值是"hello_blank_fragment",思路就是拿到控件,重新赋值即可.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_show_context, container, false);
//1 拿控件
textView = inflate.findViewById(R.id.context_tv_id);
//2 给控件赋值
Bundle arguments = getArguments();
if(arguments != null){ //第一次启动一定为null,所以要判断一下
String key = arguments.getString("key");
textView.setText(key);
}
return inflate;
}
}
完整版的a2fActivity
package com.example.day004;
import android.icu.util.BuddhistCalendar;
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.EditText;
import com.example.day004.fragment.ShowContextFragment;
import java.io.BufferedReader;
/**
-
activity 向 fragment 传值
*/
public class A2FActivity extends AppCompatActivity {
private FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a_2_f);//0,取到edit的控件和值 editText = findViewById(R.id.et_id); //1,在这里动态添加一个fragment fragmentManager = getSupportFragmentManager(); fragmentTransaction = fragmentManager.beginTransaction(); //注意这个布局文件,是R.layout.activity_a_2_f xml文件里的线性布局 fragmentTransaction.add(R.id.linear_layout_id,new ShowContextFragment()); fragmentTransaction.commit();
}
public void click(View view) {
//取到输入的值
String string = editText.getText().toString();
//创建fragment对象
ShowContextFragment showContextFragment = new ShowContextFragment();
//创建bundle
Bundle bundle = new Bundle();
bundle.putString(“key”,string);
//给fragment对象赋值
showContextFragment.setArguments(bundle);//动态修改fragment fragmentManager = getSupportFragmentManager(); fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.linear_layout_id,showContextFragment); fragmentTransaction.commit();
}
}