private Bundle detailsbundle = new Bundle();
private onFABCLick mlistener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_details, container, false);
TextView naam = (TextView) view.findViewById(R.id.name);
TextView adres = (TextView) view.findViewById(R.id.adress);
TextView gemeente = (TextView) view.findViewById(R.id.gemeente);
TextView deelgemeente = (TextView) view.findViewById(R.id.deelgemeente);
TextView postcode = (TextView) view.findViewById(R.id.postcode);
final ImageButton favourite = (ImageButton) view.findViewById(R.id.imageButton);
naam.setText(detailsbundle.getString(DETAILS_NAAM));
adres.setText(detailsbundle.getString(DETAILS_ADRES));
gemeente.setText(detailsbundle.getString(DETAILS_GEMEENTE));
deelgemeente.setText(detailsbundle.getString(DETAILS_DEELGEMEENTE));
postcode.setText(detailsbundle.getString(DETAILS_POSTCODE));
boolean isFavourite = detailsbundle.getBoolean(DETAILS_FAVOURITE);
if(isFavourite){
favourite.setImageResource(R.drawable.ic_star_black_18dp);
favourite.setTag(R.drawable.ic_star_black_18dp);
}
else{
favourite.setTag(R.drawable.ic_star_border_black_18dp);
}
favourite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mlistener.onFavClick(favourite);
}
});
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof onFABCLick) {
mlistener = (onFABCLick) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement interface");
}
}
public interface onFABCLick{
public void onFavClick(ImageButton favourite);
}
在我的活动:的ImageButton多次点击
public void onFavClick(ImageButton favourite) {
if ((Integer)favourite.getTag() == R.drawable.ic_star_border_black_18dp){
favourite.setImageResource(R.drawable.ic_star_black_18dp);
}
else{
favourite.setImageResource(R.drawable.ic_star_border_black_18dp);
}
}
所以基本上我的代码是应该做的:如果单击该按钮,这是一个最喜欢的,它会从数据库中删除(仍需写,但不相关) - >图标的变化。反之亦然。但是,如果有人想改变他们的想法,显然它必须在不改变页面的情况下撤消。但是,我的代码没有这样做,只能点击一次按钮,第二次不会执行任何操作(双击第一次点击)。
我该如何解决这个问题?