在我的layout中,我有一个recyclerview,其中包含cardviews单元,这些单元在理论上包含framelayout。理想情况下,可以使用依赖于用于填充framelayout的cardview的fragment填充Object中的view。但是,当我有多个单元格时,只有一个单元格包含framelayout。这种现象在屏幕上显示为:用于初始化recyclerview单元格的Viewholder的代码如下所示:public class JokesViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView userName; TextView likeCounterTextView; TextView commentCounterTextView; TextView timeStampTextView; CircleImageView profileImg; ImageButton likeButton; ImageButton options; ImageButton comments; FrameLayout frameLayout; public JokesViewHolder(View view) { super(view); timeStampTextView = view.findViewById(R.id.time_date_textView); frameLayout = view.findViewById(R.id.post_content); profileImg = view.findViewById(R.id.profile_imageview); userName = view.findViewById(R.id.username_textView); options = view.findViewById(R.id.options_imageButton); options.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //todo open custom dialog } }); likeButton = view.findViewById(R.id.favorite_imageButton); likeCounterTextView = view.findViewById(R.id.likes_counter_textView); comments = view.findViewById(R.id.comment_imageButton); commentCounterTextView = view.findViewById(R.id.comment_counter_textView); view.setOnClickListener(this); view.setTag(this); } @Override public void onClick(View view) { //todo open fragment to show all post data plus comments }} @Overridepublic void onBindViewHolder(final JokesViewHolder holder, int position) { final Joke joke = jokes.get(position); holder.userName.setText(joke.getUser()); Constants.DATABASE.child("users/" + joke.getUID() + "/urlString") .addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { String url = dataSnapshot.getValue(String.class); Glide.with(context).load(url).into(holder.profileImg); } @Override public void onCancelled(DatabaseError databaseError) { } }); Constants.DATABASE.child("userpostslikescomments/" + joke.getUID() + "/" + joke.getPushId() + "/likes/num") .addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { Integer num = dataSnapshot.getValue(Integer.class); holder.likeCounterTextView.setText(Integer.toString(0)); } @Override public void onCancelled(DatabaseError databaseError) { } }); Constants.DATABASE.child("userpostslikescomments/" + joke.getUID() + "/" + joke.getPushId() + "/comments/num") .addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { Integer num = dataSnapshot.getValue(Integer.class); holder.commentCounterTextView.setText(Integer.toString(0)); } @Override public void onCancelled(DatabaseError databaseError) { } }); Constants.DATABASE.child("userpostslikescomments/" + joke.getUID() + "/" + joke.getPushId() + "/likes/list/" + Constants.UID) .addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if (dataSnapshot.exists()) { holder.likeButton.setImageResource(R.drawable.ic_favorite_black_24dp); } else { holder.likeButton.setImageResource(R.drawable.ic_favorite_border_black_24dp); } } @Override public void onCancelled(DatabaseError databaseError) {} }); if (joke.getMediaURL().equals("")) { Bundle bundle = new Bundle(); bundle.putString("jokeTitle", joke.getJokeTitle()); bundle.putString("jokeBody", joke.getJokeBody()); bundle.putString("tagline", joke.getTagline()); //may not work TextPostContent tpc = new TextPostContent(); tpc.setArguments(bundle); ((android.support.v4.app.FragmentActivity) context).getSupportFragmentManager() .beginTransaction().add(holder.frameLayout.getId(), tpc).commit(); } else if (joke.getMediaURL().contains(".png")) {//may need to take into account more file types Bundle bundle = new Bundle(); bundle.putString("media_url", joke.getMediaURL()); bundle.putString("tagline", joke.getTagline()); ImagePostContent ipc = new ImagePostContent(); ipc.setArguments(bundle); ((android.support.v4.app.FragmentActivity) context).getSupportFragmentManager() .beginTransaction().add(holder.frameLayout.getId(), ipc).commit(); } else { Bundle bundle = new Bundle(); bundle.putString("media_url", joke.getMediaURL()); bundle.putString("tagline", joke.getTagline()); VideoPostContent vpc = new VideoPostContent(); vpc.setArguments(bundle); ((android.support.v4.app.FragmentActivity) context).getSupportFragmentManager() .beginTransaction().add(holder.frameLayout.getId(), vpc).commit(); } holder.timeStampTextView.setText(joke.getTimeStamp()); holder.likeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { final String path = "userpostslikescomments/" + joke.getUID() + "/" + joke.getPushId() + "/likes/list/" + Constants.UID; Constants.DATABASE.child(path) .addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if (dataSnapshot.exists()) { Constants.DATABASE.child(path).removeValue(); holder.likeButton.setImageResource(R.drawable.ic_favorite_border_black_24dp); } else { Constants.DATABASE.child(path).setValue(true); holder.likeButton.setImageResource(R.drawable.ic_favorite_black_24dp); } } @Override public void onCancelled(DatabaseError databaseError) {} }); } }); holder.comments.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //todo start animation between fragments //todo show recyclerview of comments } });}我的直觉告诉我,不应该执行片段事务,而应该以编程方式添加视图(例如textviews和imageviews)。但是我想知道是否有办法使用已经创建的framelayout类填充cardviews的fragment吗? 最佳答案 为了确保recyclerview中的每个单元格都有一个用文本,图像或视频填充的框架布局(相对于只有一个单元格的框架布局填充了应该拆分为多个单元格的信息),应调用通过frameLayout.setId(View.generateViewId())找到对帧布局的引用之后。快速滚动会导致致命错误。另一个解决方案是为不同类型的单元格创建多视图持有人。 10-07 23:24