版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaoshuxgh/article/details/84940193
先上效果图:
这是一个RecycleView列表,点击了解更多展开显示主营业务及商品列表,再次点击了解更多收起。
实现起来其实就是item列表中嵌套一个ExpandView默认隐藏,点击了解更多的时候显示,再点击是隐藏,实现起来其实挺简单的,在这块主要是自定义了一个ExpanView,下面直接上代码:
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; } }
<com.xxxx.app.widget.ExpandView android:id="@+id/expandView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/tv_learn_more" android:layout_marginTop="10px" android:background="@color/white" android:clickable="true" android:visibility="gone" />
展开动画:
<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>
收起动画:
<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>
初始化:
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{ initData(mListAccount.get(position).getID(),viewHolder.recyclerView,viewHolder.tv_dec,viewHolder.mExpandView); // viewHolder.mExpandView.expand(); // tvLearnMore.setText("点击向上收叠"); // mImageView.setImageDrawable(getResources().getDrawable(R.drawable.collapse)); } } });
实现的也无需求是:点击展开的时候加载数据。
到此完成。