在片段之间传递数据

时间:2016-07-19 07:36:29

标签: android android-fragments fragment android-recyclerview

我有一个MainActivity和两个片段。在FragmentA中,我有一个回收站视图。如果我点击"添加"按钮那里,FragmentB是开放的。我想要的是将文本写入一些EditTexts并将数据发送回FragmentA(并在回收器视图中呈现该数据)。你能给我一些建议吗?感谢

FragmentB

public class NewContactFragment extends Fragment {

    EditText name, number, email;
    public String mName;
    public String mNumber;
    public String mEmail;
    boolean isFavourite = false;

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

    public static NewContactFragment newInstance() {
        NewContactFragment fragment = new NewContactFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);

        return fragment;

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);

        //set title
        ((MainActivity) getActivity()).getSupportActionBar().setTitle(R.string.new_contact);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_new_contact, container, false);

        name = (EditText) view.findViewById(R.id.ed_name);
        number = (EditText) view.findViewById(R.id.ed_number);
        email = (EditText) view.findViewById(R.id.ed_email);

        mName = name.getText().toString();
        mNumber = number.getText().toString();
        mEmail = email.getText().toString();

        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.new_contact_menu, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_done:
                //TODO: save editTexts and return to ContactListFragment

                break;
            case R.id.action_favourite:
                getActivity().invalidateOptionsMenu();
                //Toast.makeText(getContext(), "isFavourite is: " + isFavourite, Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

FragmentA

public class ContactListFragment extends Fragment implements View.OnClickListener {

    private static final String TAG = "newcontact";
    FloatingActionButton fabButton;
    SearchView searchView;
    RecyclerView recyclerView;
    ContactsAdapter contactsAdapter;
    List<Contact> mContact = new ArrayList<>();

    public static ContactListFragment newInstance() {
        Bundle args = new Bundle();
        ContactListFragment fragment = new ContactListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_contact_list, container, false);

        searchView = (SearchView) view.findViewById(R.id.search_view);
        fabButton = (FloatingActionButton) view.findViewById(R.id.fab_button);
        fabButton.setOnClickListener(this);

        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mContact = SugarRecord.listAll(Contact.class);
        contactsAdapter = new ContactsAdapter(getActivity(), mContact);
        recyclerView.setAdapter(contactsAdapter);

        inputFilter();

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);

        //show actionBar
        ((MainActivity) getActivity()).getSupportActionBar().show();
        //show title
        ((MainActivity) getActivity()).getSupportActionBar().setTitle(R.string.app_name);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    public void inputFilter() {
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                contactsAdapter.filterList(newText);
                return true;
            }
        });
    }

    @Override
    //Fab button listener
    public void onClick(View v) {
        ((MainActivity) getActivity()).showFragment(new NewContactFragment(), TAG);
    }

1 个答案:

答案 0 :(得分:3)

碎片通常只应与其直接父活动进行通信。片段通过其父活动进行通信,允许活动管理来自该片段的数据的输入和输出,以与其他片段或活动协调。将Activity视为控制器,管理与其中包含的每个片段的所有交互。

有一些例外情况是dialog fragments来自另一个片段或nested child fragments。这两种情况都是片段嵌套子片段的情况,因此允许它们向上传递给它们的父片段(这是一个片段)。

要记住的重要一点是片段不应该直接相互沟通,并且通常只应与其父活动进行通信。片段应该是模块化,独立和可重用的组件。在大多数情况下,片段允许其父活动响应意图和回调。

片段和活动可以通过三种方式进行交流:

  • Bundle - Activity可以构造一个片段并设置参数
  • 方法 - 活动可以调用片段实例上的方法
  • 侦听器 - 片段可以通过界面触发活动上的侦听器事件

换句话说,沟通通常应遵循以下原则:

Creating and Using Fragments

了解更多有关Fragment及其沟通的信息