今天,我正在努力解决最疯狂的问题之一,这使我发疯。在我的RecyclerView项目xml中,如果我只有imageView,则什么都没有显示,并且列表为空,但是如果我在该ImageView下方添加TextView,则一切正常,ImageView和TextView都可以。发生了什么,请帮忙。
<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>
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);
}
}
}
答案 0 :(得分:0)
尝试在item_row.xml中添加ViewGroup
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp" // or wrap_content >
<ImageView/>
</LinearLayout>
答案 1 :(得分:0)
为您的ImageView
例如:提供默认的高度和宽度值:
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="@+id/imageview"
/>
或为您的ImageView
我相信您的代码实际上是有效的,只是RecyclerView
未正确设置大小。
RecyclerView
回收已创建的视图,因为您的ImageView
没有尺寸,因此将其缓存为每行的大小,这导致您在Glide
上加载图像那些小的或看不见的行。
此外,您还可以利用Glide
的大小调整功能将其放入所需的容器中,如下所示:
Glide
.with(context)
.load(path)
.apply(new RequestOptions().override(100, 100))
.into(imageView);