Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recyclerview : how to mix some post with image and no image? [duplicate]

I want to display in same recycle-view which is some post have image and some post does not have images.

I can retrieve all the post with image and non-image, but i want to change the size of the post when the user only post text(no image).

I expect the output like twitter feed..some post with image and without image have their own size.

like image 727
Baharun Abu Samah Avatar asked Apr 02 '19 04:04

Baharun Abu Samah


People also ask

Is there an Addheaderview equivalent for RecyclerView?

addItemDecoration(headerDecoration); The decoration is also reusable since there is no need to modify the adapter or the RecyclerView at all.

Why does RecyclerView repeat?

RecyclerView asks the adapter to create a new list item view for the first data item in your list. Once it has the view, it asks the adapter to provide the data to draw the item. This process repeats until the RecyclerView doesn't need any more views to fill the screen.

How do I add space between items in RecyclerView?

The simplest way is to add top/bottom margins around the first item in the adapter's row. android:layout_marginBottom="4dp". (Note adding the margins to the parent layout won't cut it.)

What is nested RecyclerView?

A nested RecyclerView is an implementation of a RecyclerView within a RecyclerView. An example of such a layout can be seen in a variety of apps such as the Play store where the outer (parent) RecyclerView is of Vertical orientation whereas the inner (child) RecyclerViews are of horizontal orientations.


1 Answers

Simple way to achieve this scenario is, All you have to do is create a view with both image and text, in recycler adapter check if image data is available make visibility of image visible else Image visibility gone.

Second Approach for this to make multiple view for RecyclerView.

 @Override
 public void onBindViewHolder(ViewHolder holder, int position) {

        Log.d(TAG, "onBindViewHolder called");        
        ContentItem item = mContentItems.get(position);

        if(item.getName()!=null){
             holder.textName.setVisibility(View.Visible);
             holder.textName.setText(item.getName());        
        }else{
             holder.textName.setVisibility(View.GONE);
        }

       if(item.getPreviewImageDefault()!=null){
             holder.imageIcon.setVisibility(View.Visible)        
             Picasso.with(mContext).load("file://" + item.getPreviewImageDefault()).into(holder.imageIcon);  
       }else{
             holder.imageIcon.setVisibility(View.GONE)
       }
 }
like image 169
Usama Nasir Avatar answered Oct 15 '22 09:10

Usama Nasir