适配器类中的加载栏

时间:2019-05-22 10:13:20

标签: android kotlin activity-indicator

我需要在视图位于mainactivity内的适配器类中添加loadingbar。单击mainactivity内部的应用程序图标时,我使用了doAsync方法来加载应用程序。 我遇到错误

  

android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图。

doAsync方法内使用loadingbar时。 下面是Adapter类

class Adapter(ctx: Context, private val appInfoList: ArrayList<AppInfoModel>, private val contentResolver: ContentResolver,
              private val packageManager: PackageManager,
              private val loadingIndicator: AVLoadingIndicatorView,
              private val applicationContext: Context) : RecyclerView.Adapter<Adapter.MyViewHolder>()  {
    private val inflater: LayoutInflater

    init {
        inflater = LayoutInflater.from(ctx)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Adapter.MyViewHolder {
        val view = inflater.inflate(R.layout.list_item, parent, false)
        return MyViewHolder(view)
    }

    override fun onBindViewHolder(holder: Adapter.MyViewHolder, position: Int) {
        var itemView: View? = null
        holder.appIcon.setImageDrawable(appInfoList[position].appIcon)
        holder.appName.setText(appInfoList[position].appName)
        holder.versionNumber.setText(appInfoList[position].versionNumber)
        if (!appInfoList[position].isInstalled) {
            holder.appInfoCard.foreground = ColorDrawable(Color.parseColor("#CCFFFFFF"))
            holder.appInfoCard.isEnabled = false
            holder.warningIcon.visibility = View.VISIBLE
        }
        holder.appInfoCard.setOnClickListener() {
            Log.e("App opened", appInfoList[position].appName)
            var loadingBar:AVLoadingIndicatorView= loadingIndicator.findViewById(R.id.avi)
            Log.d("Loading bar", loadingBar.toString())
            doAsync {
                try {
                    loadingBar!!.setVisibility(View.VISIBLE);
                    LauncherUtils.setDynamicConfig(contentResolver, packageManager,applicationContext, appInfoList[position].auxName!!, appInfoList[position].packageName!!)
                }
                catch (e: Exception) {
                    Log.e("Exception-launcher", e.toString())
                }
                uiThread {

                    //loadingBar!!.setVisibility(View.INVISIBLE)
                }

            }
        }
    }

请帮我看看这个问题

1 个答案:

答案 0 :(得分:0)

就像stacktrace所说的那样,您不应从后台线程设置任何视图的可见性。将loadingBar!!.setVisibility(View.VISIBLE);移到uiThread{}可能会解决您的问题。