使用Fragment发送数据 - Android

时间:2015-11-24 12:57:01

标签: android android-fragments

我有一个寻呼机视图,在下面的方法中,我们根据所选位置指示寻呼机显示什么。

此方法返回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;
        }

    }

我想用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;
    }
}

请帮忙

3 个答案:

答案 0 :(得分:4)

如何将数据放入Fragment

您可以使用Bundle将数据发送到Fragment

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

如何从Fragment

中检索数据

在片段中,检索数据:

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

详细了解Bundle

此外,如果您的类实现了Parcelable,您可以将您的Entity类放在bundle中,这样您就可以将类对象发送到-Fragment的片段

答案 1 :(得分:0)

我建议你让代码片段看起来像这样

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);
    }


}

并且在视图分页器代码中,您可以传递片段中所需的多个参数。例如,上面的片段使用了2个参数。

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

答案 2 :(得分:0)

这是一个很好的标准片段示例,可以使用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
    }
}

您可以通过调用

来创建片段实例
MyFragment fragment = MyFragment.newInstance(999, "Some String", new MyClass());

如果你要传递一个类的实例,请确保该类正在实现Serializable