gpt4 book ai didi

android - Framelayout 不会保留在 recyclerview 单元格中

转载 作者:行者123 更新时间:2023-11-29 19:05:00 25 4
gpt4 key购买 nike

在我的 layout 中,我有一个 recyclerview 包含 cardviews 的单元格,理论上,它包含一个 framelayout。理想情况下,cardview 中的 framelayout 可以使用依赖于用于填充查看。但是,当我有多个单元格时,只有一个单元格包含 framelayout。这种现象在屏幕上显示为:

用于初始化 recyclerview 单元格的查看器的代码如下所示:

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
}
}

@Override
public 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
}
});
}

我的直觉告诉我,我应该以编程方式添加 View (如 textviewsimageviews),而不是执行 fragment 事务。但我想知道是否有一种方法可以使用我已经创建的 fragment 类来填充 cardviewsframelayout

最佳答案

为了确保 recyclerview 中的每个单元格都有一个由文本、图像或视频填充的框架布局(相对于只有一个单元格的框架布局填充了应该分成多个单元格的信息)你在通过 findViewById(int) 找到对框架布局的引用后,应该调用 frameLayout.setId(View.generateViewId())。快速滚动会导致 fatal error 。另一种解决方案是为不同类型的单元格创建多个 View 持有者。

关于android - Framelayout 不会保留在 recyclerview 单元格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47625272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com