【问题标题】:Expanding parent's height when expanding's child height扩展子级高度时扩展父级高度
【发布时间】:2015-12-16 16:56:41
【问题描述】:

我正在使用这个 ExpandableView 组件https://github.com/nicolasjafelle/ExpandableView

有一个主类:https://github.com/nicolasjafelle/ExpandableView/blob/master/ExpandableViewProject/expandableview/src/main/java/com/expandable/view/ExpandableView.java

该组件包装了用户指定的其他自定义组件。当自定义组件具有固定高度时,一切正常。

但是,我正在创建一个具有动态行数的自定义组件。假设这是初始状态:

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


【解决方案1】:

好吧,问题在于 ExpandableView 包含一个动画,而这个动画正在修复高度,从而阻止了 android 布局按预期工作。这里有更详细的描述:

https://github.com/nicolasjafelle/ExpandableView/issues/5

所以修复是在动画结束后将高度重新分配给一个相对值。通过在动画方法的 ExpandableView 扩展上添加这些新行来解决问题:

animator.addListener(new AnimatorListenerAdapter() {
    @Override
    public void onAnimationEnd(Animator animation) {
        // release height
        ViewGroup.LayoutParams layoutParams = contentLayout.getLayoutParams();
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        contentLayout.setLayoutParams(layoutParams);
    }
});

【讨论】:

    猜你喜欢
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 2020-01-05
    • 2013-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多