RecycleView中使用ExpandView实现折叠效果

效果图:RecycleView中使用ExpandView实现折叠效果_第1张图片

 最近实现业务:点击了解更多加载商品,再点折叠上去,实现方法:item中嵌套expandView

上代码:

自定义的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

    
R.anim.expand

    

初始化:

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));
                        }
                    }
                });

 

你可能感兴趣的:(android,自定义控件)