好的,所以场景是这样的。第一次加载ChildFragments时,我可以轻松拨打customMethod()
或onCreate()
内的onCreateView()
。但是,我想要的是当我更改标签时,我想回忆一下我的ChildFragment customMethod()
我怎样才能做到这一点?
ParentFragment
private void initViewPagerAndTabs() {
mMinHeaderHeight = getResources().getDimensionPixelSize(R.dimen.flexible_space_image_height_search_dynamic);
mHeaderHeight = getResources().getDimensionPixelSize(R.dimen.header_height);
mMinHeaderTranslation = -mMinHeaderHeight + getActionBarHeight();
mHeaderLogo = (SelectableRoundedImageView) searchFragmntInflater.findViewById(R.id.header_logo);
mPagerSlidingTabStrip = (PagerSlidingTabStrip) searchFragmntInflater.findViewById(R.id.tabs);
mViewPager = (ViewPager) searchFragmntInflater.findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(1);
mPagerAdapter = new PagerAdapter(getChildFragmentManager());
mPagerAdapter.setTabHolderScrollingContent(this);
mViewPager.setAdapter(mPagerAdapter);
mPagerSlidingTabStrip.setViewPager(mViewPager);
mPagerSlidingTabStrip.setOnPageChangeListener(this);
mHeader = searchFragmntInflater.findViewById(R.id.header);
mActionBarTitleColor = getResources().getColor(R.color.quinto);
mAlphaForegroundColorSpan = new AlphaForegroundColorSpan(0xffffffff);
mSpannableString = new SpannableString(getString(R.string.app_name));
mAlphaForegroundColorSpan = new AlphaForegroundColorSpan(mActionBarTitleColor);
mPagerSlidingTabStrip = (PagerSlidingTabStrip) searchFragmntInflater.findViewById(R.id.tabs);
mViewPager = (ViewPager) searchFragmntInflater.findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(4);
mPagerAdapter = new PagerAdapter(getChildFragmentManager());
mPagerAdapter.setTabHolderScrollingContent(this);
mViewPager.setAdapter(mPagerAdapter);
mPagerSlidingTabStrip.setViewPager(mViewPager);
mPagerSlidingTabStrip.setOnPageChangeListener(this);
}
@Override
public void onPageScrollStateChanged(int arg0) {
// nothing
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// nothing
}
@Override
public void onPageSelected(int position) {
SparseArrayCompat<ScrollTabHolder> scrollTabHolders = mPagerAdapter.getScrollTabHolders();
ScrollTabHolder currentHolder = scrollTabHolders.valueAt(position);
currentHolder.adjustScroll((int) (mHeader.getHeight() + ViewHelper.getTranslationY(mHeader)));
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount, int pagePosition) {
if (mViewPager.getCurrentItem() == pagePosition) {
int scrollY = getScrollY(view);
ViewHelper.setTranslationY(mHeader, Math.max(-scrollY, mMinHeaderTranslation));
float ratio = clamp(ViewHelper.getTranslationY(mHeader) / mMinHeaderTranslation, 0.0f, 1.0f);
// interpolate(mHeaderLogo, getActionBarIconView(), sSmoothInterpolator.getInterpolation(ratio));
//setTitleAlpha(clamp(5.0F * ratio - 4.0F, 0.0F, 1.0F));
}
}
@Override
public void adjustScroll(int scrollHeight) {
// nothing
}
public int getScrollY(AbsListView view) {
View c = view.getChildAt(0);
if (c == null) {
return 0;
}
int firstVisiblePosition = view.getFirstVisiblePosition();
int top = c.getTop();
int headerHeight = 0;
if (firstVisiblePosition >= 1) {
headerHeight = mHeaderHeight;
}
return -top + firstVisiblePosition * c.getHeight() + headerHeight;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public int getActionBarHeight() {
if (mActionBarHeight != 0) {
return mActionBarHeight;
}
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
getActivity().getTheme().resolveAttribute(android.R.attr.actionBarSize, mTypedValue, true);
} else {
getActivity().getTheme().resolveAttribute(R.attr.actionBarSize, mTypedValue, true);
}
mActionBarHeight = TypedValue.complexToDimensionPixelSize(mTypedValue.data, getResources().getDisplayMetrics());
return mActionBarHeight;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private ImageView getActionBarIconView() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
return (ImageView) getActivity().findViewById(android.R.id.home);
}
return (ImageView) getActivity().findViewById(android.support.v7.appcompat.R.id.home);
}
public class PagerAdapter extends FragmentPagerAdapter {
private final String[] TITLES = {"ChildFragment1", "ChildFragment2", "ChildFragment3"};
private SparseArrayCompat<ScrollTabHolder> mScrollTabHolders;
private ScrollTabHolder mListener;
public PagerAdapter(FragmentManager fm) {
super(fm);
mScrollTabHolders = new SparseArrayCompat<ScrollTabHolder>();
}
@Override
public int getItemPosition(Object object) {
// POSITION_NONE makes it possible to reload the PagerAdapter
return POSITION_NONE;
}
public void setTabHolderScrollingContent(ScrollTabHolder listener) {
mListener = listener;
}
@Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
@Override
public int getCount() {
return TITLES.length;
}
@Override
public Fragment getItem(int position) {
if (position == 0) {
fragment = (ScrollTabHolderFragment) ChildFragment1.newInstance(position, "");
} else if (position == 1) {
fragment = (ScrollTabHolderFragment) ChildFragment2.newInstance(position, "");
} else {
fragment = (ScrollTabHolderFragment) ChildFragment3.newInstance(position, "");
}
mScrollTabHolders.put(position, fragment);
if (mListener != null) {
fragment.setScrollTabHolder(mListener);
}
return fragment;
}
public SparseArrayCompat<ScrollTabHolder> getScrollTabHolders() {
return mScrollTabHolders;
}
}
儿童片段
public static ChildFragment1 newInstance(int position, String query) {
ChildFragment1 f = new ChildFragment1();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
f.setArguments(b);
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPosition = getArguments().getInt(ARG_POSITION);
}
customMethod(){
//This method returns an arraylist data
}
答案 0 :(得分:0)
所以,我遵循了Gatunox的建议,并将我的自定义方法设为静态。现在,我可以通过直接从newinstance()方法调用我的自定义方法来刷新我的片段数据。