xml 在ViewHolder中为Recycler View扩展视图
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xml 在ViewHolder中为Recycler View扩展视图相关的知识,希望对你有一定的参考价值。
class MyProfileViewHolder(
itemView: View,
private val myProfileViewModel: MyProfileViewModel) : RecyclerView.ViewHolder(itemView) {
private val userImageView: ImageView = itemView.myProfileMainUserImageView
private val myProfileSelfIntroductionText: TextView = itemView.myProfileSelfIntroductionText
private val favoriteEmptyFrame: FrameLayout = itemView.favoriteEmptyFrame
fun bind() {
// DefaultImage
val url = myProfileViewModel.user.getProfileImageUrl(myProfileViewModel.selectedImageIndex, "medium")
profileViewHelper.setProfileMainImage(
userImageView,
myProfileViewModel.user.getProfileImageUrl(myProfileViewModel.selectedImageIndex, "medium"),
myProfileViewModel.user.gender
)
// Self intro duction
myProfileSelfIntroductionText.text = myProfileViewModel.user.description
// favorite empty frame
when(myProfileViewModel.emptyViewType) {
MyProfileViewModel.EMPTY_VIEW_TYPE_FAVORITE -> {
// it doesn't work
LayoutInflater.from(itemView.context).inflate(R.layout.section_favorite_empty, favoriteEmptyFrame, false)
}
}
}
}
class MyProfileAdapter(
private var items: List<Favorite>,
private val myProfileViewModel: MyProfileViewModel,
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
// -----------------
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
when (viewType) {
TYPE_HEADER -> {
return MyProfileViewHolderHeader(LayoutInflater
.from(parent.context)
.inflate(R.layout.section_my_profile_header, parent, false), myProfileViewModel)
}
TYPE_ITEM -> {
// ......
}
}
throw RuntimeException("there is no type that matches the type $viewType + make sure your using types correctly")
}
// -----------------
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/emptyContents"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/contentsEmptyImg"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginTop="32dp"
android:layout_gravity="center_horizontal"
app:srcCompat="@drawable/empty_image"/>
<TextView
android:id="@+id/contentsEmtpyTxt"
android:layout_width="match_parent"
android:layout_height="18dp"
android:textColor="@color/black"
android:textSize="15sp"
android:text="@string/contents_empty"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
tools:context=".presentation.view.profile.MyProfileFragment">
<ImageView
android:id="@+id/myProfileMainUserImageView"
android:layout_width="240dp"
android:layout_height="240dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"/>
<TextView
android:id="@+id/myProfileSelfIntroductionText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:background="@drawable/self_introduction_border"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:textColor="@color/black"
android:textSize="14sp" />
<FrameLayout
android:id="@+id/favoriteEmptyFrame"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
android:layout_marginBottom="?actionBarSize"
tools:context=".presentation.view.profile.MyProfileFragment">
<android.support.v7.widget.Toolbar
android:id="@+id/myProfileToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:textColor="@color/black"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_my_profile"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
以上是关于xml 在ViewHolder中为Recycler View扩展视图的主要内容,如果未能解决你的问题,请参考以下文章