Skip to content

Commit

Permalink
优化菜单控件,自适应高度
Browse files Browse the repository at this point in the history
  • Loading branch information
“xiaohaibin committed Mar 12, 2019
1 parent 9077ad7 commit 8f441f3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ private void initData() {
}

private void init() {
//动态设置菜单高度,自己根据自身需求来弄
mPageMenuLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) ((float) ScreenUtil.getScreenWidth() / 2.0f)));
mPageMenuLayout.setPageDatas(homeEntrances, new PageMenuViewHolderCreator() {
@Override
public AbstractHolder createHolder(View itemView) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.stx.xhb.pagemenulibrary;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;

/**
* @author: xiaohaibin.
* @time: 2019/3/12
* @mail:[email protected]
* @github:https://github.com/xiaohaibin
* @describe: 自定义viewpager,自适应高度
*/
public class CustomViewPager extends ViewPager {

public CustomViewPager(@NonNull Context context) {
super(context);
}

public CustomViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void onMeasure(int widthMeasureSpec, int 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();
if (h > height) {
height = h;
}
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
public class PageMenuLayout<T> extends RelativeLayout {
private static final int DEFAULT_ROW_COUNT = 2;
private static final int DEFAULT_SPAN_COUNT = 5;
private ViewPager mViewPager;
private CustomViewPager mViewPager;
/**
* 行数
*/
Expand All @@ -52,8 +52,8 @@ public PageMenuLayout(Context context, @Nullable AttributeSet attrs, int defStyl
}

private void initView(Context context, AttributeSet attrs) {
mViewPager = new ViewPager(context);
addView(mViewPager, 0);
mViewPager = new CustomViewPager(context);
addView(mViewPager, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PageMenuLayout);
if (typedArray != null) {
mRowCount = typedArray.getInteger(R.styleable.PageMenuLayout_pagemenu_row_count, DEFAULT_ROW_COUNT);
Expand Down

0 comments on commit 8f441f3

Please sign in to comment.