recyclerView item 折叠

这里写图片描述

recyclerView item 实现折叠效果

Activity:

package eebochina.com.testtechniques.recyclerViewCollapse;

import android.animation.ValueAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import eebochina.com.testtechniques.R;

public class CollapseRecyclerActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private List<HashMap<String, Boolean>> mDatas;
    private static final String EXPAND_KEY = "is_expand";
    private int customViewHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collapse_recycler);
        getData();
        mRecyclerView = (RecyclerView) findViewById(R.id.collapse_recycler);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setAdapter(new CollapseAdapter());
        //对应 item布局里面声明的高度
        customViewHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());
    }

    private void getData() {
        mDatas = new ArrayList<>();
        HashMap<String, Boolean> hashMap;
        for (int i = 0; i < 30; i++) {
            hashMap = new HashMap<>();
            hashMap.put(EXPAND_KEY, false);
            mDatas.add(hashMap);
        }
    }

    private class CollapseAdapter extends RecyclerView.Adapter {
        @Override

        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new CollapseViewHolder(getLayoutInflater().inflate(R.layout.item_collapse, parent, false));
        }

        @Override
        public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
            final HashMap<String, Boolean> data = mDatas.get(position);
            final CollapseViewHolder collapseViewHolder = (CollapseViewHolder) holder;
            final boolean tempExpand = data.get(EXPAND_KEY);
            if (!tempExpand && mOriginHeight != 0 && holder.itemView.getHeight() > mOriginHeight) {
                collapseViewHolder.customView.setVisibility(View.GONE);
                collapseViewHolder.itemView.getLayoutParams().height = mOriginHeight;
                collapseViewHolder.itemView.requestLayout();
            } else if (tempExpand) {
                collapseViewHolder.customView.setVisibility(View.VISIBLE);
                collapseViewHolder.itemView.getLayoutParams().height = mOriginHeight + customViewHeight;
                collapseViewHolder.itemView.requestLayout();
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean isExpand = data.get(EXPAND_KEY);
                    startAnimator(collapseViewHolder.itemView, isExpand, collapseViewHolder.customView, customViewHeight);
                    data.put(EXPAND_KEY, !isExpand);
                }
            });

        }

        @Override
        public int getItemCount() {
            return mDatas.size();
        }
    }

    private int mOriginHeight;

    private void startAnimator(final View itemView, boolean isExpanded, final View yourCustomView, int customViewHeight) {
        ValueAnimator valueAnimator;
        if (mOriginHeight == 0) {
            mOriginHeight = itemView.getHeight();
        }
        if (!isExpanded) {
            yourCustomView.setVisibility(View.VISIBLE);
            valueAnimator = ValueAnimator.ofInt(mOriginHeight, mOriginHeight + customViewHeight); // These values in this method can be changed to expand however much you like
        } else {
            valueAnimator = ValueAnimator.ofInt(mOriginHeight + customViewHeight, mOriginHeight);

            Animation a = new AlphaAnimation(1.00f, 0.00f); // Fade out

            a.setDuration(200);
            // Set a listener to the animation and configure onAnimationEnd
            a.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    yourCustomView.setVisibility(View.INVISIBLE);
                    yourCustomView.setEnabled(false);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });

            // Set the animation on the custom view
            yourCustomView.startAnimation(a);
        }
        valueAnimator.setDuration(200);
        valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator animation) {
                Integer value = (Integer) animation.getAnimatedValue();
                itemView.getLayoutParams().height = value.intValue();
                itemView.requestLayout();
            }
        });
        valueAnimator.start();
    }

    private static class CollapseViewHolder extends RecyclerView.ViewHolder {
        View customView;

        public CollapseViewHolder(View itemView) {
            super(itemView);
            customView = itemView.findViewById(R.id.item_collapse_custom);
            customView.setVisibility(View.GONE);
        }
    }
}

item_collapse.layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"
    >

    <TextView
        android:id="@+id/item_collapse_text"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:gravity="center"
        android:text="origin view"
        android:background="@color/company_label"
        />

    <TextView
        android:id="@+id/item_collapse_custom"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="Custom View"
        android:background="@color/bg_blue"
        />

</LinearLayout>

参考项目:
https://gist.github.com/ZkHaider/9bf0e1d7b8a2736fd676

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值