我正在我的应用程序中使用此库:Card Library : Guide并创建了一张卡,如示例所示。
现在我需要处理一个click事件,但在实例化以下内容时收到一个错误:new Card.OnCardClickListener,它表示OnCardClickListener is abstract: cannot be instantiated
在我的活动中,我有以下代码:

    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

怎么了?如何将卡设置为可点击,然后处理点击/触摸事件?
编辑
如果我这样使用它:
cardView.setOnClickListener(new Card.OnCardClickListener(){//error here
        @Override
        public void onClick(Card card, View view) {
            // This will execute when the the card is clicked
        }
    });

它说cannot resolve method setOnClickListener(it.gmariotti.cardslib.library.internal.Card.OnCardClickListener)
编辑1
你们说得对,我在卡片上设置了setOnClickListener,现在没有错误了,
但当卡被点击时,它仍然不执行任何操作。这就是新代码:
     //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();
        }
    });

我执行了调试,但从未调用过onClick方法。怎么会?

最佳答案

Card.OnCardClickListener是一个接口。必须创建定义如何使用接口中的函数的类。试试这个:

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

答案来源:https://github.com/gabrielemariotti/cardslib/blob/master/doc/CARD.md#clickable-card
Java接口信息:Java Interfaces?
编辑
此外,从卡包含的相对布局中删除android:clickable="true"。它在为自己窃取点击。

08-16 05:01