Ben Butterworth
Ben Butterworth

Reputation: 28482

Image not being downloaded by Glide inside a Fragment

I am trying to download an image into an ImageView inside a Fragment, but it seems like the network request does not even get made by Glide. (I use Proxyman to watch network traffic from my physical Android device). I am not sure what else to try.

Here is the fragment code:

class ExampleFragment : Fragment(R.layout.fragment_example) {
    @Inject
    lateinit var viewModelFactory: ViewModelProvider.Factory
    lateinit var viewModel: ExampleViewModel
    private lateinit var binding: FragmentExampleBinding

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding = FragmentExampleBinding.inflate(layoutInflater)
        (activity as ExampleActivity).appComponent.inject(this)
        viewModel = ViewModelProviders.of(this, viewModelFactory)
                .get(ExampleViewModel::class.java)

       // I have also tried `activity as FragmentActivity` instead of this
        Glide.with(this).load("https://www.popwebdesign.net/popart_blog/wp-content/uploads/2018/01/tiny-png-panda.jpg")
                .override(200)
                .into(binding.imageViewFaceTaggingStart)
    }
}

Extra details:

<androidx.constraintlayout.widget.ConstraintLayout ... >
    <androidx.appcompat.widget.Toolbar ...></androidx.appcompat.widget.Toolbar>
    <ScrollView ...>
    <LinearLayout ...>
         <ImageView
                android:id="@+id/image_view_face_tagging_start"
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:visibility="visible"
                android:background="@android:color/black"
                android:scaleType="centerCrop"/>
         ....more views (TextView, Space, Checkbox)
        </LinearLayout>
    </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 1

Views: 282

Answers (1)

rahat
rahat

Reputation: 2056

The view that is inflated and the view reference i.e. binding that you are using are not same,

class ExampleFragment : Fragment() {
    @Inject
    lateinit var viewModelFactory: ViewModelProvider.Factory
    lateinit var viewModel: ExampleViewModel
    private lateinit var binding: FragmentExampleBinding


override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentExampleBinding.inflate(layoutInflater)
        return this.binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        (activity as ExampleActivity).appComponent.inject(this)
        viewModel = ViewModelProviders.of(this, viewModelFactory)
                .get(ExampleViewModel::class.java)

       // I have also tried `activity as FragmentActivity` instead of this
        Glide.with(this).load("https://www.popwebdesign.net/popart_blog/wp-content/uploads/2018/01/tiny-png-panda.jpg")
                .override(200)
                .into(binding.imageViewFaceTaggingStart)
    }
}

Now the view of the fragment is the binding, Try with this.

issue in here class ExampleFragment : Fragment(R.layout.fragment_example)

Now the fragment will inflate this R.layout.fragment_example not the binding, so the binding refers to another view,

Upvotes: 1

Related Questions