简体   繁体   中英

Loading next item after clicking next button in recyclerView

I have an activity called ArtistActivity , which contains a RecyclerView which loads song data from Firebase and shows it in the Artist Activity. I have an adapter class and a View Holder class for my ArtistActivity RecyclerView. When I click the item in my RecyclerView it sends songUrl , songName , songArtist to another activity called SongPlayingActivity which takes songUrl and plays the song.

My main question is I have the next button in my SongPlayingActivity and I want to load the next item after the user clicks the next button. Like if the user clicks the Next Button then it stops the previous playing song and loads the next song from songList. Much like how it works on Spotify.

I hope you understand my question and if you have any answers regarding this then please reply.

Here is my code: -

ArtistActivity:

public class ArtistActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private ValueEventListener eventListener;
    private DatabaseReference mDataRef, artist_ref;

    private List<PanelList> mList;
    private PanelAdapter panelAdapter;

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

        mRecyclerView = findViewById(R.id.artist_song_recyclerview);
        mDataRef = FirebaseDatabase.getInstance().getReference("ArtistView").child(getIntent().getStringExtra("Artist"))
                .child("Songs");


        mRecyclerView.setHasFixedSize(true);

        mDataRef.keepSynced(true);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mList = new ArrayList<>();

        panelAdapter = new PanelAdapter(ArtistActivity.this, mList);

        mRecyclerView.setAdapter(panelAdapter);

        eventListener = mDataRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                mList.clear();
                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                    PanelList list = postSnapshot.getValue(PanelList.class);
                    mList.add(list);


                }

                panelAdapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }
}

ViewHolder/Adapter class:

public class PanelAdapter extends RecyclerView.Adapter<PanelAdapter.PanelViewHolder> {


    private Context mContext;
    private List<PanelList> mUploads;


    public PanelAdapter(Context context, List<PanelList> panelList) {

        mContext = context;
        mUploads = panelList;

    }

    @NonNull
    @Override
    public PanelViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.artist_recycler_layout, parent, false);
        return new PanelViewHolder(v);

       // Toast.makeText(mContext, "" + ge, Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onBindViewHolder(@NonNull PanelViewHolder holder, int position) {

        PanelList panelList = mUploads.get(position);


        holder.textViewName.setText(panelList.getSongName());

        Glide.with(mContext)
                .load(panelList.getSongImageUri())
                .centerCrop()
                .into(holder.circleImageView);
        holder.thisLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String url = panelList.getSongUrl();

                Intent playSongActivity = new Intent(mContext, D.class);
                playSongActivity.putExtra("SongUrl", panelList.getSongUrl());


                mContext.startActivity(playSongActivity);
            }
        });

  }

    @Override
    public int getItemCount() {
        return mUploads.size();
    }

    public class PanelViewHolder extends RecyclerView.ViewHolder {

        private LinearLayout thisLayout;



        public PanelViewHolder(@NonNull View itemView) {
            super(itemView);

            thisLayout = itemView.findViewById(R.id.artist_linear);



        }

    }
}

My D class: -

public class D extends AppCompatActivity {

    public static SeekBar mSeekbar;
    public static ImageView Play_Icon_D;

    private Button next_button;
    //  private Button popup_button;

    private TextView Song_D, Artist_D;

    private Integer currentIndex;

    private List<PanelList> panelList;

    private Bundle bundle;

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

        mSeekbar = findViewById(R.id.seekbar);

        Play_Icon_D = findViewById(R.id.play_icon_D_class);

        Song_D = findViewById(R.id.song_info_song_D);

        Artist_D = findViewById(R.id.song_info_artist_D);

        next_button = findViewById(R.id.D_next_button);

        bundle = getIntent().getExtras();

        panelList = bundle.getParcelableArrayList("SongUrls");

        currentIndex = bundle.getInt("currentPlayingIndex");


        playSong();


        next_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String newUrl = panelList.get(currentIndex).getSongName();
                currentIndex = currentIndex + 1;

//                Toast.makeText(D.this, newUrl, Toast.LENGTH_SHORT).show();

                FirebaseStorage storage = FirebaseStorage.getInstance();

                StorageReference reference = storage.getReferenceFromUrl(panelList.get(currentIndex).getSongUrl());

                reference.getDownloadUrl().addOnFailureListener(uri -> C.getInstance().play(uri.toString()));


            }
        });

    }

    private void playSong() {
        FirebaseStorage storage = FirebaseStorage.getInstance();
        StorageReference reference = storage.getReferenceFromUrl(panelList.get(currentIndex).getSongUrl());

        reference.getDownloadUrl().addOnFailureListener(uri -> C.getInstance().play(uri.toString()));

    }


}

Instead of sending single song, you can send parcelable arrayList and on next button click pass next position song data.

Pass all the urls in the list to the Song Activity class like this on your onclick:

holder.thisLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent playSongActivity = new Intent(mContext, D.class);
            Bundle bundle = new Bundle();
            bundle.putParcelable("SongUrls", mUploads);
            bundle.putInt("currentPlayingIndex", position)
            playSongActivity.putExtras(bundle);

            mContext.startActivity(playSongActivity);
        }
    });

Then receive them on your SongActivity class like:

Bundle bundle = getIntent().getExtras();
List<PanelList> panelList = bundle.getParcelableArrayList("SongUrls");
Integer currentIndex = bundle.getInt("currentPlayingIndex");

Onclick Listener for next button

 String newUrl = panelList[currentindex].getSongUrl();
 currentIndex = currentIndex + 1;

Don't forget to check if you playing the last array object otherwise your app will crash on adding 1 to the current playing index

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM