将数据从Fragment推送到Fragment

时间:2018-03-26 07:34:36

标签: android android-layout android-fragments

我是Android新手。如果这是一个愚蠢的问题,请原谅我:

如果我点击片段的按钮 - 它必须触发测试并在其他片段中显示结果。所以为了简单起见,按下按钮点击代码如下。它不起作用。请建议。

我的实际意图是注册一个log.add BroadcastReceiver,以便在调用任何日志错误或调试的任何地方,该消息必须出现在TestStatusFragment中。

public class TriggerTestFragment extends Fragment {

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

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_trigger, container, false);

        Button stopScheduler = (Button) view.findViewById(R.id.testButton);

        stopScheduler.setOnClickListener((View sview) -> {

            View teststatusView = inflater.inflate(R.layout.fragment_teststatus, container, false);

            TextView textView = (TextView) teststatusView.findViewById(R.id.text_view);

            textView.setText("test result....");

        });

        return view;
    }
}

MainActivity.java

private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new TriggerTestFragment(), "TRIGGERTEST");
        adapter.addFragment(new TestStatusFragment(), "TEST STATUS");
        viewPager.setAdapter(adapter);
    }

第二片段:

public class TestStatusFragment extends Fragment {

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

        Context context = getContext();

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_teststatus, container, false);


        return view;
    }
}

1 个答案:

答案 0 :(得分:0)

使用界面在它们之间进行通信。

Fragment One

public class FragOne extends Fragment {

    EditText etxtName, etxtDesc;
    Button btnSubmit;
    String name, desc;

    private OnFragmentInteractionListener mListener;

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

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

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

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

        etxtName = (EditText) view.findViewById(R.id.etxtName);
        etxtDesc = (EditText) view.findViewById(R.id.etxtDesc);
        btnSubmit = (Button) view.findViewById(R.id.btnSubmit);

        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                name = etxtName.getText().toString().trim();
                desc = etxtDesc.getText().toString().trim();

                if(name == null || desc == null) {
                    Toast.makeText(getActivity(), "Both fields required", Toast.LENGTH_SHORT).show();
                } else {
                    mListener.onFragmentInteraction(name, desc);
                    etxtName.setText("");
                    etxtDesc.setText("");
                }
            }
        });
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(String name, String desc);
    }
}

片段二

public class FragTwo extends Fragment implements FragOne.OnFragmentInteractionListener{

    TextView textView, textDesc;

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

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

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

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        textView = (TextView) view.findViewById(R.id.txtName);
        textDesc = (TextView) view.findViewById(R.id.txtDesc);
    }

    @Override
    public void onFragmentInteraction(String name, String desc) {
        textView.setText(name);
        textDesc.setText(desc);
    }

}

初始化片段

fragOne = new FragOne();
fragTwo = new FragTwo();

PagerAdapter

public static class MyPagerAdapter extends FragmentPagerAdapter implements FragOne.OnFragmentInteractionListener{

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return fragOne;
            case 1:
                return fragTwo;
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "Page " + position;
    }

    @Override
    public void onFragmentInteraction(String name, String desc) {
        fragTwo.onFragmentInteraction(name, desc);
    }
}

MainActivity需要实现onFragmentInteraction接口

public class MainActivity extends AppCompatActivity implements FragOne.OnFragmentInteractionListener{

    static FragOne fragOne;
    static FragTwo fragTwo;
    MyPagerAdapter myPagerAdapter;
    ViewPager viewPager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragOne = new FragOne();
        fragTwo = new FragTwo();
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(myPagerAdapter);
    }

    @Override
    public void onFragmentInteraction(String name, String desc) {
        myPagerAdapter.onFragmentInteraction(name, desc );
    }

    public static class MyPagerAdapter extends FragmentPagerAdapter implements FragOne.OnFragmentInteractionListener{

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return fragOne;
                case 1:
                    return fragTwo;
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return "Page " + position;
        }

        @Override
        public void onFragmentInteraction(String name, String desc) {
            fragTwo.onFragmentInteraction(name, desc);
        }
    }
}

您可以在此处阅读完整文章https://www.mytrendin.com/passing-data-between-fragments/