首页 > 解决方案 > 片段中可绘制的进度

问题描述

我是android开发的新手,正在做一个项目。当图像被加载时,我试图在我的片段中添加一个 circulaprogressdrawable。但它根本没有加载。当我不使用 Fragment 并且在活动中包含代码时,它曾经可以工作。有人可以告诉我我做错了什么吗?欢迎任何建议。谢谢。以下是供参考的代码

package com.example.demoauthfirebase


import android.content.Context
import android.graphics.Color
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.swiperefreshlayout.widget.CircularProgressDrawable
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions

/**
 * A simple [Fragment] subclass.
 */
class FragmentImage : Fragment() {


    companion object {
        const val ARG_NAME = "urlVal"

        fun newInstance(urlVal: String): FragmentImage {
            val fragment = FragmentImage()

            val bundle = Bundle().apply {
                putString(ARG_NAME, urlVal)
            }

            fragment.arguments = bundle
            return fragment
        }
    }


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val activity = activity as Context

        var circularProgressDrawable = CircularProgressDrawable(activity)
        circularProgressDrawable.strokeWidth = 10f
        circularProgressDrawable.centerRadius = 50f
        circularProgressDrawable.setColorSchemeColors(Color.GREEN)
        circularProgressDrawable.start()


        val view: View = inflater.inflate(
            R.layout.fragment_fragment_image, container,
            false
        )

        val imgView = view.findViewById<ImageView>(R.id.imgView1)

        val urlVal = arguments?.getString(ARG_NAME)
        val options = RequestOptions().placeholder(circularProgressDrawable)
        options.fitCenter()

        Glide.with(activity)
            .load(urlVal)
            .apply(options)
            .override(1200, 3000)
            .into(imgView)
        // Inflate the layout for this fragment

        return view
    }


}

标签: androidkotlin

解决方案


这个是我自己修好的。我使用的图像视图没有明确提到宽度和高度。当我添加它时,它开始出现。不知道这是否是最好的答案,但它似乎有效。下面是fragment.xml的代码

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FragmentImage">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingLeft="10dp"
        android:paddingTop="100dp"
        android:paddingRight="10dp"
        android:paddingBottom="200dp">

        <ImageView
            android:id="@+id/imgView1"
            android:layout_width="338dp"
            android:layout_height="255dp"
            android:scaleType="center"
            android:visibility="visible"
            tools:visibility="visible"></ImageView>
    </LinearLayout>

</FrameLayout>

推荐阅读