Search code examples
androidadaptervisible

how to control visibility in android?


how to control visibility in android?

if the user does not upload a photo, the ImageView will gone. In contrast, if the user has upload the photo the ImageView will visible.

so how can I do?

this is my xml code.

<LinearLayout
            android:id="@+id/infophoto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:id="@+id/post_pic"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:scaleType="centerCrop"
                android:src="@drawable/expic"
                android:transitionName="sharedView"
                android:visibility="invisible"
                />
        </LinearLayout>

and this is my adapter code.

public void onBindViewHolder(final ViewHolder holder, final int position) {
glide.load(newFeedModels.get(position).getFeedimageURL()).into(holder.uploader_pro_pic);
glide.load(newFeedModels.get(position).getFeedPostImageURL()).into(holder.post_pic);
}

class ViewHolder extends RecyclerView.ViewHolder{
post_pic = (ImageView) itemView.findViewById(R.id.post_pic);
}

this is homefragment code.

String TAG_FeedMediaPhoto_URL = "FeedPostPhoto";
newFeedModel.setFeedimageURL(json.getString(TAG_FeedMediaPhoto_URL));

I need to modify where code?


Solution

  • At the First step is to check whether this particular item having pic or not

       public void onBindViewHolder(final ViewHolder holder, final int position) {
        
       
            
        if(!TextUtils.isEmpty(newFeedModels.get(position).getFeedPostImageURL())){
            holder.post_pic.setVisibility(View.VISIBLE)
            glide.load(newFeedModels.get(position).getFeedPostImageURL()).into(holder.post_pic);
            }else{
                holder.post_pic.setVisibility(View.GONE)
            }
        }