Reputation: 28482
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:
into()
, am not using Custom Targets, and have explicitly set the size and background colour of the image view to ensure it is not "zero width".FragmentContainerView
inside the Activity.<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
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