Search code examples
javaandroidlistviewcardslib

Remove a Card Using an Event


I am using cardslib by gabrielemariotti. I am using an expandable ListView Card. I have a custom layout inside it, and on of the views inside the ListView Card is an image view, name remove. I am planning to use the Remove imageview as a trigger to dynamically remove a card from the list.

The problem is, there are two layout involved on a Card, the header and the main content. The Cards by the way are contained inside the ListView. One of the layout, is the remove imageview is located. The code below can remove a Card by clicking it. CardListView

 CardHeader header = new CardHeader(getActivity());

        //Set the header title
        header.setTitle(titleHeader);

        //Set visible the expand/collapse button
        header.setButtonExpandVisible(true);

        header.setOtherButtonClickListener(new CardHeader.OnClickCardHeaderOtherButtonListener() {
            @Override
            public void onButtonItemClick(Card card, View view) {
                Toast.makeText(getActivity(), "Drawable", Toast.LENGTH_SHORT).show();
                if(mCardArrayAdapter!=null){

                    mCardArrayAdapter.remove(card); //It is an example.
                }

            }
        });

The code below will trigger a Toast when the imageview Remove is clicked,

@Override
    public void setupInnerViewElements(ViewGroup parent,View view){



        //Add simple title to header
        if (view!=null){
            TextView mTitleView=(TextView) view.findViewById(R.id.card_header_inner_simple_title);
            if (mTitleView!=null)
                mTitleView.setText(mTitle);

            ImageView img = (ImageView)view.findViewById(R.id.cancel);

            img.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getContext(), "Clicked", Toast.LENGTH_SHORT).show();
                }});

        }

    }

I wanted to combine the two. The library


Solution

  • Cardslib works like first you add all your cards to one cards array and then you initalize your CardArrayAdapter with your cards array.

    It's same as using ListView. So deleting one item is also same as delete from your ListView.

    1) On any event remove the card from cards array.

    2) Call notifyDataSetChanged(); on your CardArrayAdapter object. It will update the Cards list and remove the deleted card from the view as well.