RecycleView中實現摺疊列表--之自定義ExpandView
阿新 • • 發佈:2018-12-11
先上效果圖:
這是一個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)); } } });
實現的也無需求是:點選展開的時候載入資料。
到此完成。