【问题标题】:UI blinking after pressing refresh按下刷新后UI闪烁
【发布时间】:2020-10-02 20:45:13
【问题描述】:

我有一个片段,我正在处理来自 xml 源的数据,一切正常,但问题是当我通过分离然后附加片段来刷新布局时,用户界面会一直闪烁,直到处理完所有数据,我检查了我的代码很多次,但我感到迷茫,我找不到任何东西

  • 这就是我处理 xml 源数据的方式
fun getData(){
           val url = URL("www.xmldatasource.com")
           val factory = XmlPullParserFactory.newInstance()
           factory.isNamespaceAware = false
           val xpp = factory.newPullParser()
           xpp.setInput(GetInputStream(url), "utf-8")
           var insideItem = false
           var eventType = xpp.eventType
           while (eventType != XmlPullParser.END_DOCUMENT) {
               if (eventType == XmlPullParser.START_TAG) {
                   if (xpp.name.equals("item", ignoreCase = true)) {
                       insideItem = true
                   } else if (xpp.name.equals("title", ignoreCase = true)) {
                       if (insideItem) {
                           title = xpp.nextText()
                       }
                   } else if (xpp.name.equals("pubDate", ignoreCase = true)) {
                       if (insideItem) {
                           val subbedDate = xpp.nextText()
                           publishDate = subbedDate.substring(0, 25)
                       }
                   } else if (xpp.name.equals("guid", ignoreCase = true)) {
                       if (insideItem) {
                           link = xpp.nextText()
                       }
                   } else if (xpp.name.equals("description", ignoreCase = true)) {
                       if (insideItem) {
                           val newsDescription = xpp.nextText()
                           if (newsDescription.contains("src") && newsDescription.contains("jpg")) {
                               imageUrl = newsDescription.substring(
                                   newsDescription.indexOf("src=") + 5,
                                   newsDescription.indexOf("jpg") + 3
                               )
                           }
                           if (newsDescription.contains("<BR>") && newsDescription.contains("</p>")) {
                               description =
                                   newsDescription.substring(
                                       newsDescription.indexOf("<BR>") + 4,
                                       newsDescription.indexOf("</p>")
                                   )
                           }
                       }
                   }
               } else if (eventType == XmlPullParser.END_TAG && xpp.name.equals("item", ignoreCase = 
                   true)) {
                   insideItem = false
                   imageUrl?.let {
                       //TODO : Getting Image Url And Convert it to Bitmap
                       val urlImg = URL(it)
                       val httpURLConnection = urlImg.openConnection() as HttpURLConnection
                       httpURLConnection.connect()
                       val inputStream = httpURLConnection.inputStream
                       val bitmap = BitmapFactory.decodeStream(inputStream)
                       CoroutineScope(Dispatchers.Main).launch{
                           xmlList?.clear()
                           val model = NewsModel(bitmap, title, description, publishDate!!, link!!)
                           xmlList?.add(model)
                           leagueViewModel.insertNews(model)
                           binding.newsrcycler.adapter = LeagueNewsAdapter(requireContext(), xmlList!!)
                           customType(requireActivity(), "left-to-right")
                               }

                           })
                         
                          
                       }

                   }
               }
               eventType = xpp.next()
           }
   }
  • 这就是我刷新片段的方式
 override fun onOptionsItemSelected(item: MenuItem): Boolean {
      when (item.itemId) {
          R.id.refresh -> {
              if(Constants.checkConnectivity(requireContext())){
                  requireActivity().supportFragmentManager.beginTransaction().detach(this).attach(this).commit()
              } else {
                  requireContext().ShowToast(requireContext(),"Please Check Your Internet..")
              }
          }
       
      }
      return true
  }

  • 这是包含我的适配器的所有视图的布局
 <layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <data>
        <variable
            name="listener"
            type="taki.eddine.premier.league.pro.uilisteners.RssListener" />
        <variable
            name="newsModel"
            type="taki.eddine.premier.league.pro.models.NewsModel" />
    </data>

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="16dp"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginTop="10dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="?attr/selectableItemBackground"
            android:onClick="@{() -> listener.RssArticle(newsModel)}">


            <de.hdodenhof.circleimageview.CircleImageView
                android:layout_width="match_parent"
                android:layout_height="140dp"
                android:layout_margin="8dp"
                android:layout_marginTop="5dp"
                app:layout_constraintBottom_toTopOf="@+id/guideline7"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.0"
                app:rssimg="@{newsModel.newsBanner}"/>

            <TextView
                android:id="@+id/newstitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginTop="4dp"
                android:layout_marginEnd="16dp"
                android:fontFamily="@font/andada"
                android:maxLines="2"
                android:text="@{newsModel.newsTitle}"
                android:textColor="@android:color/black"
                android:textSize="12sp"
                app:layout_constraintBottom_toTopOf="@+id/guideline14"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@+id/guideline7"
                app:layout_constraintVertical_bias="0.0" />

            <TextView
                android:id="@+id/newsdescription"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginTop="4dp"
                android:layout_marginEnd="16dp"
                android:fontFamily="@font/adamina"
                android:maxLines="3"
                android:text="@{newsModel.newsDescription}"
                android:textColor="@android:color/black"
                android:textSize="12sp"
                app:layout_constraintBottom_toTopOf="@+id/guideline15"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@+id/guideline14"
                app:layout_constraintVertical_bias="0.0" />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:orientation="horizontal">
                <de.hdodenhof.circleimageview.CircleImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_marginTop="11dp"
                    android:layout_marginStart="8dp"
                    android:src="@drawable/ic_baseline_date_range_24"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toStartOf="@+id/guideline22"
                    app:layout_constraintHorizontal_bias="0.842"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="@+id/guideline15" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="6dp"
                    android:layout_marginEnd="16dp"
                    android:fontFamily="@font/andada"
                    android:textStyle="bold"
                    android:textSize="11sp"
                    android:textColor="@android:color/holo_blue_dark"
                    android:layout_marginTop="15dp"
                    android:text="@{newsModel.newsDate}"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="@+id/guideline22"
                    app:layout_constraintTop_toTopOf="@+id/guideline15" />
            </LinearLayout>
        </LinearLayout>
    </androidx.cardview.widget.CardView>

</layout>
  • 这是我的片段布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/newsprogress"
        android:visibility="visible"
        android:layout_gravity="center"/>

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/newsrcycler"
        android:scrollbars="vertical"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

【问题讨论】:

    标签: android xml kotlin


    【解决方案1】:

    您似乎在 while 循环中多次设置 RecyclerView 适配器。这将不断刷新RecyclerView

    您不应该更换整个适配器,因为数据会发生变化。数据应保存在适配器内,并应在适配器内更新。

    我是如何得出这个结论的:

    • getData() 包含一个 while() 循环。
    • 在这个while循环中,在你调用CoroutineScope(Dispatchers.Main).launch {}的最后一个分支中
    • 在此launch {} 中,您调用binding.newsrcycler.adapter = LeagueNewsAdapter()

    【讨论】:

    • 感谢您的回答,所以您认为我应该将我的适配器从 while 循环块中取出并在 while 循环之后设置它?
    • 是的!然后适配器可以有一个可变数组,您可以更改其数据。更改完所有数据后,在适配器上调用adapter.notifyDataSetChanged()。这将触发视图更新。
    • 谢谢伙计,我实际上将适配器代码 sn-p 移出循环,它可以工作,感谢帮助
    猜你喜欢
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    • 2015-10-19
    • 2012-02-06
    • 1970-01-01
    • 1970-01-01
    • 2012-10-30
    • 1970-01-01
    相关资源
    最近更新 更多