在布局中添加一层外部布局,为了设置颜色
android:id="@+id/layout"
android:layout_width="match_parent"
android:paddingTop="10dp"
android:layout_height="wrap_content">
android:id="@+id/infoTab"
android:layout_width="match_parent"
android:layout_height="60dp"
app:background="@color/mainWhite"
app:tabTextAppearance="@android:style/TextAppearance.Small"
app:tabGravity="fill"
app:tabIndicatorGravity="top"
app:tabIndicatorColor="@color/colorAccent"
app:tabIndicatorHeight="2dp"
app:tabSelectedTextColor="@color/colorAccent"
app:tabTextColor="@color/itemTextColor"/>
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
因为viewpager是嵌套在ScrollView中的,所以自定义了Viewpager
自定义的viewpager实现了悬停并且重置了高度,在切换的时候高度会动态变化
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import java.util.HashMap;
import java.util.LinkedHashMap;
/**
* 添加悬停的viewpager
*/
public class FinanceViewPagerextends ViewPager {
private int current;
private int height =0;
/**
* 保存position与对于的View
*/
private HashMapmChildrenViews =new LinkedHashMap();
private boolean scrollble =true;
public FinanceViewPager(Context context) {
super(context);
}
public FinanceViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mChildrenViews.size() >current) {
View child =mChildrenViews.get(current);
if (child !=null) {
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
height = child.getMeasuredHeight();
}
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void resetHeight(int current) {
this.current = current;
if (mChildrenViews.size() > current) {
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) getLayoutParams();
if (layoutParams ==null) {
layoutParams =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, height);
}else {
layoutParams.height =height;
}
setLayoutParams(layoutParams);
}
}
/**
* 保存position与对于的View
*/
public void setObjectForPosition(View view, int position) {
mChildrenViews.put(position, view);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (!scrollble) {
return true;
}
return super.onTouchEvent(ev);
}
public boolean isScrollble() {
return scrollble;
}
public void setScrollble(boolean scrollble) {
this.scrollble = scrollble;
}
}
然后就是代码设置
private void initTab(int select) {
tabTitles.clear();
tabTitles.add("1111");
tabTitles.add("2222");
tabFragments.clear();
for (int i =0; i
Bundle bundle =new Bundle();
switch (i) {
case 0:
Fragment1 holdFragment =new Fragment1 (viewPager, 0);
holdFragment.setArguments(bundle);
tabFragments.add(holdFragment);
break;
case 1:
Fragment2 mFragment2=new Fragment2 (viewPager, 2);
mFragment2.setArguments(bundle);
tabFragments.add(mFragment2);
break;
}
}
if (tabAdapter ==null) {
tabAdapter =new FragAdapterUpTitle(getChildFragmentManager(), tabFragments, tabTitles);
}
viewPager.setAdapter(tabAdapter);
infoTab.setupWithViewPager(viewPager);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
viewPager.resetHeight(position);
viewPager.setCurrentItem(position);
index =infoTab.getTabAt(position).getPosition();
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
viewPager.resetHeight(0);
infoTab.setBackgroundColor(SkinCompatResources.getColor(getContext(), R.color.mainWhite));
infoTab.getTabAt(select).select();
ScrollView.setScrollViewListener(new FinanceScrollView.ScrollViewListener() {
@Override
public void onScrollChanged(FinanceScrollView scrollView, int l, int t, int oldl, int oldt) {
if (t >topRL.getHeight() &&infoTab.getParent() ==layout) {
layout.removeView(infoTab);
rl_layout.addView(infoTab);
}else if (t
rl_layout.removeView(infoTab);
layout.addView(infoTab);
}
}
});
}
最后重写一个适配器,主要是为了动态的修改tab的名字