pass data to another fragment by swipe view with tab android studio,not button

后端 未结 4 744
别那么骄傲
别那么骄傲 2020-12-11 04:05

Is it possible to pass a data from fragment to fragment by swipe?

There are many articles teaching us how to pass the data from fragment to fragment, but most of t

相关标签:
4条回答
  • 2020-12-11 04:46

    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..

    public class WorkData {
     string information;
     string subCon;
    ... etc..
    }
    

    You add an instance of this to hold the master copy to your "tab" activity:

    public class Tab extends ActionBarActivity implements ActionBar.TabListener {
    ...
     WorkData workData = new WorkData();
    ...
    

    I would then suggest a simple interface that each of your "tab" fragments implement; something like:

    public interface DataUpdate {
     void setData(WorkData data);
     WorkData getData();
    }
    

    Each of your tab fragments would implement this interface, updating the WorkData as required..

    public class WorkForce extends Fragment implements DataUpdate {
    ...
      private WorkData workData; // this fragment's "copy" of the data
    ...
    @Override
    public WorkData getData() {
      this.workData.subCon = this.subCon; // Assuming subcon has been updated.. else use txt1.getText();
      return this.workData;
    }
    
    @Override
    public 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...

    @Override
    public void onTabSelected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
     int position = tab.getPosition();
     DataUpdate dataUpdate = (DataUpdate) TabAdapter.getItem(position);
     // Pass the master workdata to the selected fragment
     dataUpdate.setData(this.workData);
    }
    
    @Override
    public void onTabUnselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
     int position = tab.getPosition();
     DataUpdate dataUpdate = (DataUpdate) TabAdapter.getItem(position);
     // Update the master workdata from the unselected fragment
     this.workData = dataUpdate.getData();
    }
    
    @Override
    public void onTabReselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
     // This might be pointless, but we'll do it anyway..
     int position = tab.getPosition();
     DataUpdate dataUpdate = (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.

    public class TabPagerAdapter extends FragmentStatePagerAdapter {
     private static final int NUMBER_OF_TABS = 3;
     private Fragment[] tabList = new Fragment[NUMBER_OF_TABS];
    
            public TabPagerAdapter(FragmentManager fm) {
                super(fm);
            }
           @Override
            public Fragment getItem(int i) {
                if (tabList[i] != null) {
                  // Return a tab we created earlier..
                  return tabList[i];
                } else {
                  switch (i) {
                      case 0:
                          tabList[0] = Information.newInstance("name");
                          return  tabList[0];
                      case 1:
                          tabList[1] = WorkForce.newInstance("SubCon");
                          return tabList[1];
                      case 2:
                          tabList[2] = WorkDetailsTable.newInstance();
                          return tabList[2];
                  }
                }
                return null ;
            }
            @Override
            public int getCount() {
                return NUMBER_OF_TABS;
            }
    

    Hope this helps. Good luck :-)

    0 讨论(0)
  • 2020-12-11 04:50

    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.

    0 讨论(0)
  • 2020-12-11 05:00

    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(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        mviewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                adapter = ((SOFragmentPagerAdapter) mviewpager.getAdapter());
    
    //getting the view of fragments at positions              
    
    
     if(position==0)
                {
                    View v = 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=new ArrayAdapter<String>(SOListItemSelectedActivity.this,android.R.layout.simple_list_item_1,soRequestFragment.al_list_of_services);
                    lv.setAdapter(arrayAdapter);
                }
    
                if(position==1)
                {
    
    
                }
            }
    
            @Override
            public void onPageSelected(int position) {
                if(position==0)
                {
                    View v = null;
                    soRequestFragment=(SORequestFragment)adapter.getFragment(position);
                    v=soRequestFragment.getMyView();
                }
                if(position==1)
                {
    
    
                }
            }
    
            @Override
            public void onPageScrollStateChanged(int state) {
    
            }
        });
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mviewpager.setCurrentItem(tab.getPosition());
            }
    
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
    
            }
    
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
    
            }
        });
    

    and create a FragmentPagerAdapter as:

    public class SOFragmentPagerAdapter extends FragmentPagerAdapter {
    HashMap<Integer,Fragment> mPageReferenceMap;
    int mNumOfTabs;
    public SOFragmentPagerAdapter(FragmentManager fm,int mNumOfTabs) {
        super(fm);
        this.mNumOfTabs=mNumOfTabs;
        mPageReferenceMap=new HashMap<Integer,Fragment>();
    }
    
    @Override
    public Fragment getItem(int position) {
          switch (position)
          {
              case 0:
                  Fragment1 fragment1=new tFragment1();
                  mPageReferenceMap.put(position,fragment1);
                  return fragment1;
    
              case 1:
                  Fragment2 fragment2=new Fragment2();
                  mPageReferenceMap.put(position,fragment2);
                  return fragment2;
    
              default:
                  return null;
          }
    
    }
    public Fragment getFragment(int key) {
        return mPageReferenceMap.get(key);
    }
    @Override
    public int getCount() {
        return 2;
    }}
    

    In Fragments add the getmyview() which will return the view of that fragment as:

    public void getmyview()
    

    { 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.

    0 讨论(0)
  • 2020-12-11 05:05

    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 :)

    0 讨论(0)
提交回复
热议问题