简体   繁体   English

使用Fragment发送数据-Android

[英]Send data with Fragment - Android

I have a pager view and in the below method we instruct pager what to display based on the selected position. 我有一个寻呼机视图,在下面的方法中,我们指示寻呼机根据所选位置显示什么。

This method return the fragment for the every position in the View Pager 此方法返回View Pager中每个位置的片段

 @Override
    public Fragment getItem(int position) {

        if(position == 0) 
        {
            Tab1 tab1 = new Tab1();
            return tab1;
        }
        else   if(position == 1)         
        {
            Tab2 tab2 = new Tab2();
            return tab2;
        }
        else  if(position == 2) {
            Tab3 tab3 = new Tab3();
            return tab3;
        }

        else  if(position == 3) {
            Tab4 tab4 = new Tab4();
            return tab4;
        }
        else  if(position == 4) {
            Tab5 tab5 = new Tab5();
            return tab5;
        }
        else {
            Tab6 tab6 = new Tab6();
            return tab6;
        }

    }

I want to sent argument with the Tab1 so that i can display data based on parameter 我想使用Tab1发送参数,以便我可以基于参数显示数据

public class Tab1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v =inflater.inflate(R.layout.tab_1,container,false);
        return v;
    }
}

Please help 请帮忙

How to put data in Fragment 如何将数据放入片段

You can use Bundle to send data to Fragment 您可以使用Bundle将数据发送到Fragment

Tab1 someFragment = new Tab1();
Bundle bundle = new Bundle();
bundle.putInt(key, value);
someFragment.setArguments(bundle);

How to retrieve data from Fragment 如何从片段中检索数据

In your Fragment, retrieve the data: 在您的片段中,检索数据:

Bundle bundle = this.getArguments();
int myDataInt = bundle.getInt(key, defaultValue);

Read more about Bundle 进一步了解Bundle

Also, you can put your Entity classes in bundle, if your class implements Parcelable , in this way you can send class objects to-an-from Fragment 另外,如果您的实体实现了Parcelable ,则可以将Entity类放入包中,这样您就可以将类对象发送给Fragment

I would suggest you to make you fragment code to look something like this 我建议您使代码片段看起来像这样

public class Tab1 extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;


    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment Tab1.
     */
    // TODO: Rename and change types and number of parameters
    public static Tab1 newInstance(String param1, String param2) {
        Tab1 fragment = new Tab1();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public Tab1() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_tab1, container, false);
    }


}

and in the view pager code you can pass as many number of parameters you need in you fragment. 并且可以在查看寻呼机代码中传递片段中所需数量的参数。 Above fragment uses 2 parameters for instance. 上面的片段例如使用2个参数。

    if(position == 0) 
                {
                    Tab1 tab1 = Tab1.newInstance("param1","param2");
                    return tab1;
                }

Here's an example of a good standard Fragment with the ability to add arguments easily, by using newInstance() 这是一个很好的标准Fragment的示例,它可以通过使用newInstance()轻松添加参数

public class MyFragment extends Fragment {

    public static MyFragment newInstance(int someInt, String someString, MyClass myClass){
        MyFragment myFrgament = new MyFragment();

        Bundle bundle = new Bundle();
        bundle.putInt("SomeIntKey", someInt);
        bundle.putString("SomeStringKey", someString);
        bundle.putSerializable("SomeSerializableKey", someSerializable);

        myFragment.setArguments(bundle);

        return myFragment;
    }



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        Bundle arguments = getArguments();
        if (arguments != null){
            int someInt = arguments.getInt("SomeIntKey");
            String someString = arguments.getString("SomeStringKey");
            MyClass myClass = arguments.getSerializable("SomeSerializableKey");
        }

        // Normal Fragment onCreateView Code
    }
}

And you can create your fragment instance by calling 您可以通过调用创建片段实例

MyFragment fragment = MyFragment.newInstance(999, "Some String", new MyClass());

If you're passing in an instance of a class, make sure that class is implementing Serializable . 如果要传递一个类的实例,请确保该类正在实现Serializable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM