请问我如何使我的viewpager包裹它的子项目的高度。我的意思是其中的每个子项,不是最高的子项。我希望它包装到当前关注的每个孩子。我需要帮助,希望它可以理解。
所以我创建了一个自定义类来扩展viewpager。
public class wrapitemviewpager extends ViewPager {
List<Integer> childheight = new ArrayList<>(getChildCount());
int currposition = 0;
public wrapitemviewpager(@NonNull Context context) {
super(context);
onpagelistener();
getLayoutParams();
}
public wrapitemviewpager(@NonNull Context context, @Nullable AttributeSet
attrs) {
super(context, attrs);
onpagelistener();
getLayoutParams();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int mode = MeasureSpec.getMode(heightMeasureSpec);
if (mode == MeasureSpec.UNSPECIFIED || mode == MeasureSpec.AT_MOST) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int height = 0;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
height = h;
childheight.add(i, h);
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
MeasureSpec.EXACTLY);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
requestLayout();
}
private void onpagelistener(){
this.addOnPageChangeListener(new SimpleOnPageChangeListener(){
@Override
public void onPageSelected(int position) {
currposition = position;
ViewGroup.LayoutParams layoutParams =
wrapitemviewpager.this.getLayoutParams();
layoutParams.height = childheight.get(position);
wrapitemviewpager.this.setLayoutParams(layoutParams);
wrapitemviewpager.this.invalidate();
}
});
}
}
这是布局
<RelativeLayout
android:id="@+id/postimageorvideoviewlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.ntrii.Ovallook.customclass.wrapitemviewpager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:descendantFocusability="blocksDescendants"
android:layout_height="wrap_content"
android:overScrollMode="never">
</com.ntrii.Ovallook.customclass.wrapitemviewpager>
<android.support.v7.widget.CardView
android:id="@+id/pinnedshow"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
app:cardCornerRadius="10dp"
android:layout_alignBottom="@id/viewpager"
android:layout_centerHorizontal="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="7.5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginStart="2dp"
android:layout_marginLeft="2dp"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:layout_gravity="center_vertical">
<ImageView
android:id="@+id/broadcastpinnedshowimage"
android:layout_width="15dp"
android:layout_height="15dp"
android:src="@mipmap/fb20160712_13_31_09_saved_picture" />
</android.support.v7.widget.CardView>
<TextView
android:id="@+id/pinnedtexttalk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="5dp"
android:paddingLeft="5dp"
android:paddingTop="2dp"
android:paddingEnd="5dp"
android:paddingRight="5dp"
android:paddingBottom="2dp"
android:text="BC Pinned"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:textColor="@color/colorblack"
android:textSize="13sp"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>