2017-08-17 23 views

回答

0

添加的依賴在你的build.gradle

buildscript { 
    repositories { 
     jcenter() 
    } 
} 

dependencies { 
    compile 'com.github.aakira:expandable-layout:[email protected]' 
} 

創建XML文件

<com.github.aakira.expandablelayout.ExpandableRelativeLayout 
    android:id="@+id/expandableLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:ael_expanded="false" 
    app:ael_duration="500" 
    app:ael_interpolator="bounce" 
    app:ael_orientation="vertical"> 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="sample" /> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/text" 
     android:text="sample2" /> 
</com.github.aakira.expandablelayout.ExpandableRelativeLayout> 

Java代碼添加

ExpandableLinearLayout expandableLayout 
= (ExpandableLinearLayout) findViewById(R.id.expandableLayout); 

child.setText("Sets text from a server"); 
expandableLayout.initLayout(); // Recalculate size of children 

// recycler view 
// you must set a ViewHolder#setIsRecyclable(false) and ExpandableLinearLayout#setInRecyclerView(true) 

@Override 
public void onBindViewHolder(final ViewHolder holder, final int position) { 
    holder.setIsRecyclable(false); 
    holder.expandableLinearLayout.setInRecyclerView(true); 
} 
0

只需使用此方法爲您的子視圖。

private void animateView(final View target, final int type) { 
     Animation anim = new ExpandCollapseAnimation(target,type); 
     anim.setDuration(getAnimationDuration()); 
     anim.setAnimationListener(new AnimationListener() { 

     @Override 
     public void onAnimationStart(Animation animation) {} 

     @Override 
     public void onAnimationRepeat(Animation animation) {} 

     @Override 
     public void onAnimationEnd(Animation animation) { 
      if (type == ExpandCollapseAnimation.EXPAND) { 
       if (parent instanceof ListView) { 
        ListView listView = (ListView) parent; 
        int movement = target.getBottom(); 

        Rect r = new Rect(); 
        boolean visible = target.getGlobalVisibleRect(r); 
        Rect r2 = new Rect(); 
        listView.getGlobalVisibleRect(r2); 

        if (!visible) { 
         listView.smoothScrollBy(movement, getAnimationDuration()); 
        } else { 
         if (r2.bottom == r.bottom) { 
          listView.smoothScrollBy(movement,getAnimationDuration()); 
         } 
         } 
       } 
      } 
     } 
    }); 
    target.startAnimation(anim); 
}