Pass Data To Another Fragment By Swipe View With Tab Android Studio,not Button
Solution 1:
If I understand your problem correctly, you are essentially implementing something a little bit like a "Wizard" where each step passes information to the next step as you swipe between the tabs or select them.
So in reality your problem is how to get the information out of a fragment when it is deselected and into a fragment when selected.
At the simplest level I would suggest your activity holds the "master" copy of all of the information and passes it into/takes it from each fragment in your tab pager adapter.
You would need some kind of "Domain" object to hold all the information you need to collect. Each tab would only update the bits of information it cares about..
publicclassWorkData {
string information;
string subCon;
... etc..
}
You add an instance of this to hold the master copy to your "tab" activity:
publicclassTabextendsActionBarActivityimplementsActionBar.TabListener {
...
WorkDataworkData=newWorkData();
...
I would then suggest a simple interface that each of your "tab" fragments implement; something like:
publicinterfaceDataUpdate {
voidsetData(WorkData data);
WorkData getData();
}
Each of your tab fragments would implement this interface, updating the WorkData as required..
publicclassWorkForceextendsFragmentimplementsDataUpdate{
...
private WorkData workData; // this fragment's "copy" of the data
...
@Overridepublic WorkData getData() {
this.workData.subCon = this.subCon; // Assuming subcon has been updated.. else use txt1.getText();returnthis.workData;
}
@Overridepublic void setData(WorkData workData) {
this.workData = workData;
// Update this page's views with the workData...// This assumes the fragment has already been created and txt1 is set to a view
txt1.setText(workData.subCon);
this.subCon = workData.subCon; // Actually could just use subCon in workData, but be aware that workData actually points to the Activity's copy (kinda makes getdata redundant.. but I like symmetry and couldn't be bothered making lots of copies of the object).
}
Then you just need to add the code to pass the data backwards and forwards.. in your "Tab" activity which looks like...
@OverridepublicvoidonTabSelected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
intposition= tab.getPosition();
DataUpdatedataUpdate= (DataUpdate) TabAdapter.getItem(position);
// Pass the master workdata to the selected fragment
dataUpdate.setData(this.workData);
}
@OverridepublicvoidonTabUnselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
intposition= tab.getPosition();
DataUpdatedataUpdate= (DataUpdate) TabAdapter.getItem(position);
// Update the master workdata from the unselected fragmentthis.workData = dataUpdate.getData();
}
@OverridepublicvoidonTabReselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
// This might be pointless, but we'll do it anyway..intposition= tab.getPosition();
DataUpdatedataUpdate= (DataUpdate) TabAdapter.getItem(position);
// Pass the master workdata to the selected fragment
dataUpdate.setData(this.workData);
}
An important thing to notice here is that your TabPagerAdapter will create a new fragment every time you call getItem().. that will mean that we will never get any updates because each time we try to get the fragment it returns a new, empty fragment. We need to change this so that the fragments are still created when first asked for, but only created once so that we don't keep throwing away our work.
publicclassTabPagerAdapterextendsFragmentStatePagerAdapter {
privatestaticfinalintNUMBER_OF_TABS=3;
private Fragment[] tabList = newFragment[NUMBER_OF_TABS];
publicTabPagerAdapter(FragmentManager fm) {
super(fm);
}
@Overridepublic Fragment getItem(int i) {
if (tabList[i] != null) {
// Return a tab we created earlier..return tabList[i];
} else {
switch (i) {
case0:
tabList[0] = Information.newInstance("name");
return tabList[0];
case1:
tabList[1] = WorkForce.newInstance("SubCon");
return tabList[1];
case2:
tabList[2] = WorkDetailsTable.newInstance();
return tabList[2];
}
}
returnnull ;
}
@OverridepublicintgetCount() {
return NUMBER_OF_TABS;
}
Hope this helps. Good luck :-)
Solution 2:
Although C James provides good tips to solve your problems, I would like to introduce another way without using implementing of interfaces. Please check below link out. If you use an event bus library such a http://square.github.io/otto/, you can easily pass data you want to share among fragments and even activities. Additionally, you can reduce a lot of code line since it only requires Sender(PUBLISHING), Receiver(Subscriber) while implementation of interfaces requires additional lines of code. Here is a tutorial of Otto libarary. http://www.vogella.com/tutorials/JavaLibrary-EventBusOtto/article.html
Hope it helps :)
Solution 3:
I would more go the Observer Pattern way. Each Fragments changes an POJO with is rendered in your Fragments in some way. You simply have to Observe the pojo in your Fragments. Changing Fragments will notify interested observers without knowing them.
I believe that's a much cleaner way to implement this.
Fragment A -> PojoInstance.setXY("foo"); Fragment A -> informs the Observers which e.b Informs Fragment B:
Fragment B will see the change tru the Observer.
Because ViewPagers or other Components will cache Fragments thats a way to get information in already created Fragments, even when their are not seen.
You could also try to use an EventBus where you pass the POJO around.
Solution 4:
To transfer data from one fragment to another fragment when swipe is performed ,firstly you should get the view of the each fragment.here is the sample code that can help you out a bit. write this code in Activity:
mviewpager.addOnPageChangeListener(newTabLayout.TabLayoutOnPageChangeListener(tabLayout));
mviewpager.addOnPageChangeListener(newViewPager.OnPageChangeListener() {
@OverridepublicvoidonPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
adapter = ((SOFragmentPagerAdapter) mviewpager.getAdapter());
//getting the view of fragments at positions if(position==0)
{
Viewv=null;
Fragment1=(Fragment1)adapter.getFragment(position);
v=fragment1.getMyView();//this is how you get the view
ListView lv=(ListView)v.findViewById(R.id.lv_services);
ArrayAdapter<String> arrayAdapter=newArrayAdapter<String>(SOListItemSelectedActivity.this,android.R.layout.simple_list_item_1,soRequestFragment.al_list_of_services);
lv.setAdapter(arrayAdapter);
}
if(position==1)
{
}
}
@OverridepublicvoidonPageSelected(int position) {
if(position==0)
{
Viewv=null;
soRequestFragment=(SORequestFragment)adapter.getFragment(position);
v=soRequestFragment.getMyView();
}
if(position==1)
{
}
}
@OverridepublicvoidonPageScrollStateChanged(int state) {
}
});
tabLayout.setOnTabSelectedListener(newTabLayout.OnTabSelectedListener() {
@OverridepublicvoidonTabSelected(TabLayout.Tab tab) {
mviewpager.setCurrentItem(tab.getPosition());
}
@OverridepublicvoidonTabUnselected(TabLayout.Tab tab) {
}
@OverridepublicvoidonTabReselected(TabLayout.Tab tab) {
}
});
and create a FragmentPagerAdapter as:
publicclassSOFragmentPagerAdapterextendsFragmentPagerAdapter {
HashMap<Integer,Fragment> mPageReferenceMap;
int mNumOfTabs;
publicSOFragmentPagerAdapter(FragmentManager fm,int mNumOfTabs) {
super(fm);
this.mNumOfTabs=mNumOfTabs;
mPageReferenceMap=newHashMap<Integer,Fragment>();
}
@Overridepublic Fragment getItem(int position) {
switch (position)
{
case0:
Fragment1 fragment1=newtFragment1();
mPageReferenceMap.put(position,fragment1);
return fragment1;
case1:
Fragment2 fragment2=newFragment2();
mPageReferenceMap.put(position,fragment2);
return fragment2;
default:
returnnull;
}
}
public Fragment getFragment(int key) {
return mPageReferenceMap.get(key);
}
@OverridepublicintgetCount() {
return2;
}}
In Fragments add the getmyview() which will return the view of that fragment as:
publicvoidgetmyview()
{ return myview;//myview is fragment view which you will return in oncreateview method }
Note: Viewpager execute onpagescroll first and get the position 0,1 and when you scroll ,views at position 1,2 will execute and page selected 0 will execute. For tabselections: Tabunselected,Tabselected Tab reselected is the sequence of execution. so write accordingly in the respective positions of fragments. Hope this helps you.
Post a Comment for "Pass Data To Another Fragment By Swipe View With Tab Android Studio,not Button"