我有navigational drawer
和每个抽屉项目的片段。现在,在每个片段下,我们使用FragmentPagerAdapter
创建了多个片段。
我有3个抽屉项目 Home
, Group
和 Feedback
。在 Home
下,有2个片段,即 My Profile
和 Logged
所以我为 My Profile
和 Logged
创建了一个可滑动的标签页,这两个片段位于主页项下。现在,我已经在两个片段( My Profile, Logged
)中进行了一些网络调用,但是如何识别FragmentPagerAdapter
上哪个片段处于活动状态?是否可以提前停止适配器创建片段。
HomeFragment
DrawerModel model = new DrawerModel();
LoggedFragment loggedFrag = new LoggedFragment();
loggedFrag.setTargetFragment(loggedFrag, 0);
model.setFragment(loggedFrag);
dFragments.add(model);
model = new DrawerModel();
model.setDrawerTitle(getString(R.string.profile_tab_title));
model.setFragment(new ProfileFragment());
dFragments.add(model);
Bundle bundle = new Bundle();
bundle.putSerializable("fragments", dFragments);
fragment = new DrawerFragment();
fragment.setArguments(bundle);
navigateFragment(fragment);
DrawerFragment
public class DrawerFragment extends Fragment {
private FragmentAdapter pagerAdapter;
private ViewPager mViewPager;
private ArrayList<DrawerModel> fList;
@SuppressWarnings("unchecked")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
fList = (ArrayList<DrawerModel>) getArguments().getSerializable(
"fragments");
View v = inflater.inflate(R.layout.swipe_tabs, container, false);
pagerAdapter = new FragmentAdapter(getChildFragmentManager(), fList);
mViewPager = (ViewPager) v.findViewById(R.id.pager);
mViewPager.setAdapter(pagerAdapter);
return v;
}
}
FragmentAdapter
public class FragmentAdapter extends FragmentStatePagerAdapter {
private int TOTAL_TABS = 0;
private ArrayList<DrawerModel> fragmentList;
private SparseArray<Fragment> mPageReference = new SparseArray<Fragment>();
public FragmentAdapter(FragmentManager fm, ArrayList<DrawerModel> fList) {
super(fm);
// TODO Auto-generated constructor stub
TOTAL_TABS = fList.size();
fragmentList = fList;
}
@Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
return fragmentList.get(position).getDrawerFragment();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return TOTAL_TABS;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
Fragment fragment = (Fragment) super.instantiateItem(container,
position);
mPageReference.put(position, fragment);
Log.d("FragmentAdapter - OnCreate", mPageReference.size() + "");
return fragment;
}
@Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return fragmentList.get(position).getDrawerTitle();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
mPageReference.remove(position);
Log.d("FragmentAdapter - onDestroy", mPageReference.size() + "");
super.destroyItem(container, position, object);
}
public Fragment getFragment(int key) {
return mPageReference.get(key);
}
}
Logged Fragment
public class LoggedFragment extends Fragment implements OnClickListener {
TextView tHeader, tEmail, tvPhone;
SharedPreferences sp;
ImageView ivAds;
String bannerUrl = null;
private Handler handler = new Handler();
private static final String TAG = "LoggedFragment";
private void addListeners() {
// TODO Auto-generated method stub
ivAds.setOnClickListener(this);
}
Runnable updateImage = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
if (Utility.checkConnectivity(getActivity()))
new ImageTask((Home_Activity) getActivity(), "Logged")
.execute();
handler.postDelayed(this, Utility.AD_DURATION);
}
};
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
if (handler != null)
handler.post(updateImage);
}
@Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
if (handler != null)
handler.removeCallbacks(updateImage);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.logged_in, container, false);
bindComponents(v);
addListeners();
return v;
}
private void bindComponents(View v) {
// TODO Auto-generated method stub
ivAds = (ImageView) v.findViewById(R.id.ivLoggedAds);
}
public void updateBanner(Bitmap bitmap, String url) {
ivAds.setImageBitmap(bitmap);
bannerUrl = url;
}
}
Call to update fragment method
Fragment fragment = activity.getSupportFragmentManager()
.findFragmentById(R.id.content_frame)
.getTargetFragment();
if (fragment instanceof LoggedFragment) {
LoggedFragment logged = (LoggedFragment) fragment;
logged.updateBanner(result, parameter[1]);
} else
Log.d(TAG, "No Logged Fragment Found");
现在在记录和我的个人资料片段中我需要动态获取一些数据,因为用户可以看到片段,但无法这样做吗?
非常感谢任何想法/建议。
答案 0 :(得分:1)
使用下面的代码找到你的片段,
FragmentAdapter pa = (FragmentAdapter)viewPager.getAdapter();
pa.getFragment(viewPager.getCurrentItem()).updateBanner();
代码选择当前可见片段,您可以通过
将其更改为其他片段 pa.getFragment(index of other fragment).updateBanner();
您必须将pa.getFragment(viewPager.getCurrentItem())
投射到您想要的片段,例如,如果在该位置记录它,请将其更改为:
((LoggedFragment)pa.getFragment(viewPager.getCurrentItem()) )
答案 1 :(得分:0)
我会说你有两个不同的选择。
选项一:使用setTargetFragment()
和getTargetFragment()
,以便获得所需片段的引用。
选项二:使用FragmentManager
只需从FragmentManager获取所需的片段,然后调用您在片段中定义的方法。