我想将row_center_list.xml放在fragment_center的名为gym_list_recycler的recyclerView中。我不知道,因为 gpt 说没有代码错误。我遵循 ChatGPT 告诉的每一个步骤。制作适配器,创建 Item xml ,,
我不知道为什么没有显示 recyclerview。这是我第一次发布我的问题,所以请解释一下...
<!-----------CenterFragment.kt-------------!>
class CenterFragment : Fragment() {
lateinit var fragmentCenterBinding: FragmentCenterBinding
lateinit var recyclerView: RecyclerView
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
view?.let { super.onViewCreated(it, savedInstanceState) }
val centers = listOf(
Center("Fit Center", "Best gym in town / 5 stars / Free water", "Crossfit", "1234 Street, 2km", R.drawable.ic_launcher_background),
Center("Health Club", "New classes available / 4.5 stars / Free towel", "Yoga", "5678 Avenue, 500m", R.drawable.ic_launcher_background)
)
val view = inflater.inflate(R.layout.fragment_center, container, false)
val purpleColor = Color.parseColor("#800080") // 보라색 정수 값
val defaultColor = ColorStateList.valueOf(Color.TRANSPARENT) // 투명 색상을 나타내는 ColorStateList 생성
val chipDistance = view.findViewById<Chip>(R.id.chip_distance)
val chipDailyPass = view.findViewById<Chip>(R.id.chip_daily_pass)
val chipDiscount = view.findViewById<Chip>(R.id.chip_discount)
val chipClickListener = View.OnClickListener { v ->
val chip = v as Chip
// 현재 칩 색상 확인 후 색상 변경
if (chip.chipBackgroundColor?.defaultColor != purpleColor) {
chip.chipBackgroundColor = ColorStateList.valueOf(purpleColor)
} else {
chip.chipBackgroundColor = defaultColor
}
}
chipDistance.setOnClickListener(chipClickListener)
chipDailyPass.setOnClickListener(chipClickListener)
chipDiscount.setOnClickListener(chipClickListener)
return inflater.inflate(R.layout.fragment_center, container, false)
}
class CenterAdapter(private val centerList: List<Center>) : RecyclerView.Adapter<CenterAdapter.CenterViewHolder>() {
class CenterViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val imageView: ImageView = itemView.findViewById(R.id.imageViewCenterPicture)
val buttonLike: Button = itemView.findViewById(R.id.buttonLikeOff)
val textViewNotice: TextView = itemView.findViewById(R.id.textViewNotice)
val textViewCategory: TextView = itemView.findViewById(R.id.textViewCategory)
val textViewCenterName: TextView = itemView.findViewById(R.id.textViewCenterName)
val textViewDistance: TextView = itemView.findViewById(R.id.textViewDistance)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CenterViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.row_center_list, parent, false)
return CenterViewHolder(itemView)
}
override fun onBindViewHolder(holder: CenterViewHolder, position: Int) {
val currentCenter = centerList[position]
holder.imageView.setImageResource(currentCenter.imageResourceId)
holder.textViewNotice.text = currentCenter.notice
holder.textViewCategory.text = currentCenter.category
holder.textViewCenterName.text = currentCenter.name
holder.textViewDistance.text = currentCenter.address
holder.buttonLike.setOnClickListener {
}
}
override fun getItemCount() = centerList.size
}
data class Center(
val name: String,
val notice: String,
val category: String,
val address: String,
val imageResourceId: Int
)
}
<!-------------row_center_list.xml-------------!>
<?xml version="1.0" encoding="utf-8"?>
<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">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardCornerRadius="10dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="168dp"
android:layout_height="143dp">
<ImageView
android:id="@+id/imageViewCenterPicture"
android:layout_width="123dp"
android:layout_height="103dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.355"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.4"
app:srcCompat="@drawable/ic_launcher_background" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="222dp">
<Button
android:id="@+id/buttonLikeOff"
android:layout_width="48dp"
android:layout_height="43dp"
android:layout_marginEnd="16dp"
android:onClick="onImageClick"
app:icon="@drawable/favorite"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.181" />
<TextView
android:id="@+id/textViewNotice"
android:layout_width="381dp"
android:layout_height="43dp"
android:background="#00FFFFFF"
android:text="공지사항 / 후기 점수 / 무료 서비스"
tools:layout_editor_absoluteX="12dp"
tools:layout_editor_absoluteY="168dp" />
<TextView
android:id="@+id/textViewCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="운동 종류"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="197dp" />
<TextView
android:id="@+id/textViewCenterName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="센터 이름"
app:layout_constraintTop_toBottomOf="@+id/textViewCategory"
tools:layout_editor_absoluteX="197dp" />
<TextView
android:id="@+id/textViewDistance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="주소, 거리"
app:layout_constraintTop_toBottomOf="@+id/textViewCenterName"
tools:layout_editor_absoluteX="197dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</layout>
<!------------------------fragment_center.xml----------------------!>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
</data>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="운동 센터"
android:textAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title" />
<ImageButton
android:id="@+id/cartButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/shopping_cart" />
</androidx.appcompat.widget.Toolbar>
<com.google.android.material.tabs.TabLayout
android:id="@+id/trainerInfoTab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:tabIndicatorColor="@color/black"
app:tabIndicatorFullWidth="true"
app:tabIndicatorHeight="1dp"
app:tabMode="fixed"
app:tabTextColor="@color/black">
<com.google.android.material.tabs.TabItem
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="헬스" />
<com.google.android.material.tabs.TabItem
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="필라테스" />
<com.google.android.material.tabs.TabItem
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="수영" />
</com.google.android.material.tabs.TabLayout>
<com.google.android.material.chip.ChipGroup
android:id="@+id/chipGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:singleLine="true">
<com.google.android.material.chip.Chip
android:id="@+id/chip_distance"
style="@style/Widget.MaterialComponents.Chip.Choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkable="true"
android:onClick="onSortByDistanceClicked"
android:text="거리순"
app:chipBackgroundColor="@color/InputCompartmentOutline_Color" />
<com.google.android.material.chip.Chip
android:id="@+id/chip_daily_pass"
style="@style/Widget.MaterialComponents.Chip.Choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkable="true"
android:text="일일권"
app:chipBackgroundColor="@color/InputCompartmentOutline_Color" />
<com.google.android.material.chip.Chip
android:id="@+id/chip_discount"
style="@style/Widget.MaterialComponents.Chip.Choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkable="true"
android:text="할인시설"
app:chipBackgroundColor="@color/InputCompartmentOutline_Color" />
</com.google.android.material.chip.ChipGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/Detail_Padding"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/gym_list_recylcler"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
</LinearLayout>
</ScrollView>
</layout>
看起来你还没有写.setAdapter() 老实说,我是这个领域的新手,所以它有点盐 我希望这有帮助