zoukankan      html  css  js  c++  java
  • Fragment间相互调用并传值

    public class MainFragment extends Fragment {
    private static final String ARG_DATE="com.example.ting.datepickerfragment,date";
    private static final String TAG_DATE="DatePickerFragment";
    private static final int REQUEST_CODE=0;
    private Button mButton;
    private Date mDate;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    super.onCreateView(inflater,container,savedInstanceState);
    View v=inflater.inflate(R.layout.buttonview,container,false);
    mButton= (Button) v.findViewById(R.id.button_view);
    mDate= (Date) getArguments().getSerializable(ARG_DATE);
    mButton.setText(getArguments().getSerializable(ARG_DATE).toString()+"");
    mButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    // Toast.makeText(getActivity(),"请不要点我!",Toast.LENGTH_SHORT).show();
    // DialogFragment.
    FragmentManager fm=getFragmentManager()//获取目前的Fragment,由于是在Fragment可以直接获取,如果在activity中需要自己创建。
    DatePickerFragment datePickerFragment=DatePickerFragment.newInstance(mDate);
    datePickerFragment.setTargetFragment(MainFragment.this,REQUEST_CODE);//设置将要被启动Fragment的父Fragment.
    datePickerFragment.show(fm,TAG_DATE);//启动DialogFragment
    }
    });
    // mButton.setText("sdfsdfsdf");
    return v;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    mDate= (Date) data.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
    mButton.setText(mDate.toString());
    }

    public static MainFragment newInstance(Date date){
    Bundle bundle=new Bundle();//将当前数据绑定到Bundle,通过Arguments传入Fragment
    bundle.putSerializable(ARG_DATE,date);
    MainFragment fragment=new MainFragment();
    fragment.setArguments(bundle);
    return fragment;
    }
    }

    /****************************************************/
    public class DatePickerFragment extends DialogFragment {
    private static final String BUNDLE_DATE = "com.example.ting.datepickerfragment.date";
    public static final String EXTRA_DATE="com.example.ting.datepickerfragment.date";
    private DatePicker mDatePicker;
    private Date date;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {//返回对话框样式
    super.onCreateDialog(savedInstanceState);
    View v = LayoutInflater.from(getActivity()).inflate(R.layout.date_picker, null);
    mDatePicker = (DatePicker) v.findViewById(R.id.date_picker_view);
    date = (Date) getArguments().getSerializable(BUNDLE_DATE);
    mDatePicker.init(date.getYear(), date.getMonth(), date.getDay(), null);
        //创建AlertDialog样式。
    return new AlertDialog.Builder(getActivity()).setTitle("Welcome").setView(v).setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
    Date mDate= new GregorianCalendar(mDatePicker.getYear(),mDatePicker.getMonth(),mDatePicker.getDayOfMonth()).getTime();
    Intent intent=new Intent();
    intent.putExtra(EXTRA_DATE,mDate);
    getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK,intent);
    //通过直接调用父Fragment的onActivityResult进行值传递。
    //父Fragment通过实现onActivityResult方法来接收数据。

    }
    }).create();
    }

    public static DatePickerFragment newInstance(Date date) {
    Bundle bundle = new Bundle();//将值绑定Bundle 通过Arguments传入子Fragment
    bundle.putSerializable(BUNDLE_DATE, date);
    DatePickerFragment datePickerFragment = new DatePickerFragment();
    datePickerFragment.setArguments(bundle);
    return datePickerFragment;
    }
    }
  • 相关阅读:
    结对项目之需求分析与原型设计
    第二次结对编程作业——毕设导师智能匹配
    历届软工作品、竞赛平台作品调研
    软件工程实践项目课程的自我目标
    Build to win!——获得小黄衫的感想
    VC++智能感知插件 Visual Assist X
    Haproxy+Keepalived高可用环境部署梳理(主主和主从模式)
    安装cronsun管理定时脚本
    四层、七层负载均衡的区别
    使用LVS实现负载均衡原理及安装配置详解
  • 原文地址:https://www.cnblogs.com/swordyt/p/6603140.html
Copyright © 2011-2022 走看看