ExpandableListView下ViewPager不显示的解决方案咨询

我想要在ExpandableListView下方实现ViewPager,但代码实现后ViewPager无法显示。我也曾尝试在ScrollView下进行测试,结果相同。推测是由于同时存在两个可滚动布局导致的问题。目前我已找到ScrollView与ViewPager搭配的解决方案:添加**android:fillViewport="true"**属性后,ScrollView内的ViewPager可以正常显示。
请问如何在ExpandableListView下实现ViewPager?ExpandableListView没有**android:fillViewport="true"**属性,除了该属性外还有什么替代方案?
相关XML布局代码
需要添加ViewPager的布局:
<LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="37" android:background="@color/whiteWithAlpha75"> <ExpandableListView android:minWidth="25px" android:minHeight="25px" android:childDivider="@drawable/childSeparator" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="1dp" android:id="@+id/expandableListView" /> </LinearLayout>
ExpandableListAdapter代码
using System; using System.Collections.Generic; using Android.App; using Android.Views; using Android.Widget; using Android.Graphics; using ENTITIES; using System.Linq; using Android.Support.V4.Content; using Android.Support.V4.View; namespace H2POS { class CategoryItemExpandableAdapter : BaseExpandableListAdapter { private Activity activity; private List<ENT_DataEntries> categoryList; List<ViewPager> viewPagerList; public CategoryItemExpandableAdapter(Activity activity, List<ENT_DataEntries> categoryList, List<ViewPager> viewPagerList) : base() { this.activity = activity; this.categoryList = categoryList; this.viewPagerList = viewPagerList; } public override int GroupCount { get { return categoryList.Count; } } public override bool HasStableIds { get { return true; } } public override Java.Lang.Object GetChild(int groupPosition, int childPosition) { return childPosition; } public override long GetChildId(int groupPosition, int childPosition) { return childPosition; } public override int GetChildrenCount(int groupPosition) { return 1; } public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent) { try { var row = convertView; if (row == null) { row = LayoutInflater.From(activity).Inflate(Resource.Layout.Layout_LinearVertical, null); LinearLayout llLinearViewPager = row.FindViewById<LinearLayout>(Resource.Id.llLinearViewPager); //测试用,此TextView可以显示 TextView tvSample = new TextView(activity); tvSample.SetHeight(50); tvSample.SetWidth(50); tvSample.Text = "Sample"; llLinearViewPager.AddView(tvSample); //我的ViewPager,无法显示 //我尝试将其附加到其他布局时可以正常显示 if(viewPagerList[groupPosition].Parent == null) llLinearViewPager.AddView(viewPagerList[groupPosition]); } return row; } catch (Exception e) { UIHelper.ToastMessage(activity.BaseContext, "Error on expandable adapter"); } return new View(activity); } public override Java.Lang.Object GetGroup(int groupPosition) { throw new NotImplementedException(); } public override long GetGroupId(int groupPosition) { return groupPosition; } public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent) { View row = convertView; if (row == null) { row = LayoutInflater.From(activity).Inflate(Resource.Layout.Layout_Category, null); } //设置标题文本 TextView categoryHeader = row.FindViewById<TextView>(Resource.Id.tvCategoryName); categoryHeader.Text = categoryList[groupPosition].Description; categoryHeader.SetTypeface(Typeface.Default, TypefaceStyle.Bold); if (isExpanded) categoryHeader.SetBackgroundColor(new Color(ContextCompat.GetColor(activity, Resource.Color.baseColorLightWithAlpha10))); else categoryHeader.SetBackgroundColor(new Color(ContextCompat.GetColor(activity, Resource.Color.whiteWithAlpha10))); return row; } public override bool IsChildSelectable(int groupPosition, int childPosition) { return true; } } }
成功在ScrollView下显示ViewPager的布局
<ScrollView android:id="@+id/scrollItemSelectedArea" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="95" android:paddingBottom="-65dp" android:fillViewport="true" app:layout_behavior="@string/appbar_scrolling_view_behavior" android:background="@color/whiteWithAlpha15"> <LinearLayout android:id="@+id/llItemSelectedArea" android:orientation="vertical" android:layout_width="match_parent" android:layout_hei
针对你的问题,我整理了几个实用的解决方案:
1. 自定义ExpandableListView重写测量逻辑
ExpandableListView默认会忽略可滚动子View的高度计算,导致ViewPager被压缩隐藏。你可以自定义一个类继承它,强制让它正确测量所有子项:
public class CustomExpandableListView : ExpandableListView { public CustomExpandableListView(Context context) : base(context) { } public CustomExpandableListView(Context context, IAttributeSet attrs) : base(context, attrs) { } public CustomExpandableListView(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr) { } protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec) { // 用足够大的高度值让控件完整计算子项高度 int expandSpec = MeasureSpec.MakeMeasureSpec(int.MaxValue >> 2, MeasureSpecMode.AtMost); base.OnMeasure(widthMeasureSpec, expandSpec); } }
之后把布局里的ExpandableListView替换成这个自定义控件,就能让ViewPager获得正确的显示空间了。
2. 给ViewPager明确设置布局参数
动态添加ViewPager时,一定要给它指定布局参数,避免父布局无法分配空间:
if(viewPagerList[groupPosition].Parent == null) { LinearLayout.LayoutParams vpParams = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MatchParent, 200); // 可根据需求设置固定高度或wrap_content viewPagerList[groupPosition].LayoutParameters = vpParams; llLinearViewPager.AddView(viewPagerList[groupPosition]); }
3. 检查ViewPager的初始化完整性
确认viewPagerList里的每个ViewPager都已经设置好Adapter、页面数量等必要配置,空内容的ViewPager也会看起来像是没显示。
4. 调整整体布局结构
如果上面的方法都不适用,你可以考虑把ExpandableListView和ViewPager放在同一个垂直LinearLayout中(而非将ViewPager作为ExpandableListView的子项),不过这需要结合你的业务需求来调整。
内容的提问来源于stack exchange,提问作者jace

