gpt4 book ai didi

java - Recycler View 不能与 ImageView 一起使用,但可以与 ImageView+TextView 一起使用

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

今天我正在努力解决最疯狂的问题之一,它让我发疯。在我的 RecyclerView 项目 xml 中,如果我只有 imageView,那么什么也没有显示,列表是空白的,但是如果我在该 ImageView 下面添加 TextView,那么一切正常,ImageView 和 TextView。怎么回事,求助。

item_row.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/saved_post_imageview"/>

<!--<TextView-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:id="@+id/text"/>-->

</RelativeLayout>

ProfileActivity.java

public class ProfileActivity extends AppCompatActivity {

FirebaseAuth mFirebaseAuth;
TextView usernameTxt;
ImageView displayPicImg;
RecyclerView recyclerView;
FirebaseDatabase mFirebaseDatabase;
DatabaseReference mUsersReference;
ArrayList<String> mPostList;
SavedPostsAdapter savedPostsAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_activity);

usernameTxt = findViewById(R.id.username);
displayPicImg = findViewById(R.id.display_pic);
recyclerView = findViewById(R.id.saved_posts);
mFirebaseDatabase = FirebaseDatabase.getInstance();
mUsersReference = mFirebaseDatabase.getReference().child("users");
mPostList = new ArrayList<>();
mFirebaseAuth = FirebaseAuth.getInstance();
String username = mFirebaseAuth.getCurrentUser().getDisplayName();
getSavedPosts(username);
savedPostsAdapter = new SavedPostsAdapter(mPostList,ProfileActivity.this);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(false);
recyclerView.setAdapter(savedPostsAdapter);
Uri displayPicURL = mFirebaseAuth.getCurrentUser().getPhotoUrl();
usernameTxt.setText(username);
setTitle(username);
Glide.with(ProfileActivity.this).load(displayPicURL).apply(new RequestOptions().circleCrop()).apply(new RequestOptions().placeholder(R.drawable.defaultuser)).into(displayPicImg);
}

private void getSavedPosts(String username){
Query query = mUsersReference.orderByChild("username").equalTo(username.toLowerCase());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Log.d(MainActivity.TAG,""+dataSnapshot.getChildrenCount());
for(DataSnapshot data: dataSnapshot.getChildren()){
ArrayList<String> postList = (ArrayList<String>) data.child("post").getValue();
Log.d(MainActivity.TAG,"post "+dataSnapshot.getChildrenCount());
for(String imageUrl: postList){
Log.d(MainActivity.TAG,"Image url = "+imageUrl);
mPostList.add(imageUrl);
savedPostsAdapter.notifyDataSetChanged();
}
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}

}

适配器

public class SavedPostsAdapter extends RecyclerView.Adapter<SavedPostsAdapter.ViewHolder> {

ArrayList<String> savedPostList;
Context context;

public SavedPostsAdapter(ArrayList<String> savedPostList, Context context) {
this.savedPostList = savedPostList;
this.context = context;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_row,viewGroup,false);
ViewHolder vh = new ViewHolder(view);
return vh;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
// viewHolder.text.setText("fsdfdas");
Glide.with(context).load("https://firebasestorage.googleapis.com/v0/b/memespace-58171.appspot.com/o/images%2Fimage%3A197200?alt=media&token=8501146f-201d-4fca-a9e3-63cb49fc47de").into(viewHolder.savedPostImageView);
}

@Override
public int getItemCount() {
Log.d(MainActivity.TAG,"count = "+savedPostList.size());
return savedPostList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

ImageView savedPostImageView;
// TextView text;
public ViewHolder(@NonNull View itemView) {
super(itemView);
savedPostImageView = itemView.findViewById(R.id.imageview);
// text = itemView.findViewById(R.id.text);

}
}
}

最佳答案

为您的 ImageView 提供默认的高度和宽度值,例如:

<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="@+id/imageview"
/>

或者为您的 ImageView 提供默认静态图像

我相信您的代码确实有效,只是 RecyclerView 未正确设置大小。

RecyclerView 回收已经创建的 View ,因为你的 ImageView 没有它缓存的尺寸作为每行的大小,这导致你的 滑动 在那些小的或不可见的行上加载图像。

此外,您还可以利用 Glide 的大小调整功能将其放入所需的容器中,如下所示:

Glide
.with(context)
.load(path)
.apply(new RequestOptions().override(100, 100))
.into(imageView);

关于java - Recycler View 不能与 ImageView 一起使用,但可以与 ImageView+TextView 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54753556/

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