AleSpero / ExpandableCardView

Simple expandable CardView for Android.
Apache License 2.0
689 stars 88 forks source link

expand and collapse problem #45

Open JavaDeveloper08 opened 5 years ago

JavaDeveloper08 commented 5 years ago

Hi, I have 2 cardviews. If I click the first card view, I would like to expand this card view and to collapse the second card view. By the same way, if I click the second card view, I would like to expand this card view and to collapse the first card view,

Unfortunately, the card view that I want to collapse is disappearing from the screen. and it is not reachable again. please help me for solving this problem.

card1 = root.findViewById(R.id.x1); card2 = root.findViewById(R.id.x2);

    card1.setOnExpandedListener(new ExpandableCardView.OnExpandedListener() {
        @Override
        public void onExpandChanged(View v, boolean isExpanded) {

            if (isExpanded)
                card2.collapse();
        }
    });

    card2.setOnExpandedListener(new ExpandableCardView.OnExpandedListener() {
        @Override
        public void onExpandChanged(View v, boolean isExpanded) {

            if(isExpanded)
                card1.collapse();

        }
    });
princeivankent commented 4 years ago

I have the same problem. Is this issue solved?

JavaDeveloper08 commented 4 years ago

Hi @princeivankent, I solved this problem. You need to expand cardviews by giving a delay at first. I am adding code below.

private void cardViewListeners() {

    try {

        new Handler().postDelayed(() -> binding.cardView1.expand(), 100);

        new Handler().postDelayed(() -> binding.cardView2.expand(), 100);

        binding.cardView1.setOnExpandedListener((v, isExpanded) -> {

            if (isExpanded) {
                new Handler().postDelayed(() -> {
                    binding.cardView2.collapse();
                }, 100);
            }
        });

        binding.cardView2.setOnExpandedListener((v, isExpanded) -> {

            if (isExpanded) {
                new Handler().postDelayed(() -> {
                    binding.cardView1.collapse();
                }, 30);
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Anilugale commented 3 years ago

handed this, check the card is expanded before the collapse

             if (card2.isExpanded) {
                 card2.collapse()
             }`
shahzad1 commented 3 years ago

I'm having same problem