3

I have a card declared in the file cardslib_item_card_view:

<it.gmariotti.cardslib.library.view.CardViewNative     
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:card="http://schemas.android.com/apk/res-auto"
  xmlns:card_view="http://schemas.android.com/apk/res-auto"
  card_view:cardCornerRadius="4dp"
  style="@style/native_recyclerview_card.base"
  android:id="@+id/carddemo"
  android:layout_width="match_parent" android:layout_height="wrap_content">

and set as content view within onCreate() method:

public class CardMariotti extends ActionBarActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cardslib_item_card_view);
        //Create a Card
        Card card = new Card(this);
        CardViewNative cardView = (CardViewNative) this.findViewById(R.id.carddemo);
        cardView.setCard(card);

        card.setOnClickListener(new Card.OnCardClickListener() {
            @Override
            public void onClick(Card card, View view) {
                Toast.makeText(CardMariotti.this, "Clickable card", Toast.LENGTH_LONG).show();
            }
        });
}

Now, I'd like to customize it with my own layout, containing a narrow header and some information, as follows:

<RelativeLayout  android:id="@+id/cardlayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="?android:selectableItemBackground"    
    android:clickable="true">

    <!-- layout containing 3 TextView -->
</RelativeLayout> 

What is the canonic procedure for such a process? I've tried a good deal of adjustments, i.e.:

  • creating a second xml file called cardslib_item_layout.xml and referencing it with the Card's constructor this way: Card card = new Card(this, R.layout.cardslib_item_layout); and then setting the setContentView(R.layout.cardslib_item_card_view)
  • Appending the layout inside the card and then setting the setContentView(R.layout.cardslib_item_card_view).

This way; cardslib_item_card_view:

<it.gmariotti.cardslib.library.view.CardViewNative     
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:card="http://schemas.android.com/apk/res-auto" 
      xmlns:card_view="http://schemas.android.com/apk/res-auto"
      card_view:cardCornerRadius="4dp"
      android:id="@+id/carddemo"
      android:layout_width="match_parent" android:layout_height="wrap_content">

      <RelativeLayout>
          <!-- my layout containing a header and some TextViews -->
      </RelativeLayout>
</it.gmariotti.cardslib.library.view.CardViewNative>

In both tests I experience the following issues:

  • The overall result is completely distorced
  • most importantly, the RelativeLayout is placed ON TOP of the card, making any operation on the card impossible (for example, setting the Card.OnCardClickListener on the card itself won't work since the user will be clicking the RelativeLayout and not the card itself)

attempt 1: enter image description here attempt 2: enter image description here

What is the canonic procedure?

EDIT2: ANSWER

The contribution given by @Msk worked fine for me, although I discovered later that with some minor changes it is also possible to obtain the same results by using the original cardslib's Card class, without resorting to the creation of a new class DeviceCard extending the Card class.

I was able to adjust my layout (header and the rest of the card's layout overlapping with each other, as shown in the screenshots) with just some minor and trivial changes in the cardslib_item_layout.xml file (which I had overlooked before); at the same time I was able to eliminate the phantom padding that is automatically attached to every card, by applying Mariotti's answer to this question.

Community
  • 1
  • 1
Cris
  • 2,002
  • 4
  • 30
  • 51

1 Answers1

2

Try this
You can define your own layout for the cards-lib.

Create your custom XML:

Here is an example custom_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="8dp"
    android:paddingRight="6dp"
    android:paddingTop="7dp"
    android:paddingBottom="7dp"
    android:id="@+id/parentView">


    <TextView
        android:id="@+id/name"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="27"
        android:paddingRight="8dp"
        android:paddingLeft="8dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:text=""
        android:layout_gravity="center"
        android:editable="false"
       />

</LinearLayout>

In your JAVA code create a class for the custom card that you wish to use:

    import it.gmariotti.cardslib.library.internal.Card;
    import it.gmariotti.cardslib.library.internal.ViewToClickToExpand;

public class DeviceCard extends Card {

        private String IP;
        private String MAC;
        private String name;
        private Boolean reachable;
        private Boolean editable;
        Boolean markedFlag;

        public DeviceCard(Context context) {
            this(context, R.layout.custom_layout);
        }

        public DeviceCard(Context context,String param1,...,Type paramn) {

            this(context, R.layout.device_card);
            this.name = param1;
        }



        public DeviceCard(Context context, int innerLayout) {
            super(context, innerLayout);
            init();
            Log.d("myTag", "Init called");
        }


        private void init(){


        }

        @Override
        public void setupInnerViewElements(ViewGroup parent, final View view)          {
            Log.i("myTag","setupInnerView");

            final TextView nameBox = (TextView)view.findViewById(R.id.name);
          //edit name if required

        }
    }

Now in your JAVA code, when you need to use the card-list:

DeviceCard card = new DeviceCard(this, name);

This method has always worked for me

Elye
  • 53,639
  • 54
  • 212
  • 474
Msk
  • 857
  • 9
  • 16
  • I don't know exactly, what happened is that the layout looks just as like my first screenshot, and the RelativeLayout is squeezed in a smaller rectangle! I believe to have done as you told me, but probably I have somewhat wronged your example? – Cris May 01 '15 at 16:58
  • 1
    you do not need to do things you had done before. Remove them completely and check once – Msk May 02 '15 at 06:05
  • it's working after adjusting a bit my inner layout (=the overall structure remains consistent); but there's something more! the inner layout seems to never align with the CardRecyclerView card's layout. That means that it shows as if I add some padding in all directions, but I didn't! Do you know why? I can post more details if you need them! – Cris May 02 '15 at 12:00
  • Yesits because the base card layoit defined has some padding. You might be primarily getting more padidng at the bottom? Right. Just post a new image of what youa re getting now. Maybe i can be of more help;-) – Msk May 02 '15 at 12:36
  • I've found a solution to this last problem with hidden padding, by overriding the dimensions' values as described here: http://stackoverflow.com/questions/29134528/cardslib-unwanted-12dp-margin-on-all-cards Thanks for your help. – Cris May 02 '15 at 14:47