-1

我正在創建一個音樂應用程序,我有一個「專輯」片段,它在RecyclerView中顯示歌曲的專輯封面。我想要的是,當我點擊任何這些項目(專輯封面)時,我應該轉到另一個包含該特定專輯歌曲的活動(albumsDetails.java)。所有這些歌曲應顯示在RecyclerView。 我知道如何使用意圖,我已經嘗試了很多東西,但都沒有工作。如何點擊recyclerView項目並轉到另一個包含點擊項目詳細信息的recyclerView?

請不要低估我的問題,因爲我對ANDROID STUDIO非常陌生。

Album.java

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.albums_activity, container, false); 

    recyclerViewAlbum = view.findViewById(R.id.albums_reyclerView); 
    recyclerViewAlbum.setHasFixedSize(true); 

    GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),2); 
    recyclerViewAlbum.setLayoutManager(gridLayoutManager); 

    albumsAdapter = new AlbumsAdapter(SongList1,getContext(), new AlbumsAdapter.RecyclerItemClickListener() { 
     @Override 
     public void onClickListener(SongInfoModel song, int position) { 


      Intent i = new Intent(getContext(), AlbumDetails.class); 
      i.putExtra("SongName", song.getSongName()); 
      startActivity(i); 

      Activity activity = getActivity(); 
      if (activity instanceof MainActivity) {} 
     } 
    }); 

    Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
    String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0"; 
    Cursor cursor = getActivity().getContentResolver().query(uri, null, selection, null, null); 
    if (cursor != null) { 
     if (cursor.moveToFirst()) { 
      do { 
       String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)); 
       String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)); 
       long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION)); 
       String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)); 
       String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)); 
       long albumId = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)); 
       Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart"); 
       Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, albumId); 
       SongInfoModel s = new SongInfoModel(name, artist, null, album, null, duration, data,albumArtUri); 
       SongList1.add(s); 

      } while (cursor.moveToNext()); 
     } 

     cursor.close(); 
      Collections.sort(SongList1, new Comparator<SongInfoModel>() { 
      @Override 
      public int compare(SongInfoModel lhs, SongInfoModel rhs) { 
       return lhs.getAlbum().compareTo(rhs.getAlbum()); 
      } 
     }); 
    } 

    recyclerViewAlbum.setAdapter(albumsAdapter); 
    albumsAdapter.notifyDataSetChanged(); 
    return view; 
    } 
} 

AlbumsDetails.java

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.album_details); 

    albumsDetails_reyclerView = findViewById(R.id.albumsDetails_reyclerView); 

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext()); 
    albumsDetails_reyclerView.setLayoutManager(linearLayoutManager); 

    Bundle extras = getIntent().getExtras(); 
    if(extras != null){ 
    } 


    albumsDetailsAdapter = new AlbumsDetailsAdapter(getApplicationContext(), SongList2, new AlbumsDetailsAdapter.RecyclerItemClickListenerAlbumsDetails() { 
     @Override 
     public void onClickListener(SongInfoModel songInfoModelAlbumDetails, int positionAlbumDetails) { 
     } 
    }){ 


    }; 
    albumsDetails_reyclerView.setAdapter(albumsDetailsAdapter); 
    albumsDetailsAdapter.notifyDataSetChanged(); 
    } 
} 
+0

在適配器內部的視圖持有者類內部單擊。這是從適配器獲取點擊的正確方法 –

回答

0

內部回收站查看適配器,從ViewHolder類你可以CLL

View.setOnClickListener();

的內部recyclerview

0

你需要得到相冊ID,並通過該專輯ID意圖開始的活動,特定項目。在該活動中,使用該專輯ID並查詢光標以獲取與該專輯相關的歌曲。希望回答這個問題。以下是您可以用來獲取allSongsViaAlbum ID的代碼。

public List<SongModel> getAllSongsViaAlbumId(long albumId) throws Exception { 
    List<SongModel> songList = null; 
    if (mIsPermissionGranted) { 
     ContentResolver contentResolver = mContext.getContentResolver(); 

     Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
     String selection = MediaStore.Audio.Media.ALBUM_ID + "=?"; 
     String [] whereArgs = {String.valueOf(albumId)}; 
     String sortOrder = MediaStore.Audio.Media.TITLE + " ASC"; 
     Cursor cursor = contentResolver.query(uri, null, selection, whereArgs, sortOrder); 

     if (cursor != null && cursor.moveToFirst()) { 
      songList = new ArrayList<>(); 
      int totalSongs = cursor.getCount(); 
      LogUtility.debugLog(MediaUtility.class.getSimpleName(), "Total number of audios " + totalSongs); 
      while (cursor.moveToNext()) { 
       SongModel currentSong = new SongModel(); 
       currentSong.setAlbum(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM))); 
       currentSong.setAlbumId(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID))); 
       currentSong.setArtist(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST))); 
       currentSong.setData(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA))); 
       currentSong.setTitle(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE))); 
       currentSong.setAlbumArt(getAlbumArtViaAlbumId(currentSong.getAlbumId())); 

       songList.add(currentSong); 
      } 
      cursor.close(); 
     } 
    } 
    return songList; 
} 

On Click事件使用下面的代碼

Intent intent = new Intent(this, yourDesireActivity); 
    intent.putExtra("album_id", albumId); 
    startActivity(intent); 

而當新的活動開始調用getAllSongsViaAlbumId()方法,並傳遞你從以前的活動意圖讓你可以使用下面的代碼來獲取ID

long albumId = getIntent().getExtras().getLong("album_id") 

希望幫助你

+0

您可以爲此寫一段代碼嗎?如果您不介意 –

+0

@SebinPaul我已添加代碼 –

+0

謝謝!我會研究它,讓我知道如果我遇到任何問題! –

1

1)你需要b鍵從適配器到活動。

  • 其中您可以傳遞要在下一個回收視圖中顯示的模型,只需將該模型通過下一個回收視圖構造函數即可。

2)在下一個回收站視圖適配器中,您有模型只顯示該模型的值。

注意: - 假設你知道如何給回電話。

0

首先,您需要了解如何聲明RecyclerView適配器。這是一個非常好的教程:https://antonioleiva.com/recyclerview/

此外,您需要設置標籤到列表中的每個元素,Holder.itemView.setTag(HOLDER POSITION);最後,您可以聲明一個點擊偵聽器,您可以在其中檢索標記。

public void onClick(View v) { 

       int tempid = (int) v.getTag(); 

       Intent intent = new Intent(getContext(), destination.class); 

       intent.putExtra("ID", (int) v.getTag()); 

       startActivity(intent); 

在目標類上,可以通過調用id = getIntent()。getExtras()來檢索id。getInt( 「ID」);

相關問題