gabrielemariotti / cardslib

Android Library to build a UI Card
4.66k stars 1.19k forks source link

Dynamically add cards with custom content #310

Open ben04 opened 9 years ago

ben04 commented 9 years ago

Hey,

I'd like to develop an app, where cards get dynamically added, according to the user input on the activity before the one with the cards. Each card should have the same (custom) layout with 3 textviews and 3 buttons. Though, the textviews of each card need to have different content.

Moreover it has to be possible to click the three buttons and get a reaction according to the content of the textviews, so I have to get the ID of the card of the button clicked.

I've been skimming your doc for a few days now and have tried a few simple things like adding new cards with custom layouts and so on. Though I'm not sure if I can achieve my goals with this library and, if so, how to preferably do that.

I guess I should take the CardListView? Shall I take a custom layout (card:list_card_layout_resourceID="@layout/customcard_layout") or setupInnerViewElements? The perfect solution would be kind of extra methods like your .setTitle() etc. to fill those textviews. Is that possible? Or is there an even easier way?

It would be great if you could give me kind of a rough roadmap what's fitted best for my requirements :-)

Greetz Ben

gabrielemariotti commented 9 years ago

Currently you can use only a global layout with the CardListView. To be clear you can use only one layout with card:list_card_layout_resourceID="@layout/customcard_layout". I will introduce a new feature with 2.x to allow multiple layouts.

Now, you can use different inner layouts with your card in the list. To be clear it means: Card card = new Card(context, R.my_inner_layout); You can find more info here: https://github.com/gabrielemariotti/cardslib/blob/master/doc/CARDLIST.md#cards-with-different-inner-layouts

However in your case you can also do it:

The perfect solution would be kind of extra methods like your .setTitle() etc... In this case you can extend your card and set ui elements in the setupInnerViewElement using your favorite layout. MyCard card = new MyCard(context, R.layout.favorite_layout);

public class MyCard extends Card{

  public void setSubTitle(String subtitle.....){
      mSubtitle = subtitle
  }

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

            //Retrieve elements
            TextView mSubTitleTv = (TextView) 
                             parent.findViewById(R.id.subtitle);
            mSubTitleTv.setText(mSubtitle);
    }
}
ben04 commented 9 years ago

I want to use one layout, so each card shall have 3 TextViews and 3 Buttons. What I need to do is set the texts of the TextViews and actions of the Buttons individually for every card. However, I managed to add and show cards dynamically, that works perfect now.

I tried to customize my custom card class as you did in your example above, but how do I call these custom functions then? card.setSubTitle("Text") did not work.

gabrielemariotti commented 9 years ago

Post your code

ben04 commented 9 years ago

I found my mistake myself, so that works now too! :-)

Next question: How do I manage the clicks on the three buttons of each card? I'd need either the ID of the card or just the text of the second textview and of course which button was pressed, then I'd know how to handle this..

gabrielemariotti commented 9 years ago

The way is always the same

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

         myButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    mymethod(getId(),textView,,,,);
                }
            });
    }
}
ben04 commented 9 years ago

The result here is an error in Android Studio:

bildschirmfoto 2014-10-06 um 20 38 00

gabrielemariotti commented 9 years ago

You have to use a ClickListener on the button, while you are using the onCardClickListener.

ben04 commented 9 years ago

Thanks a lot, that works now (though you edited your post, didn't you? :D )

gabrielemariotti commented 9 years ago

I edited my answer just 2 minutes after writing it.

ben04 commented 9 years ago

When I add the cards dynamically to my CardListView it seems to generate two shadows for each card. How can I change this?

bildschirmfoto 2014-10-11 um 11 38 18