RecycleView中使用ExpandView實現摺疊效果
阿新 • • 發佈:2018-12-11
效果圖:
最近實現業務:點選瞭解更多載入商品,再點摺疊上去,實現方法:item中巢狀expandView
上程式碼:
<com.xxxx.xxx.widget.ExpandView android:id="@+id/expandView" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:visibility="gone" android:layout_marginTop="10px" android:layout_below="@+id/tv_learn_more" android:clickable="true"/>
自定義的ExpandView
public class ExpandView extends FrameLayout { private Animation mExpandAnimation; private Animation mCollapseAnimation; private boolean mIsExpand; public ExpandView(Context context) { this(context,null); // TODO Auto-generated constructor stub } public ExpandView(Context context, AttributeSet attrs) { this(context, attrs,0); // TODO Auto-generated constructor stub } public ExpandView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub initExpandView(); } private void initExpandView() { LayoutInflater.from(getContext()).inflate(R.layout.layout_expand, this, true); mExpandAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.expand); mExpandAnimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationEnd(Animation animation) { setVisibility(View.VISIBLE); } }); mCollapseAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.collapse); mCollapseAnimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationEnd(Animation animation) { setVisibility(View.INVISIBLE); } }); } public void collapse() { if (mIsExpand) { mIsExpand = false; clearAnimation(); startAnimation(mCollapseAnimation); } } public void expand() { if (!mIsExpand) { mIsExpand = true; clearAnimation(); startAnimation(mExpandAnimation); } } public boolean isExpand() { return mIsExpand; } public View setContentView(){ View view = null; view = LayoutInflater.from(getContext()).inflate(R.layout.layout_expand, null); removeAllViews(); addView(view); return view; } }摺疊效果:
R.anim.collapse <set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:duration="120" android:fromXScale="1." android:fromYScale="1." android:pivotX="50%" android:pivotY="0%" android:toXScale="1." android:toYScale="0." /> </set>
R.anim.expand <set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:duration="200" android:fromXScale="1." android:fromYScale=".0" android:pivotX="50%" android:pivotY="0%" android:toXScale="1." android:toYScale="1." /> </set>
初始化:
mExpandView = (ExpandView) itemView.findViewById(R.id.expandView); View view = mExpandView.setContentView(); recyclerView = view.findViewById(R.id.rv_tuijian); tv_dec = view.findViewById(R.id.tv_dec);
使用
viewHolder.tvLearnMore.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(viewHolder.mExpandView.isExpand()){ viewHolder.mExpandView.collapse(); // tvLearnMore.setText("點擊向下展開"); viewHolder. mExpandView.setVisibility(View.GONE); notifyDataSetChanged(); // mImageView.setImageDrawable(getResources().getDrawable(R.drawable.expand)); }else{ viewHolder.mExpandView.expand(); // mExpandView.setVisibility(View.GONE); // tvLearnMore.setText("點擊向上收疊"); // mImageView.setImageDrawable(getResources().getDrawable(R.drawable.collapse)); } } });