我创建了一个viewpager和三个标签。 我有两个问题。
在viewpager和tablayout中启动活动后,在我的适配器的getItem方法中自动选择第一个标签(通常)和第二个标签,并显示第一个标签片段!
当我选择第三个标签时,viewpager会遇到问题。选中第二个选项卡和第三个选项卡,第三个选项卡无法滚动和....我不明白!
其他信息: - 我用这个库滚动了三个片段: com.github.ksoichiro:Android的observablescrollview:1.5.0 - 从ObservableScrollViewCallbacks实现的viewpager和tablayoutT中的Activity 并在每个framgnets中使用此代码在每个片段中的活动父和ObservableScrollView之间进行链接: scrInfo.setScrollViewCallbacks((ObservableScrollViewCallbacks)parentActivity);
创建标签:
AdapterPager adapter = new AdapterPager(getSupportFragmentManager());
adapter.myAddFragment(new FragmentComment());
adapter.myAddFragment(new FragmentProduct());
adapter.myAddFragment(new FragmentInfo());
vpgCampPage.setAdapter(adapter);
tabCampPage.setupWithViewPager(vpgCampPage);
tabCampPage.getTabAt(0).setIcon(R.mipmap.info_message);
tabCampPage.getTabAt(1).setIcon(R.mipmap.info_product);
tabCampPage.getTabAt(2).setIcon(R.mipmap.info_desc);
AdapterPager:
public class AdapterPager extends FragmentPagerAdapter {
private List<Fragment> fragments = new ArrayList<>();
public AdapterPager(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
public void myAddFragment(Fragment fragment) {
fragments.add(fragment);
}
}
第三个片段:
public class FragmentInfo extends Fragment {
private Activity parentActivity;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
parentActivity = getActivity();
View view = inflater.inflate(R.layout.fragment_info, container, false);
ObservableScrollView scrInfo = (ObservableScrollView) view.findViewById(R.id.scrInfo);
scrInfo.setScrollViewCallbacks((ObservableScrollViewCallbacks) parentActivity);
return view;
}
}
活动父级中标题滚动的动画:
@Override
public void onUpOrCancelMotionEvent(ScrollState scrollState) {
if (scrollState == ScrollState.UP) {
if (toolbarIsShown()) { // TODO Not implemented
hideToolbar(); // TODO Not implemented
}
} else if (scrollState == ScrollState.DOWN) {
if (toolbarIsHidden()) { // TODO Not implemented
showToolbar(); // TODO Not implemented
}
}
}
private boolean toolbarIsShown() {
return ViewHelper.getTranslationY(layCampPageHeader) == 0;
}
private boolean toolbarIsHidden() {
return ViewHelper.getTranslationY(layCampPageHeader) - sizeTab == -layCampPageHeader.getHeight();
}
private void showToolbar() {
moveToolbar(0);
}
private void hideToolbar() {
moveToolbar(-layCampPageHeader.getHeight() + sizeTab);
}
private void moveToolbar(float toTranslationY) {
ValueAnimator animator = ValueAnimator.ofFloat(ViewHelper.getTranslationY(layCampPageHeader), toTranslationY).setDuration(300);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float translationY = (float) animation.getAnimatedValue();
ViewHelper.setTranslationY(layCampPageHeader, translationY);
ViewHelper.setTranslationY((View) vpgCampPage, translationY);
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) ((View) vpgCampPage).getLayoutParams();
lp.height = (int) -translationY + /*getScreenHeight()*/1776 - lp.topMargin;
((View) vpgCampPage).requestLayout();
}
});
animator.start();
}