在我的应用程序中,我有一些片段,我用这个漂亮的动画在它们之间切换:
activity?.supportFragmentManager?.beginTransaction()
?.setCustomAnimations(
R.anim.slide_in_from_right,
R.anim.slide_out_to_left,
R.anim.slide_in_from_left,
R.anim.slide_out_to_right)
?.replace(R.id.fragmentContainer, articleContentFragment)
?.addToBackStack(null)
?.commit()
在每个片段中,我都有一个 RecyclerView,其中包含多种类型的项目,例如 TextViews 或 ImageViews。它工作正常,但是
当我添加一个 WebView 作为我的 RecyclerView 项目的一种类型时,片段事务的漂亮动画在我第一次打开片段时开始冻结。原因是 WebView 膨胀。
如何在后台线程中膨胀视图,或者可能有更好的解决方案?
(它在 RecyclerViews 中仍然可以正常工作,它不会将 WebView 作为一个项目膨胀)
我的适配器:
class ArticleContentAdapter(
context: Context
) : RecyclerView.Adapter<ArticleContentAdapter.BaseViewHolder<*>>() {
companion object {
private const val headerType = 1
private const val paragraphType = 2
private const val codeSnippetType = 3
private const val imageType = 4
}
private val inflater: LayoutInflater = LayoutInflater.from(context)
private var articlePieces = ArrayList<ArticlePiece>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<*> {
return when (viewType) {
headerType -> ArticleHeaderViewHolder(
inflater.inflate(
R.layout.article_header_item,
parent,
false
)
)
paragraphType -> ArticleParagraphViewHolder(
inflater.inflate(
R.layout.article_paragraph_item,
parent,
false
)
)
codeSnippetType -> ArticleCodeSnippetViewHolder(
inflater.inflate(
R.layout.article_code_snippet_item,
parent,
false
)
)
imageType -> ArticleImageViewHolder(
inflater.inflate(
R.layout.article_image_item,
parent,
false
)
)
else -> throw IllegalArgumentException("Invalid view type.")
}
}
override fun getItemViewType(position: Int): Int {
return when (articlePieces[position]) {
is ArticleHeader -> headerType
is ArticleParagraph -> paragraphType
is ArticleCodeSnippet -> codeSnippetType
is ArticleImage -> imageType
else -> throw IllegalArgumentException("Invalid type of data $position.")
}
}
override fun getItemCount() = articlePieces.size
internal fun addArticlePieces(articlePieces: List<ArticlePiece>) {
this.articlePieces = getAllSortedPieces(this.articlePieces, articlePieces)
notifyDataSetChanged()
}
private fun getAllSortedPieces(
formerPieces: List<ArticlePiece>,
newPieces: List<ArticlePiece>
): ArrayList<ArticlePiece> {
val allSortedPieces = ArrayList<ArticlePiece>()
allSortedPieces.addAll(formerPieces)
allSortedPieces.addAll(newPieces)
allSortedPieces.sortBy { A -> A.positionInArticle }
return allSortedPieces
}
abstract class BaseViewHolder<T>(itemView: View) : RecyclerView.ViewHolder(itemView) {
abstract fun bind(item: T)
}
class ArticleHeaderViewHolder(itemView: View) :
BaseViewHolder<ArticleHeader>(itemView) {
private val articleHeader: TextView = itemView.header
override fun bind(item: ArticleHeader) {
articleHeader.text = item.getEssentialDataOfPiece()
}
}
class ArticleParagraphViewHolder(itemView: View) :
BaseViewHolder<ArticleParagraph>(itemView) {
private val articleParagraph: TextView = itemView.paragraph
override fun bind(item: ArticleParagraph) {
val text = "\t ${item.getEssentialDataOfPiece()}"
articleParagraph.text = text
}
}
class ArticleCodeSnippetViewHolder(itemView: View) :
BaseViewHolder<ArticleCodeSnippet>(itemView) {
private val webView: WebView = itemView.webView
private val loadingPlaceholder: LinearLayout = itemView.loadingPlaceholder
private val noInternetConnectionPlaceholder: LinearLayout =
itemView.noInternetConnectionPlaceholder
override fun bind(item: ArticleCodeSnippet) {
val codeSnippetController = CodeSnippetController(
item,
webView,
loadingPlaceholder,
noInternetConnectionPlaceholder
)
if (item.viewHeight != 0 || item.viewHeight != 1) {
webView.layoutParams.height = item.viewHeight
loadingPlaceholder.layoutParams.height = item.viewHeight
noInternetConnectionPlaceholder.layoutParams.height = item.viewHeight
}
codeSnippetController.loadCodeSnippet()
}
}
class ArticleImageViewHolder(itemView: View) :
BaseViewHolder<ArticleImage>(itemView) {
private val imageView: ImageView = itemView.imageView
// private val loadingPlaceholder: LinearLayout = itemView.loadingPlaceholder
// private val noInternetConnectionPlaceholder: LinearLayout =
// itemView.noInternetConnectionPlaceholder
override fun bind(item: ArticleImage) {
Picasso.get().load(item.url).into(imageView)
}
}
override fun onBindViewHolder(holder: BaseViewHolder<*>, position: Int) {
val piece = articlePieces[position]
when (holder) {
is ArticleHeaderViewHolder -> holder.bind(piece as ArticleHeader)
is ArticleParagraphViewHolder -> holder.bind(piece as ArticleParagraph)
is ArticleCodeSnippetViewHolder -> holder.bind(piece as ArticleCodeSnippet)
is ArticleImageViewHolder -> holder.bind(piece as ArticleImage)
else -> throw IllegalArgumentException()
}
}
}