android activity向fragment通信,获取fragment的三种方法

发布于:2021-05-12 20:00:27

三种方法
第一种:静态fragment


如果是在layout文件中定义的fragment,则使用



getSupportFragmentManager().findFragmentById();
第二种:动态fragment


如果还在程序中代码变更了fragment,则使用



getSupportFragmentManager().findFragmentByTag();
第三种:使用ViewPager装载的fragment


如果使用ViewPager装载fragment时,则需要修改Adapter



定义SmartFragmentStatePagerAdapter.java

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.util.SparseArray;
import android.view.ViewGroup;

/*
Extension of FragmentStatePagerAdapter which intelligently caches
all active fragments and manages the fragment lifecycles.
Usage involves extending from SmartFragmentStatePagerAdapter as you would any other PagerAdapter.
*/
public abstract class SmartFragmentStatePagerAdapter extends FragmentStatePagerAdapter {
// Sparse array to keep track of registered fragments in memory
private SparseArray registeredFragments = new SparseArray();

public SmartFragmentStatePagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}

// Register the fragment when the item is instantiated
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}

// Unregister when the item is inactive
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}

// Returns the fragment for the position (if instantiated)
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
}

将SectionsPagerAdapter继承于SmartFragmentStatePagerAdapter

/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends SmartFragmentStatePagerAdapter {

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

@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch (position) {
case 0:
return Fragment1.newInstance(position);
case 1:
return Fragment2.newInstance(position);
default:
return null;
}
}

@Override
public int getCount() {
// Show 2 total pages.
return 2;
}

@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 0";
case 1:
return "SECTION 1";
}
return null;
}
}

在onCreate()等中加入初始化代码

mSmartFragmentStatePagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.fragment_container);
mViewPager.setAdapter(mSmartFragmentStatePagerAdapter);

使用的时候只需下面的代码即可

((Fragment1) mSmartFragmentStatePagerAdapter.getRegisteredFragment(1));

相关推荐

最新更新

猜你喜欢