2016-07-29 84 views
0
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); 
    } 
} 

所以基本上我的代碼是應該做的:如果單擊該按鈕,這是一個最喜歡的,它會從數據庫中刪除(仍需寫,但不相關) - >圖標的變化。反之亦然。但是,如果有人想改變他們的想法,顯然它必須在不改變頁面的情況下撤消。但是,我的代碼沒有這樣做,只能點擊一次按鈕,第二次不會執行任何操作(雙擊第一次點擊)。

我該如何解決這個問題?

回答

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); } 
    } } 
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 
} 
});