我试图开发一个音乐应用程序,所以我有两个活动,例如ArtistActivityFavoriteActivity。我的ArtistActivity包含一个RecyclerView,该SongNameSongUrlSongArtistListView加载到FavoriteActivity中,并从Firebase加载数据,并且每个项目的末尾都有一个心跳图标。我的主要问题是,当我单击项目时,我想在其他活动中发送被单击的项目,这是我的FavoriteActivity,并且当我单击SongUrl时,我想播放歌曲。因此,为此,我需要在FavoriteActivity中管理Add to favorite in some apps,对此感到困惑。

例如 :

当我们单击FavoriteActivity时,您可以在我的应用中以Spotify为例。我想建立相同的功能,我在下面提供了我的代码。因此,如果您对我的问题有任何答案,请回答我。

这是我的代码:

我的适配器类:

package com.geetmp3.GeetMp3.ArtistPanel;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.geetmp3.GeetMp3.R;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import xyz.hanks.library.bang.SmallBangView;

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.bangView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (holder.bangView.isSelected()) {
                    holder.bangView.setSelected(false);

                    // What can i do here


                } else {
                    holder.bangView.setSelected(true);
                    holder.bangView.likeAnimation(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            super.onAnimationEnd(animation);
                            Toast.makeText(mContext, mUploads.get(position).getSongName() + "Added to playlist", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });


    }

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

    public class PanelViewHolder extends RecyclerView.ViewHolder {

        private TextView textViewName;
        private CircleImageView circleImageView;
        private LinearLayout thisLayout;

        private SmallBangView bangView;


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

            thisLayout = itemView.findViewById(R.id.artist_linear);
            textViewName = itemView.findViewById(R.id.artist_song_name);
            circleImageView = itemView.findViewById(R.id.artist_songs_image);
            bangView = itemView.findViewById(R.id.bang_like_heart);


        }

    }
}


我的getter和setter课:

public class PanelList {


        private String SongName;
        private String SongUrl;
        private String SongImageUri;
        private String SongLyrics;
        private String SongMusicDirector;
        private String SongProducer;
        private String SongArtist;

        public PanelList() {

        }

        public PanelList(String SongName, String SongUrl, String SongImageUri, String SongArtist, String SongLyrics, String SongMusicDirector, String SongProducer) {
            this.SongName = SongName;
            this.SongUrl = SongUrl;
            this.SongImageUri = SongImageUri;
            this.SongArtist = SongArtist;
            this.SongLyrics = SongLyrics;
            this.SongMusicDirector = SongMusicDirector;
            this.SongProducer = SongProducer;
        }


        public String getSongArtist() {
            return SongArtist;
        }


        public String getSongLyrics() {
            return SongLyrics;
        }


        public String getSongMusicDirector() {
            return SongMusicDirector;
        }


        public String getSongProducer() {
            return SongProducer;
        }


        public String getSongName() {
            return SongName;
        }


        public String getSongUrl() {
            return SongUrl;
        }


        public String getSongImageUri() {
            return SongImageUri;
        }


    }


我的艺术家活动:

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) {

            }
        });
    }
}


除了onCreate,我的中什么都没有。

最佳答案

如果列表是动态的,请不要在适配器中传递。

在您的适配器中添加此功能

public void setPanelList(List<PanelList> panelList){

    panelList = mList;
    notifyDataSetChanged();
}


修改您的mDataRef.addValueEventListener

   @Override
   public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

       mList.clear();

       for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {

           PanelList list = postSnapshot.getValue(PanelList.class);
           mList.add(list);
       }

       panelAdapter.setPanelList(mList);
  }


希望这可以帮助。

07-26 05:22