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>