Today i am struggling with one of the craziest problems and its making me crazy. In my RecyclerView item xml, if i have only the imageView then nothing shows up and the list is blank but if i add TextView below that ImageView then everything work, both ImageView and TextView. What is happening , please help.
<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);
}
}
}
Try to add ViewGroup in item_row.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp" // or wrap_content >
<ImageView/>
</LinearLayout>
Provide default height and width value for your ImageView
ex:
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="@+id/imageview"
/>
Or provide default static image for your ImageView
I believe that your code is actually working, it's just that the size wasn't set by the RecyclerView
properly.
RecyclerView
recycles the views that were already created, since your ImageView
doesn't have dimensions it cached that as the size of each row, which results in your Glide
loading image on those small or invisible rows.
Plus, you can take advantage of Glide
's sizing feature also to fit it in the desired container like below:
Glide
.with(context)
.load(path)
.apply(new RequestOptions().override(100, 100))
.into(imageView);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.