从活动运行时与片段进行通信(获取用户输入后)

时间:2018-08-02 03:27:17

标签: android-fragments android-viewpager bundle android-tablayout

我在mainAcitivty中有带有viewpager的FAB和TabLayout。当用户通过FAB提供输入时,我想更新viewpager的片段。尝试通过Bundle发送数据,但在从fragment接收时,总是Bundle为null。 我认为我的问题在于接收数据。我应该使用哪个片段方法来接收数据?

这是我的MainActivity

package com.example.shahalam.rvfabvptl;

import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


    EditText title, description;
    FloatingActionButton fab;
    ViewPager mPager;
    TabLayout tabLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mPager = findViewById(R.id.viewPager);
        tabLayout = findViewById(R.id.tabLayout);
        fab = findViewById(R.id.fab);

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
                View mView = getLayoutInflater().inflate(R.layout.dialog_box,null);

                title = mView.findViewById(R.id.titleDialogBox);
                description = mView.findViewById(R.id.descriptionDialogBox);
                Button btnOK = mView.findViewById(R.id.btnOk);

                mBuilder.setView(mView);
                final AlertDialog dialog = mBuilder.create();
                dialog.show();
                btnOK.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        FragmentManager fm = getSupportFragmentManager();
                        final FragmentTransaction ft = fm.beginTransaction();
                        final FragmentOne fragOne = new FragmentOne();
                        Bundle mBundle = new Bundle();

                        mBundle.putString("title", title.getText().toString());
                        mBundle.putString("description", description.getText().toString());

                        //PASS OVER THE BUNDLE TO OUR FRAGMENT
                        FragmentOne myFragment = new FragmentOne();
                        myFragment.setArguments(mBundle);

                        ft.add(R.id.linearLayoutFragmentOne,fragOne);
                        ft.commit();
                        dialog.cancel();
                    }
                });
            }
        });

        ViewPagerAdapter pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(pagerAdapter);

        tabLayout.setupWithViewPager(mPager);

    }

}

这是我的Fragment类:

    package com.example.shahalam.rvfabvptl;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class FragmentOne extends Fragment {

    public FragmentOne() {
    }

    RecyclerView fragOneRecyclerView;
    List<MyDataModel> myDataModels = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_one,container,false);

        fragOneRecyclerView = view.findViewById(R.id.fragmentOneRecyclerView);
        fragOneRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        final RecyclerViewAdapter adapterFragmentOne = new RecyclerViewAdapter(getContext(),myDataModels);
        fragOneRecyclerView.setAdapter(adapterFragmentOne);

        Bundle mBundle =getArguments();
        String title="if Bundle is null", description="No description";
        if (mBundle != null) {
            title = this.getArguments().getString("title");
            description = this.getArguments().getString("description");
        }

        MyDataModel mOne4 = new MyDataModel(title,description);

        myDataModels.add(mOne4);

        adapterFragmentOne.notifyDataSetChanged();

        return view;
    }


}

1 个答案:

答案 0 :(得分:0)

  

您正在创建片段FragmentOne的两个不同的对象,   和setting the bundle to object myFragmentpassing another fragment object fragOne in FragmentTransition.这是错误的,请使用   下面的代码。

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
FragmentOne fragOne = new FragmentOne();
Bundle mBundle = new Bundle();
mBundle.putString("title", title.getText().toString());
mBundle.putString("description", description.getText().toString());

  //PASS OVER THE BUNDLE TO OUR FRAGMENT
  fragOne.setArguments(mBundle);
  ft.add(R.id.linearLayoutFragmentOne,fragOne);
  ft.commit();
  dialog.cancel();