2016-07-29 4 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

、あなたも、タグを変更する必要があり、そうでない場合、タグは同じままです。

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

あなたはボタンのためにこれを実装することができますが、イベントをクリック:onFavClickオン

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 
} 
}); 
関連する問題