gpt4 book ai didi

java - 什么时候可以获取 RecyclerView 中 View 的宽度?

转载 作者:行者123 更新时间:2023-11-30 05:20:04 24 4
gpt4 key购买 nike

我正在使用回收器。我需要将 View 的高度设置为容器的宽度。但问题是,当我尝试获取宽度大小时,它似乎还不存在,因此它返回零。如何确保 View 已创建以获取其值并在设置高度时使用它?

这是我设置高度的代码

imageView.getLayoutParams().height = container.getWidth();
imageView.requestLayout();

适配器的代码

public class OnlineUsersAdapter extends RecyclerView.Adapter<OnlineUsersAdapter.OnlineUserViewHolder> {

private List<OnlineUser> onlineUsers = new ArrayList<>();
private OnItemClickListener.OnItemClickCallback onItemClickCallback;
private OnItemClickListener.OnItemClickCallback onChatClickCallback;
private OnItemClickListener.OnItemClickCallback onLikeClickCallback;
private Context context;

public OnlineUsersAdapter(OnItemClickListener.OnItemClickCallback onItemClickCallback,
OnItemClickListener.OnItemClickCallback onChatClickCallback,
OnItemClickListener.OnItemClickCallback onLikeClickCallback) {
this.onItemClickCallback = onItemClickCallback;
this.onChatClickCallback = onChatClickCallback;
this.onLikeClickCallback = onLikeClickCallback;
}

public void addUsers(List<OnlineUser> userList) {
onlineUsers.addAll(userList);
notifyItemRangeInserted(onlineUsers.size() - userList.size(), onlineUsers.size());
}

public void clearData(List<OnlineUser> userList){
int oldSize = userList.size();
onlineUsers = userList;
notifyItemRangeRemoved(0, oldSize);
notifyItemRangeInserted(0, userList.size());
}

@NonNull
@Override
public OnlineUsersAdapter.OnlineUserViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user, parent, false);
return new OnlineUsersAdapter.OnlineUserViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull OnlineUsersAdapter.OnlineUserViewHolder holder, int position) {
OnlineUser user = onlineUsers.get(position);
holder.bind(user, position);
}

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

class OnlineUserViewHolder extends RecyclerView.ViewHolder {
RelativeLayout container;
ImageView imageView, likeBtn, chatBtn;
TextView name, country;

private LottieAnimationView animationView;

OnlineUserViewHolder(View itemView) {
super(itemView);
context = itemView.getContext();
container = itemView.findViewById(R.id.item_user_container);
imageView = itemView.findViewById(R.id.user_img);
likeBtn = itemView.findViewById(R.id.search_btn_like);
chatBtn = itemView.findViewById(R.id.search_btn_chat);
name = itemView.findViewById(R.id.user_name);
country = itemView.findViewById(R.id.user_country);
animationView = itemView.findViewById(R.id.lottieAnimationView);
}

void bind(OnlineUser user, int position) {
ViewCompat.setTransitionName(imageView, user.getName());

if (FirebaseUtils.isUserExist() && user.getUid() != null) {
new FriendRepository().isLiked(user.getUid(), flag -> {
if (flag) {
likeBtn.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_favorite));
animationView.setVisibility(View.VISIBLE);
} else {
likeBtn.setBackground(ContextCompat.getDrawable(context, R.drawable.heart_outline));
animationView.setVisibility(View.GONE);
}
});
}

if (user.getUid() != null) {
chatBtn.setOnClickListener(new OnItemClickListener(position, onChatClickCallback));
likeBtn.setOnClickListener(new OnItemClickListener(position, onLikeClickCallback));
}

imageView.setOnClickListener(new OnItemClickListener(position, onItemClickCallback));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);


if (user.getImage().equals(Consts.DEFAULT)) {
Glide.with(context).load(context.getResources().getDrawable(R.drawable.default_avatar)).into(imageView);
} else {
Glide.with(context).load(user.getImage()).thumbnail(0.5f).into(imageView);
}

country.setText(user.getCountry());

ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f).setDuration(500);
animator.addUpdateListener(valueAnimator ->
animationView.setProgress((Float) valueAnimator.getAnimatedValue()));

if (animationView.getProgress() == 0f) {
animator.start();
} else {
animationView.setProgress(0f);
}
imageView.getLayoutParams().height = 500;
imageView.requestLayout();
}
}

}

最佳答案

等待您的 View 在 UI 上呈现。有几种方法可以做到这一点。其中之一是使用您想要获取高度的 View 的 post()

喜欢

container.post(new Runnable() {
@Override
public void run() {
imageView.getLayoutParams().height = container.getWidth();
imageView.requestLayout();
}
});

View's getWidth() and getHeight() returns 0是一篇很棒的文章,其中也包含一些 Kotlin 解决方案,这比 Java 更容易。

关于java - 什么时候可以获取 RecyclerView 中 View 的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59710325/

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