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);
}
在我的活动中:
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);
}
}
基本上我的代码应该做的是:如果单击按钮,并且它是最喜欢的,它将从数据库中删除(仍然需要写入,但不相关) - >图标更改。反之亦然。但是,如果有人要改变主意,显然必须在不更改页面的情况下撤消。但是,我的代码没有这样做,只能单击按钮一次,第二次它不会执行任何操作(第一次单击双向工作)。
我该如何解决这个问题?
答案 0 :(得分:1)
在onFavClick上,你也必须更改标签,否则标签保持不变。
public void onFavClick(ImageButton favourite) {
if ((Integer)favourite.getTag() == R.drawable.ic_star_border_black_18dp){
favourite.setImageResource(R.drawable.ic_star_black_18dp);
favourite.setTag(R.drawable.ic_star_black_18dp); }
} else{
favourite.setImageResource(R.drawable.ic_star_border_black_18dp);
favourite.setTag(R.drawable.ic_star_border_black_18dp); }
} }
答案 1 :(得分:0)
您可以为按钮点击事件实施此功能:
private int _clicks = 0;
k = (ImageButton)findViewById(R.id.imageButton1);
if(count == 1)
//do whatever
if(count == 2)
//do whatever
if(count == 3)
//do whatever
}
});