RecycleView中使用ExpandView实现折叠效果

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaoshuxgh/article/details/84776884

效果图:

 最近实现业务:点击了解更多加载商品,再点折叠上去,实现方法: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));
                        }
                    }
                });

猜你喜欢

转载自blog.csdn.net/xiaoshuxgh/article/details/84776884
今日推荐