我是Firebase的新手。我正在尝试开发一个应用,它将在RecyclerView
上显示音频消息列表作为项目。我希望我的应用程序用户能够发布音频消息,以便其他用户可以收听,评论,喜欢,共享等。我的数据库具有以下结构:
当用户发布帖子时,他/她将被定向到他/她的墙,在那里他/她应该能够看到过去发布的所有帖子,包括最近发布的最新帖子。我在WallActivity.class的onCreate()
方法中放入的FirebaseRecyclerAdapter的代码如下:
Query mRef = FirebaseDatabase.getInstance().getReference().child("users").child(FirebaseAuth.getInstance().getUid()).child("posts");
RecyclerView recycler = (RecyclerView) findViewById(R.id.wall_recycler_view);
recycler.setHasFixedSize(true);
recycler.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerOptions<Post> options =
new FirebaseRecyclerOptions.Builder<Post>()
.setQuery(mRef,Post.class)
.build();
adapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(options) {
@NonNull
@Override
public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.post_layout, parent, false);
return new PostViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull final PostViewHolder holder, final int position, @NonNull final Post model) {
// Initialize media player
mPlayer = new MediaPlayer();
holder.comment.setText(model.comment);
holder.btn_play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// If media player another instance already running then stop it first
stopPlaying(mPlayer);
playMusic(model.getAudio(),mPlayer,holder.seekBar,holder.tv_pass, holder.tv_duration, holder.tv_due);
}
});
holder.btn_stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stopPlaying(mPlayer);
}
});
holder.seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if(mPlayer!=null && b){
mPlayer.seekTo(i*1000);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
};
recycler.setAdapter(adapter);
在我的onStart()
和onStop()
中,具有以下代码:
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
我的ViewHolder类代码如下:
private class PostViewHolder extends RecyclerView.ViewHolder {
LinearLayout root;
RelativeLayout relativeLayout;
LinearLayout linearLayout;
TextView comment, tv_pass, tv_due, tv_duration;
SeekBar seekBar;
Button btn_play, btn_stop;
private PostViewHolder(View itemView) {
super(itemView);
root = (LinearLayout) itemView.findViewById(R.id.list_root);
relativeLayout = (RelativeLayout) root.findViewById(R.id.rlp);
linearLayout = (LinearLayout) root.findViewById(R.id.llp);
comment = (TextView)root.findViewById(R.id.tv_cmt);
tv_pass = (TextView) relativeLayout.findViewById(R.id.tv_pass);
tv_due = (TextView) relativeLayout.findViewById(R.id.tv_due);
tv_duration = (TextView) relativeLayout.findViewById(R.id.tv_duration);
seekBar = (SeekBar) relativeLayout.findViewById(R.id.seek_bar);
btn_play = (Button) linearLayout.findViewById(R.id.btn_play);
btn_stop = (Button) linearLayout.findViewById(R.id.btn_stop);
}
}
注意:
Intent
,有时为空。有时,我只会获得所创建的最新帖子,有时却会充满所有帖子。为了简化调试过程,我将此活动设置为启动器活动,而我得到的只是一个空的RecyclerView
。