Cardslib's CardView OnCardClickListener's onClick method not being called

给你一囗甜甜゛ 提交于 2019-12-11 02:08:21

问题


I am using this library in my app: Card Library : Guide and created a card as shown in the example. Now I need to handle a click event, but I receive an error when instantiating the following: new Card.OnCardClickListener, which says OnCardClickListener is abstract: cannot be instantiated. In my Activity I have this code:

    Card card = new Card(getBaseContext());//getContext()
    CardHeader header = new CardHeader(getBaseContext());
    card.addCardHeader(header);
    final CardViewNative cardView = (CardViewNative) findViewById(R.id.carddemo);
    cardView.setCard(card);

    cardView.setClickable(true);
    cardView.setOnClickListener(new Card.OnCardClickListener()); //error here

What's wrong? How can I set my card as clickable and then handle click/touch events?

EDIT

If I use it this way:

cardView.setOnClickListener(new Card.OnCardClickListener(){//error here
        @Override
        public void onClick(Card card, View view) {
            // This will execute when the the card is clicked
        }
    });

it says cannot resolve method setOnClickListener(it.gmariotti.cardslib.library.internal.Card.OnCardClickListener)

EDIT1

You guys are right, I set the setOnClickListener on the card itself, and there's no error anymore, but still it doesn't perform any action when the card is clicked. That's the new code:

     //Create a Card
    Card card = new Card(this);//getContext()

    //Create a CardHeader
    CardHeader header = new CardHeader(this);
    //....
    //Add Header to card
    card.addCardHeader(header);

    //Set card in the cardView
    CardViewNative cardView = (CardViewNative) this.findViewById(R.id.carddemo);
    cardView.setCard(card);

    card.setClickable(true);
    card.setOnClickListener(new Card.OnCardClickListener(){
        @Override
        public void onClick(Card card, View view) {
            // This will execute when the the card is clicked
            Toast.makeText(CardMariotti.this,"Clickable card", Toast.LENGTH_LONG).show();
        }
    });

I performed a debug, but the onClick method is never called. How come?


回答1:


Card.OnCardClickListener is an Interface. You must create the class that defines how the functions within the interface are to be used. Try this:

card.setOnClickListener(new Card.OnCardClickListener() {
    @Override
    public void onClick(Card card, View view) {
        // This will execute when the the card is clicked
    }
});

Source for answer: https://github.com/gabrielemariotti/cardslib/blob/master/doc/CARD.md#clickable-card

Information on Java interfaces: Java Interfaces?

EDIT

Furthermore, remove android:clickable="true" from the RelativeLayout contained by the card. It's stealing the click for itself.




回答2:


I've found the error but I don't know how to handle it. It's in the layout containing the card, cardslib_item_card.xml. If I remove everything that I've added and I just keep this:

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

then the card gets clicked, but if try to personalize it, for example adding a Relative Layout, as such:

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

    <RelativeLayout android:id="@+id/cardlayout"
        android:layout_width="match_parent" android:layout_height="match_parent"
        android:background="?android:selectableItemBackground"              
        android:clickable="true">
    <!-- layout containing 3 TextViews -->
    </RelativeLayout>
</it.gmariotti.cardslib.library.view.CardViewNative>

then the card doesn't get clicked anymore. To be more precise, it gets clicked just sometimes, as if the RelativeLayout is covering the whole card and and when i find a spot where it's not then it detects my finger. It works if I only add an empty card, but how am I supposed to customize it then?

It's like having two views on top of each other




回答3:


You have to define the clickListener on the card (model) instead of CardView (view)

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

At the same time you should use:

   //it is not necessary if you define a ClickListener on the card
   card.setClickable(true);  

Btw, you should also use something like this in an Activity:

Card card = new Card(this);


来源:https://stackoverflow.com/questions/29926073/cardslibs-cardview-oncardclicklisteners-onclick-method-not-being-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!