使用 TabLayout 将数据从 Fragment 发送到 Fragment

Sending Data From Fragment to Fragment Using TabLayout

在使用 TabLayout 时,我一直无法将数据从一个片段发送到另一个片段。我已经阅读了大量关于如何执行此操作的在线帖子,但在自己实施时却没有成功。

我在我的应用程序中设置了四个片段。我正在尝试将从第四个片段中的 EditText 元素收集的数据发送到第二个片段的 TextView 元素。

我没有收到任何错误,但文本没有出现在第二个片段中。我不知道如何从这里开始。谢谢!

第四个片段(发送片段)

public class settings extends Fragment {
    SendMessage SM;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(
                R.layout.setl, container, false);
        return rootView;


    }

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

        Button btnPassData = (Button) view.findViewById(R.id.btnPassData);
        final EditText inData = (EditText) view.findViewById(R.id.inMessage);
        btnPassData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SM.sendData(inData.getText().toString().trim());
            }
        });

    }

    public interface SendMessage {
        void sendData(String message);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        try {
            SM = (SendMessage) getActivity();
        } catch (ClassCastException e) {
            throw new ClassCastException("Error in retrieving data. Please try again");
        }
    }
}

第二个片段(接收片段)

public class tt extends Fragment {

    TextView txtData;

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

        return rootView;



    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        txtData = (TextView)view.findViewById(R.id.txtData);


    }

    public void displayReceivedData(String message) {
        txtData.setText("Data received: "+ message);

    }
}

主要Activity

public class MainActivity extends AppCompatActivity implements settings.SendMessage {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_test);
        SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
        ViewPager viewPager = findViewById(R.id.view_pager);
        viewPager.setAdapter(sectionsPagerAdapter);
        TabLayout tabs = findViewById(R.id.tabs);
        tabs.setupWithViewPager(viewPager);
    }

    @Override
    public void sendData(String message) {
        String tag = "android:switcher:" + R.id.view_pager + ":" + 1;
        tt f = (tt) getSupportFragmentManager().findFragmentByTag(tag);
        f.displayReceivedData(message);
    }
}

您应用的其他部分可能出现问题。

以下是您的工作示例的代码。

class 主活动

public class MainActivity extends AppCompatActivity implements Setting.SendMessage {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(),this );
        ViewPager viewPager = findViewById(R.id.viewpager);
        viewPager.setAdapter(sectionsPagerAdapter);
        TabLayout tabs = findViewById(R.id.sliding_tabs);
        tabs.setupWithViewPager(viewPager);
    }

    @Override
    public void sendData(String message) {
        String tag = "android:switcher:" + R.id.viewpager + ":" + 1;
        tt f = (tt) getSupportFragmentManager().findFragmentByTag(tag);
        f.displayReceivedData(message);
    }
}

class 设置

 public class Setting extends Fragment {
        SendMessage SM;
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View rootView = inflater.inflate(
                    R.layout.setl, container, false);
            return rootView;
        }

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

            Button btnPassData = (Button) view.findViewById(R.id.btnPassData);
            final EditText inData = (EditText) view.findViewById(R.id.inMessage);
            btnPassData.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SM.sendData(inData.getText().toString().trim());
                }
            });

        }
        public interface SendMessage {
            void sendData(String message);
        }

        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            try {
                SM = (SendMessage) getActivity();
            } catch (ClassCastException e) {
                throw new ClassCastException("Error in retrieving data. Please try again");
            }
        }
    }

class tt

public class tt extends Fragment {

    TextView txtData;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(
                R.layout.tt, container, false);
        return rootView;
    }

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

    public void displayReceivedData(String message) {
        txtData.setText("Data received: "+ message);

    }
}

class SectionsPagerAdapter

public class SectionsPagerAdapter  extends FragmentPagerAdapter {
    final int PAGE_COUNT = 2;
    private String tabTitles[] = new String[] { "Tab1", "Tab2"};
    private Context context;

    public SectionsPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

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

    @Override
    public Fragment getItem(int position) {
        if(position==0){
            return new Setting();
        }
        else
            return new tt();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabTitles[position];
    }
}