我正在尝试制作音乐流应用程序。我创建了一个名为“显示活动”的活动。我在“展示活动”中制作了一个水平RecyclerView。在我的Firebase中,我创建了一个节点名称'ArtistView',其中包含Artist List。 之后,我像在Spotify中一样在我的Horizontal recyclerView中加载Artist图像和Artist名称。我的Firebase中还有另一个节点,其中包含Artist的songList。现在我的主要问题是如何使功能类似,当用户单击My RecyclerView中的“ Guri”艺术家,然后在单击后将其加载到Firebase的“新活动”中的歌曲列表,如在Spotify中,如果我们单击任何艺术家,然后将其加载到New活动。这是我的Firebsae数据库的图像:- Firebase Screenshot
如果您理解我的问题,那么这是代码:-
显示活动:-
public class DisplayActivity extends AppCompatActivity {
private ArtistRecyclerView artistRecyclerView;
private DatabaseReference databaseReference;
private ValueEventListener eventListener;
private List<ArtistHoler> artistHolerList;
private RecyclerView recyclerView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dispplay);
artistHolerList = new ArrayList<>();
artistRecyclerView = new ArtistRecyclerView(this , artistHolerList);
databaseReference = FirebaseDatabase.getInstance().getReference("ArtistView");
databaseReference.keepSynced(true);
recyclerView = findViewById(R.id.dispay_recycler_view);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this , LinearLayoutManager.HORIZONTAL , false);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(artistRecyclerView);
eventListener = databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
ArtistHoler artistHoler = snapshot.getValue(ArtistHoler.class);
artistHolerList.add(artistHoler);
}
artistRecyclerView.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
艺术家适配器:-
public class ArtistRecyclerView extends RecyclerView.Adapter<ArtistRecyclerView.HolderOfArtist> {
private List<ArtistHoler> mHolder;
private Context mContext;
public ArtistRecyclerView(Context context, List<ArtistHoler> holder) {
mHolder = holder;
mContext = context;
}
@NonNull
@Override
public HolderOfArtist onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.resource, parent, false);
return new HolderOfArtist(view);
}
@Override
public void onBindViewHolder(@NonNull HolderOfArtist holder, int position) {
ArtistHoler artistHoler = mHolder.get(position);
String text1 = artistHoler.getName();
Glide.with(mContext)
.asBitmap()
.centerCrop()
.load(artistHoler.getImageUri())
.into(holder.image);
holder.image.setOnClickListener(view -> Toast.makeText(mContext, text1, Toast.LENGTH_SHORT).show());
holder.name.setText(text1);
}
@Override
public int getItemCount() {
return mHolder.size();
}
public class HolderOfArtist extends RecyclerView.ViewHolder {
ImageView image;
TextView name;
public HolderOfArtist(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.resource_image);
name = itemView.findViewById(R.id.resource_text);
}
}
}
艺术家持有人:-
public class ArtistHoler {
private String mName;
private String mImageUri;
public ArtistHoler() {
}
public ArtistHoler(String name , String imageUri){
mName = name;
mImageUri = imageUri;
}
public String getName() {
return mName;
}
public void setName(String name){
mName = name;
}
public String getImageUri(){
return mImageUri;
}
public void setImageUri(String imageUri) {
mImageUri = imageUri;
}
}
答案 0 :(得分:0)
在持有人项目上添加click
事件,并采用相应的演出者姓名,并将其传递给下一个活动。
@Override
public void onBindViewHolder(@NonNull HolderOfArtist holder, int position) {
...
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String artistName = mHolder.get(position).getName();
Intent detailIntent = new Intent(mContext, SongsActivity.class);
detailIntent.putExtra("ArtistName", artistName);
startActivity(detailIntent);
}
});
}
然后在下一个活动的onCreate
中获取歌手姓名,如下所示,并调用firebase获取该特定歌手的歌曲列表,然后在onDataChange
String artistName = getIntent().getStringExtra("ArtistName");
FirebaseDatabase.getInstance().getReference("ArtistSongs").orderByKey().equalTo(artistName).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot song: dataSnapshot.child(artistName).getChildren()) {
//here song indicates "Song" with SongName and SongUrl
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});