bharath chandra bharath chandra -3 years ago 87
Android Question

How to dynamically change content in CardView

I'm new to android programming. I have been trying to make Reminder app that takes Time, Date and Description as Inputs.

I want to add a swipe action to the card. Initially, the Card shows time and date, but when the user swipes on the card, the content in the card must change, it should completely show a new layout which has a TextView containing Description in it.

I have searched everywhere, but all of them have given about removing or moving the card in the RecyclerView.

What I want is, how can I dynamically change the content(or layout) in the card with animation using swipe action?

Answer Source

There are a couple options. The easiest would be to have your cardview contain your description but set the visibility to GONE initially, then when the swipe event happens invert the initial state to GONE and the description to VISIBLE. I would create a helper method to do this. As far as capturing the swipe gesture, take a look at this answer.

Swipe to Dismiss for RecyclerView

You will need to be using a recyclerview but this should work if you put your invert view logic in the onSwiped method

EDIT To get the cardview, use the ViewHolder from the recyclerview

@Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
        MyCustomViewHolder vh = (MyCustomViewHolder) viewHolder;
        if(vh.isDescriptionShown()){
            vh.showDetails()
        }else {
            vh.showDescription()
        }
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download