2015-12-16 41 views
0

我用這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
  • 的LinearLayout(RelativeLayout的,沒有onMeasure方法的擴展)(定義在XML中)
  • ExpandableView(RelativeLayout的擴展,無onMeasure方法)
  • 我的自定義組件(WeekTimes,LinearLayout的擴展,n o 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到ex根據孩子的身高傾向嗎?這是創建onMeasure()方法的問題嗎?

+0

請發佈代碼,您嘗試過! –

+0

已發佈,但在創建onMeasure()方法時父級ExpandableView組件自動調整大小,動態調整了子級高度。 – pellyadolfo

回答

0

嗯,問題是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); 
    } 
});