2017-07-10 42 views
0

我正在致力于在CardView上展开和收缩属性。CardView的可扩展高度

public class SimpleCardView extends CardView { 
private int animationDuration; 


    public SimpleCardView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    public SimpleCardView(Context context) { 
     super(context); 
    } 

    public SimpleCardView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public void expand(){ 
     final int initialHeight = getHeight(); 
     final float scale = getContext().getResources().getDisplayMetrics().density; 
     int targetHeight = (int) (232 * scale + 0.5f); 
     final int distance_to_expand = targetHeight - initialHeight; 
     Animation animation = new Animation() { 
      @Override 
      protected void applyTransformation(float interpolatedTime, Transformation t) { 
       getLayoutParams().height = (int) (initialHeight +(distance_to_expand*interpolatedTime)); 
       requestLayout(); 
      } 

      @Override 
      public boolean willChangeBounds() { 
       return true; 
      } 
     }; 
     animationDuration = distance_to_expand; 
     animation.setDuration((long)distance_to_expand); 
     startAnimation(animation); 
    } 

    public int getAnimationTime(){ 
     return animationDuration; 
    } 

    public void collapse(){} 

} 

这是我的截图:

enter image description here

我设定目标高度的恒定值。

int targetHeight = (int) (232 * scale + 0.5f); 

这里,targetHeightCardView一个膨胀高度。 因此,当内容太长时,只显示少部分内容。

有什么办法来动态设置高度不是一个常数值吗?

+0

简单的解决方案兄弟。设置CardView高度wrap_content setYour标题当你做和下面的TextView内容可见性应该消失。所以当你点击展开只是试图看到包含内容的TextView。 –

+0

让视图可见性可见/消失是个好主意。现在我不这样做。那么,有什么办法可以动态地达到目标高度。 –

回答

0

你有没有试过用:WRAP_CONTENT

android:layout_height="wrap_content" 

<android.support.v7.widget.CardView 
android:id="@+id/cardView" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"> 

而且你需要删除静态高度窗体类SimpleCardView

**,或者你需要计算身高**

为此,您需要设置文成TextView后的TextView低于线,然后 计算高度进入SimpleCardView通过使任何setter方法。

TextView textview ; 

textveiw.setText("your text"); 
textview.mesure(0,0); 
int height = textview.getMesuredHeight(); 
+0

是的,我已经有了那个。 –

+0

这工作? –

+0

有没有什么办法可以将SimpleCardView类中的高度动态地设置为'WRAP_CONTENT'。 –