Skip to content

Instantly share code, notes, and snippets.

@zizibaloob
Created March 1, 2018 18:14
Show Gist options
  • Save zizibaloob/c9dab8ebf02aa02664c7a80d3af6036b to your computer and use it in GitHub Desktop.
Save zizibaloob/c9dab8ebf02aa02664c7a80d3af6036b to your computer and use it in GitHub Desktop.
Collapsing cardview example
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/card"
android:animateLayoutChanges="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp">
<android.support.constraint.ConstraintLayout
android:id="@+id/contents"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:padding="16dp">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
tools:text="Notification #1"/>
<ImageView
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="#000"
app:layout_constraintTop_toTopOf="@+id/title"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="@+id/title"
app:srcCompat="@drawable/ic_arrow_back_24dp"/>
<TextView
android:id="@+id/body"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/lorem_ipsum"
app:layout_constraintTop_toBottomOf="@+id/title"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<View
android:id="@+id/divider"
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_marginTop="16dp"
android:background="@color/divider"
app:layout_constraintTop_toBottomOf="@+id/body"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<TextView
android:id="@+id/footer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Some footer"
app:layout_constraintTop_toBottomOf="@+id/divider"/>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recycler = findViewById(R.id.recycler);
recycler.setAdapter(new MyAdapter());
}
private static class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.item, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.title.setText("Notification #" + position);
}
@Override
public int getItemCount() {
return 100;
}
}
private static class MyViewHolder extends RecyclerView.ViewHolder {
private final TextView title;
private final View toggle;
private final View body;
public MyViewHolder(View itemView) {
super(itemView);
this.title = itemView.findViewById(R.id.title);
this.toggle = itemView.findViewById(R.id.toggle);
this.body = itemView.findViewById(R.id.body);
toggle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
body.setVisibility(body.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
}
});
CardView card = itemView.findViewById(R.id.card);
card.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
ConstraintLayout contents = itemView.findViewById(R.id.contents);
contents.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
contents.getLayoutTransition().setStartDelay(LayoutTransition.CHANGE_DISAPPEARING, 0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment