问题描述
我使用这个库在我的应用程序:并创建了一个卡如图所示的例子。
现在,我需要处理一个click事件,但是实例化下面时,我收到一个错误:新Card.OnCardClickListener
,它说 OnCardClickListener是抽象的:不能被实例化
。
在我的活动我有这个code:
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?
修改
如果我用这种方式:
cardView.setOnClickListener(new Card.OnCardClickListener(){//error here
@Override
public void onClick(Card card, View view) {
// This will execute when the the card is clicked
}
});
它说无法解析方法setOnClickListener(it.gmariotti.cardslib.library.internal.Card.OnCardClickListener)
EDIT1
你们是对的,我设置了 setOnClickListener
在卡上,而且也没有错误了,
但仍没有执行任何动作点击该卡时。这是新的code:
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();
}
});
我进行了调试,但的onClick
方法不会被调用。怎么会呢?
I performed a debug, but the onClick
method is never called. How come?
推荐答案
Card.OnCardClickListener是一个接口。必须创建定义了接口中的功能如何被使用的类。试试这个:
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
}
});
来源为答案:的
在Java接口的信息: Java接口
Information on Java interfaces: Java Interfaces?
修改
此外,删除的android:点击=真正的
通过该卡包含的RelativeLayout的。它偷点击本身。
Furthermore, remove android:clickable="true"
from the RelativeLayout contained by the card. It's stealing the click for itself.
这篇关于Cardslib的CardView OnCardClickListener的onClick方法不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!