我在ViewPager中有一张正面和背面的卡片列表。默认情况下,我显示正面。当单击正面时,将出现背面。现在我想显示一个按钮,单击该按钮时它将到下一张前台卡。 我可以在InstantiateItem中显示正面和背面,但是单击卡后我无法在MainActiviy中显示“下一步”按钮。
有代码:
public Object instantiateItem(ViewGroup container, int position) {
TextView txtFront, txtBack, txtFrontHeader, txtBackHeader;
final CardView fr,bk;
layoutInflater=LayoutInflater.from(context);
View view=layoutInflater.inflate(R.layout.cards_review_layout,container,false);
String s = context.getString(R.string.box);
txtFrontHeader = (TextView) view.findViewById(R.id.front_header_txt);
txtBackHeader = (TextView) view.findViewById(R.id.back_header_txt);
txtFront = (TextView) view.findViewById(R.id.display_card_front_text);
txtBack = (TextView) view.findViewById(R.id.display_card_back_text);
fr = (CardView) view.findViewById(R.id.card_front);
bk = (CardView) view.findViewById(R.id.card_back);
txtFrontHeader.setText(cardList.get(position).getGroupName() + " " + cardList.get(position).getId());
txtBackHeader.setText(s + " " + boxString(cardList.get(position).getBoxNo()));
txtFront.setText(cardList.get(position).getFront());
txtBack.setText(cardList.get(position).getRear());
fr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fr.animate().rotationX(-90).setDuration(300).setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
fr.setVisibility(View.GONE);
bk.setRotationX(90);
bk.setVisibility(View.VISIBLE);
bk.animate().rotationX(0).setDuration(300).setListener(null);
}
});
}
});
bk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bk.animate().rotationX(-90).setDuration(300).setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
bk.setVisibility(View.GONE);
fr.setRotationX(90);
fr.setVisibility(View.VISIBLE);
fr.animate().rotationX(0).setDuration(300).setListener(null);
}
});
}
});
container.addView(view,0);
return view;
}
和MainActivity
db = new MyDatabaseHelper(getApplicationContext());
cards = db.getAllCards();
adapter=new Adapter(cards,this);
viewPager=(ViewPager) findViewById(R.id.pager_review);
viewPager.setAdapter(adapter);
viewPager.setPadding(0,0,0,0);
btn=(Botton) findViewById(R.id.next_card);
viewPager.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btn.setVisibility(View.VISIBLE);
}
});
为什么ViewPager上的setOnclickListener不起作用?