2016-01-06 112 views
0

我無法弄清楚爲什麼我的按鈕不可點擊。我有一個gridview中的歌曲列表,當用戶點擊列表中的某個項目時,應用程序將把它們帶到我的SongDetailFragment活動中,其中包含有關該歌曲的更多信息。我有一個星形按鈕,如果數據庫中的某首歌曲我想讓它像這樣btn_star_big_on,並且如果它不在數據庫中,我希望它關閉btn_star_big_off。問題是當我運行我的應用程序時,我無法點擊按鈕。就好像它只是一個靜態圖像,因爲用戶應該能夠打開和關閉它。圖片按鈕不可點擊 - Android

任何人都可以請指教如何解決這個問題?

Here是我得到了我的星號按鈕:

這裏是我的星號按鈕的xml:

<ImageView 
    android:id="@+id/imgFavBtn" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:contentDescription="@string/favourite" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:src="@android:drawable/btn_star_big_off" /> 

這裏是我的詳細信息片段:

public class SongDetailFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>{ 
    private Song song; 
    private static final int CURSOR_LOADER_ID = 0; 
    ImageView imgViewFavButton; 
    Boolean mIsFavourite; 

    public SongDetailFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.song_fragment_detail, container, false); 


     Intent intent = getActivity().getIntent(); 
     if (intent != null && intent.hasExtra("song")) { 


      song = intent.getParcelableExtra("song"); 

      ((TextView) rootView.findViewById(R.id.detail_title_textview)) 
        .setText(song.getTitle()); 

      ((TextView)rootView.findViewById(R.id.detail_overview_textview)) 
        .setText(song.getDescription()); 


      ((TextView)rootView.findViewById(R.id.song_date_textview)) 
        .setText(song.getdate()); 




      ImageView imageView = (ImageView) rootView.findViewById(R.id.song_detail_poster_imageview); 
      Picasso.with(getActivity()).load(song.getPoster()).into(imageView); 
     } 

     imgViewFavButton = (ImageView) rootView.findViewById(R.id.imgFavBtn); 
     checkFavourites(song); 
     imgViewFavButton.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 

       Cursor c = 
         getActivity().getContentResolver().query(songContract.songEntry.CONTENT_URI, 
           new String[]{songContract.songEntry._ID}, 
           null, 
           null, 
           null); 

        insertData(song); 

       getLoaderManager().initLoader(CURSOR_LOADER_ID, null, songDetailFragment.this); 

      } 
     }); 

     return rootView; 
    } 


    public void insertData(song song){ 
     ContentValues songValues = new ContentValues(); 
     songValues.put(songContract.songEntry.COLUMN_ID, song.getsong_id()); 
     songValues.put(songContract.songEntry.COLUMN_IMAGE, song.getPoster()); 
     songValues.put(songContract.songEntry.COLUMN_TITLE, song.getTitle()); 
     songValues.put(songContract.songEntry.COLUMN_OVERVIEW, song.getDescription()); 
     songValues.put(songContract.songEntry.COLUMN_date, song.getdate()); 



     getActivity().getContentResolver().insert(songContract.songEntry.CONTENT_URI, 
       songValues); 

    } 


    private void checkFavourites(song song) { 
     Cursor c = 
       getActivity().getContentResolver().query(songContract.songEntry.CONTENT_URI, 
         null, 
         songContract.songEntry.COLUMN_ID + " = ?", 
         new String[]{song.getsong_id()}, 
         null); 

     if (c != null) { 
      c.moveToFirst(); 
      int index = c.getColumnIndex(songContract.songEntry.COLUMN_ID); 

      if (c.getCount() > 0 && c.getString(index).equals(song.getsong_id())) { 
       mIsFavourite = true; 
       imgViewFavButton.setImageResource(android.R.drawable.btn_star_big_on); 
      } 

      else{ 
       imgViewFavButton.setImageResource(android.R.drawable.btn_star_big_off); 
      } 
       c.close(); 

      } 



    } 


    @Override 
    public Loader<Cursor> onCreateLoader(int id, Bundle args){ 
     return new CursorLoader(getActivity(), 
       songContract.songEntry.CONTENT_URI, 
       null, 
       null, 
       null, 
       null); 
    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState){ 
     super.onViewCreated(view, savedInstanceState); 
    } 


    // Set the cursor in our CursorAdapter once the Cursor is loaded 
    @Override 
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 




    } 

    // reset CursorAdapter on Loader Reset 
    @Override 
    public void onLoaderReset(Loader<Cursor> loader){ 

    } 

} 

回答

0

你必須實現一個selectorbtn_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/btn_star_big_on" android:state_selected="true"/> 
    <item android:drawable="@drawable/btn_star_big_off" android:state_selected="false"/> 
</selector> 

,並設置btn_selector爲您ImageView按鈕的背景壁紙

,並添加一行到onClickListener ..

imgViewFavButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     imgViewFavButton.setSelected(!imgViewFavButton.isSelected()); 
     . 
     . 
     . 
    } 
}); 
1

看起來你當你想要一個ImageButton時,你正在使用ImageView。或者您可以將android:clickable="true"添加到XML中。