Ich habe dieses Tutorial (http://www.truiton.com/2015/06/android-tabs-example-fragments-viewpager/) verfolgt, um Swipe-Registerkarten zu erstellen.findFragmentById() in Tablayout Fragmente Kommunikation?
Allerdings habe ich Schwierigkeiten bei der Umsetzung von Fragment-Kommunikation. Im Grunde möchte ich Daten von einem editText in fragment2 an Fragment 3 übergeben. Die Methode findFragmentById() benötigt ein Ressourcen-ID-Argument, aber ich habe keine Fragment-ID in den xml-Dateien wie im Tutorial definiert. Gibt es hier eine alternative Methode zur Fragmentkommunikation?
Dies ist das Fragment, aus dem ich Daten übergeben möchte.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Tab 2"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="enter here"
android:ems="10"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:id="@+id/editText" />
Und hier ist die Java-Datei
public class TabFragment2 extends Fragment {
EditText editText;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_tab_fragment2, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
editText = (EditText)getActivity().findViewById(R.id.editText);
}
public String getEditText(){
return editText.getText().toString();
}
}
Dies ist das Fragment Ich möchte Daten zu übergeben zu
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Tab 3"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:id="@+id/button" />
Und hier ist die Java Datei
public class TabFragment3 extends Fragment {
Button button;
TabInterface tabInterface;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_tab_fragment3, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
button = (Button)getActivity().findViewById(R.id.button);
tabInterface = (TabInterface)getActivity();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String show = tabInterface.respond();
Toast.makeText(getActivity(),show,Toast.LENGTH_SHORT).show();
}
});
}
public interface TabInterface {
public String respond();
}
}
Die MainActivity
, die die Schnittstelle
public class MainActivity extends AppCompatActivity implements TabFragment3.TabInterface{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
@Override
public String respond() {
FragmentManager fragmentManager = getSupportFragmentManager();
TabFragment2 tabFragment2 = fragmentManager.findFragmentById();
return tabFragment2.getEditText();
}
}
Bei dem obigen Verfahren respond implementiert() welchem Argument kann ich pass auf fragmentManager.findFragmentById()?
Die XML-Datei
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.drngrne.fragmenttabtest.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/tab_layout"/>
Und hier ist die FragmentStatePagerAdapter Datei:
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabFragment1 tab1 = new TabFragment1();
return tab1;
case 1:
TabFragment2 tab2 = new TabFragment2();
return tab2;
case 2:
TabFragment3 tab3 = new TabFragment3();
return tab3;
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
Hallo, danke für die Antwort, aber ich habe eine Frage. In Fragment2, wo sollte ich speziell onMessageEvent() verwenden? –
In Ihrem Fall sollten Sie ein Ereignis wie "onResponse (Fragment2Event)" in Ihrem Fragment2 registrieren. Sie können "post (Fragment2Event)" aufrufen, um das Fragment 2 zu benachrichtigen, dass auf die Schaltfläche fragment3 geklickt wurde. Dann können Sie die edittext-Nachricht von fragment2 -> fragment3 über onMessage zurückschreiben. –
Ok, was meinst du damit, ich poste ein Ereignis in fragment3 und wenn die Schaltfläche angeklickt wird, würde es die onEvent() in fragment2 auslösen, die dann ein Ereignis in fragment2 postet, das ein onEvent() in fragment3 auslösen würde? –