Skip to content

Commit 8f441f3

Browse files
author
“xiaohaibin
committed
优化菜单控件,自适应高度
1 parent 9077ad7 commit 8f441f3

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

app/src/main/java/com/stx/xhb/meituancategorydemo/MainActivity.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ private void initData() {
6868
}
6969

7070
private void init() {
71-
//动态设置菜单高度,自己根据自身需求来弄
72-
mPageMenuLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) ((float) ScreenUtil.getScreenWidth() / 2.0f)));
7371
mPageMenuLayout.setPageDatas(homeEntrances, new PageMenuViewHolderCreator() {
7472
@Override
7573
public AbstractHolder createHolder(View itemView) {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.stx.xhb.pagemenulibrary;
2+
3+
import android.content.Context;
4+
import android.support.annotation.NonNull;
5+
import android.support.annotation.Nullable;
6+
import android.support.v4.view.ViewPager;
7+
import android.util.AttributeSet;
8+
import android.view.View;
9+
10+
/**
11+
* @author: xiaohaibin.
12+
* @time: 2019/3/12
13+
14+
* @github:https://github.com/xiaohaibin
15+
* @describe: 自定义viewpager,自适应高度
16+
*/
17+
public class CustomViewPager extends ViewPager {
18+
19+
public CustomViewPager(@NonNull Context context) {
20+
super(context);
21+
}
22+
23+
public CustomViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
24+
super(context, attrs);
25+
}
26+
27+
@Override
28+
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
29+
int height = 0;
30+
for (int i = 0; i < getChildCount(); i++) {
31+
View child = getChildAt(i);
32+
child.measure(widthMeasureSpec,
33+
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
34+
int h = child.getMeasuredHeight();
35+
if (h > height) {
36+
height = h;
37+
}
38+
}
39+
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
40+
MeasureSpec.EXACTLY);
41+
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
42+
}
43+
}

pagemenulibrary/src/main/java/com/stx/xhb/pagemenulibrary/PageMenuLayout.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
public class PageMenuLayout<T> extends RelativeLayout {
2929
private static final int DEFAULT_ROW_COUNT = 2;
3030
private static final int DEFAULT_SPAN_COUNT = 5;
31-
private ViewPager mViewPager;
31+
private CustomViewPager mViewPager;
3232
/**
3333
* 行数
3434
*/
@@ -52,8 +52,8 @@ public PageMenuLayout(Context context, @Nullable AttributeSet attrs, int defStyl
5252
}
5353

5454
private void initView(Context context, AttributeSet attrs) {
55-
mViewPager = new ViewPager(context);
56-
addView(mViewPager, 0);
55+
mViewPager = new CustomViewPager(context);
56+
addView(mViewPager, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
5757
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PageMenuLayout);
5858
if (typedArray != null) {
5959
mRowCount = typedArray.getInteger(R.styleable.PageMenuLayout_pagemenu_row_count, DEFAULT_ROW_COUNT);

0 commit comments

Comments
 (0)