【发布时间】:2015-12-16 16:56:41
【问题描述】:
我正在使用这个 ExpandableView 组件https://github.com/nicolasjafelle/ExpandableView
该组件包装了用户指定的其他自定义组件。当自定义组件具有固定高度时,一切正常。
但是,我正在创建一个具有动态行数的自定义组件。假设这是初始状态:
this is row1
this is row2
this is row3
button
当我点击按钮时,它会显示
this is row1
this is row2
this is row3
this is row4
button
当我在 LinearLayout 中测试我的组件时,没有问题,并且显示了新行。但是,当我在 ExpandableView 中嵌入我的自定义组件时,ExpandableView 的 ContentView 不会扩展以包装新行,而是按钮隐藏在底部。
我想为了让 ExpandableView 的 ContentView 根据内部自定义组件的大小动态扩展,我应该实现 ExpandableView 的 onMeasure() 方法。但到目前为止,我所有的测试都失败了。
我的自定义组件的层次结构是:
- 顶部
- ...
- ExpandableView(RelativeLayout的扩展,没有onMeasure方法)
- LinearLayout(在 XML 中定义)
- ExpandableView(RelativeLayout的扩展,没有onMeasure方法)
- 我的自定义组件(WeekTimes,LinearLayout 的扩展,没有 onMeasure 方法)
有什么想法吗?
我猜这是 ExpandableView 组件中的一个错误,但不知道如何修复它。
更多细节:这是实例化 ExpandableView 的代码:
ExpandableView expandableContract = (ExpandableView) formsContained.findViewById(R.id.expandable_academy);
expandableContract.fillData(R.drawable.button_selected_full, getStringResource(R.string.about_academy), false);
expandableContract.addContentView(getLayoutInflater().inflate(R.layout.activity_offer_form_academy, new ExpandableView(this), true));
其中 R.layout.activity_offer_form_academy 只是我的组件的布局:
<com.dynassets.assets.util.weektimes.WeekTimes
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/weektimes"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
我的自定义组件的代码是
public class WeekTimes extends LinearLayout {
public WeekTimes(Context context) {
super(context);
init();
}
public WeekTimes(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
setOrientation(LinearLayout.VERTICAL);
Button button = new Button(getContext());
button.setText("click me");
addView(button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
try {
Button button = new Button(getContext());
button.setText("click me2");
addView(button);
requestLayout();
} catch (Exception e) {
Log.e("MyApp", "exception", e);
}
}
});
}
}
那么,如何告诉父级 ExpandableView 根据子级的高度进行扩展呢?这是创建 onMeasure() 方法的问题吗?
【问题讨论】:
-
请贴出你试过的代码!
-
已发布但在父 ExpandableView 组件上创建 onMeasure() 方法以在孩子的高度动态调整大小时自行调整大小时迷失了方向。
标签: android android-layout expandablelistview