viewpagerは各itemの高さを動的に変更します
15166 ワード
viewpagerはほとんどのプロジェクトで使用されており、ページが複雑な場合があり、各itemの高さを動的に変更する必要があるため、解決する必要があります.ソリューションは次のとおりです.
まずwrapcontentを行うにはviewpagerをカスタマイズしてonmeasureメソッドを書き換える必要があり、そこで処理を行った後、viewpagerの高さを0に再設定すれば解決できます.次のコードを見てください.
レイアウトの定義
変更時に呼び出す
まずwrapcontentを行うにはviewpagerをカスタマイズしてonmeasureメソッドを書き換える必要があり、そこで処理を行った後、viewpagerの高さを0に再設定すれば解決できます.次のコードを見てください.
/**
* Created by longcai:zhangyu on 17/3/14.
*/
public class WrapContentViewPager extends ViewPager {
private int current;
private int height = 0;
/**
* position View
*/
private HashMap, View> mChildrenViews = new LinkedHashMap, View>();
private boolean scrollble = true;
public WrapContentViewPager(Context context) {
super(context);
}
public WrapContentViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mChildrenViews.size() > current) {
View child = mChildrenViews.get(current);
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.MATCH_PARENT, 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;
}
レイアウトの定義
android:id="@+id/vp_01"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
for (int i = 0; i < viewList.size(); i++) {
vp01.setObjectForPosition(viewList.get(i), i);
}
vp01.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
vp01.resetHeight(position);
switch (position) {
case 0:
initTab();
tvinvest09.setSelected(true);
tab_1.setVisibility(View.VISIBLE);
tab_2.setVisibility(View.GONE);
tab_3.setVisibility(View.GONE);
tab_4.setVisibility(View.GONE);
break;
case 1:
initTab();
tvinvest10.setSelected(true);
tab_1.setVisibility(View.GONE);
tab_2.setVisibility(View.VISIBLE);
tab_3.setVisibility(View.GONE);
tab_4.setVisibility(View.GONE);
break;
case 2:
initTab();
tvinvest11.setSelected(true);
tab_1.setVisibility(View.GONE);
tab_2.setVisibility(View.GONE);
tab_3.setVisibility(View.VISIBLE);
tab_4.setVisibility(View.GONE);
break;
case 3:
initTab();
tvinvest12.setSelected(true);
tab_1.setVisibility(View.GONE);
tab_2.setVisibility(View.GONE);
tab_3.setVisibility(View.GONE);
tab_4.setVisibility(View.VISIBLE);
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
vp01.resetHeight(0);
変更時に呼び出す
vp01.setObjectForPosition(viewList.get(i), i);