gabrielemariotti / cardslib

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

Good day! #548

Open jerome-savellano opened 8 years ago

jerome-savellano commented 8 years ago

I made a custom card and I can't add a onClickListener to the button inside the card. What am I doing wrong?

`public class CardLearn extends Card {

protected Button btn;
protected TextView tvLorem;

public CardLearn(Context context) {
    super(context, R.layout.native_recyclerview_card_layout);
    init();

}

public CardLearn(Context context, int innerLayout){
    super(context, innerLayout);
    init();
}
private void init(){

    //No Header

    //Set a OnClickListener listener
    setOnClickListener(new OnCardClickListener() {
        @Override
        public void onClick(Card card, View view) {
            Toast.makeText(getContext(), "WOWOWEE", Toast.LENGTH_LONG).show();
        }
    });
}

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

    btn = (Button) parent.findViewById(R.id.button);
    tvLorem = (TextView) parent.findViewById(R.id.lorem);
    tvLorem.setText("WOWOWEE WOWOWEE WOWOWEE WOWOWEE");

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getContext(), "WOWOWEE", Toast.LENGTH_SHORT).show();
        }
    });

}

}`

Here is the fragment where this card is attached to.

`public class Learn extends Fragment {

public Learn(){
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    final View rootView = inflater.inflate(R.layout.fragment_learn, container, false);

    ArrayList<Card> cards = new ArrayList<Card>();

    CardHeader header = new CardHeader(getActivity());

    for(int i = 0; i<10; i++){
        Card card = new CardLearn(getActivity());
        card.addCardHeader(header);
        cards.add(card);
    }

    CardArrayRecyclerViewAdapter mCardArrayAdapter = new CardArrayRecyclerViewAdapter(getActivity(), cards);

    //Staggered grid view
    CardRecyclerView mRecyclerView = (CardRecyclerView) rootView.findViewById(R.id.carddemo_recyclerview);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    //Set Empty view
    if(mRecyclerView != null){
        mRecyclerView.setAdapter(mCardArrayAdapter);
    }

    return rootView;
}

}`

And here is the XML file of the card

`<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="500dp" android:padding="10dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="2">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="4"
        android:orientation="vertical"
        android:background="@drawable/overpop">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="3">
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:padding="5dp"
            android:background="#90000000">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Over population"
                android:id="@+id/list_learn_textview"
                android:layout_centerVertical="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:textColor="#ccc"
                android:paddingLeft="10dp"
                android:typeface="normal"
                android:visibility="visible" />
        </RelativeLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:weightSum="4">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="3"
            android:padding="10dp">

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="@string/lorem_ipsum"
                android:ellipsize="end"
                android:id="@+id/lorem"/>

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:padding="10dp">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="LEARN MORE"
                android:id="@+id/button"
                android:layout_centerVertical="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:background="@android:color/transparent"/>
        </RelativeLayout>

    </LinearLayout>

</LinearLayout>

`

Thank you and God bless!