androidstudioのRecyclerViewとアダプターについて質問です。

実現したいこと

androidstudioで、異なる2つのレイアウトファイルを、1つのアダプタ―に渡して、RecyclerViewの表示を切り替えたいです。

前提

下記にURLを張りますが、androidデベロッパーというサイトのチュートリアルでDoglersというアプリを作っています。このアプリではGrid状、水平・垂直方向のレイアウトファイルをアダプターにわたし、スクロール可能な3種類のビューを、Mainactivityの3つのボタンによって切り替え表示をするシンプルなアプリです。

ここで、レイアウトファイルは2つです。理由は、水平・垂直方向のビューのレイアウトは一緒だからです。そのため、Grid状のレイアウトファイルと水平・垂直方向のレイアウトファイルの2つになります。そして、onCreateViewHolderに条件分岐でアダプターのレイアウトを渡しています。

ここで質問なのですが、レイアウトファイルは2種類ありますが、中のビューのIDは同じにしていいのでしょうか?

というのも、チュートリアルのサイトには「ヒント: 各ビューには任意の ID を使用できますが、いずれのレイアウトも同じ ViewHolder クラスを使用するので、各レイアウト内の対応するビューで同じ ID を使用すると良いでしょう。たとえば、グリッド レイアウトと水平 / 垂直レイアウトの両方で、ID が dog_name の TextView を使用します。」と書いてあります。これは明らかに、ビューのレイアウトが異なるレイアウトファイルでも、ビューのリソースが一緒ならば同じID名を付けることを推奨していますよね?

しかし、最初にIDを一緒にいていたところ、アクティビティを実行数するとレイアウトが一緒になってしまいます。もしIDを一緒にしてもしっかりレイアウトを分けられるのであればその方法を教えてほしいです。

下にアダプターのソースコードを張りました。IDを一緒にできるのであればどこを変更すべきなのか教えてほしいです。

https://developer.android.com/codelabs/basic-android-kotlin-training-project-dogglers-app?hl=ja&continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-kotlin-unit-2-pathway-3%3Fhl%3Dja%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-training-project-dogglers-app#2 

発生している問題・エラーメッセージ

エラーメッセージ

該当のソースコード

kotlin

1/* 2* Copyright (C) 2021 The Android Open Source Project. 3* 4* Licensed under the Apache License, Version 2.0 (the "License"); 5* you may not use this file except in compliance with the License. 6* You may obtain a copy of the License at 7* 8* http://www.apache.org/licenses/LICENSE-2.0 9* 10* Unless required by applicable law or agreed to in writing, software 11* distributed under the License is distributed on an "AS IS" BASIS, 12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13* See the License for the specific language governing permissions and 14* limitations under the License. 15*/16package com.example.dogglers.adapter 17 18import android.content.Context 19import android.view.LayoutInflater 20import android.view.View 21import android.view.ViewGroup 22import android.widget.ImageView 23import android.widget.TextView 24import androidx.recyclerview.widget.RecyclerView 25import com.example.dogglers.R 26import com.example.dogglers.const.Layout 27import com.example.dogglers.data.DataSource 28import com.example.dogglers.data.DataSource.dogs 29import org.w3c.dom.Text 30 31/** 32 * Adapter to inflate the appropriate list item layout and populate the view with information 33 * from the appropriate data source 34 */35class DogCardAdapter(36 private val context: Context?,37 private val layout: Int 38): RecyclerView.Adapter<DogCardAdapter.DogCardViewHolder>() {39 40 val myDataset = DataSource 41 //アダプター作成時には、データを宣言する42 43 class DogCardViewHolder(view: View?): RecyclerView.ViewHolder(view!!) {44 45 val dogImage: ImageView = view!!.findViewById(R.id.Imageview)46 val dogName: TextView = view!!.findViewById(R.id.dogName)47 val dogAge: TextView = view!!.findViewById(R.id.dogAge)48 val dogHobbies: TextView = view!!.findViewById(R.id.dogHobbies)49 50 }51 52 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DogCardViewHolder {53 val adapterLayout = LayoutInflater.from(parent.context)54 .inflate(R.layout.vertical_horizontal_list_item, parent, false)55 56 if (layout == Layout.GRID) {57 val adapterLayout = LayoutInflater.from(parent.context)58 .inflate(R.layout.grid_list_item, parent, false)59 } else {60 val adapterLayout = LayoutInflater.from(parent.context)61 .inflate(R.layout.vertical_horizontal_list_item, parent, false)62 }63 // TODO: Use a conditional to determine the layout type and set it accordingly.64 // if the layout variable is Layout.GRID the grid list item should be used. Otherwise the65 // the vertical/horizontal list item should be used.66 67 // TODO Inflate the layout68 69 // TODO: Null should not be passed into the view holder. This should be updated to reflect70 // the inflated layout.71 return DogCardViewHolder(adapterLayout)72 }73 74 override fun getItemCount(): Int = DataSource.dogs.size// TODO: return the size of the data set instead of 075 76 override fun onBindViewHolder(holder: DogCardViewHolder, position: Int) {77 val item = DataSource.dogs[position]78 holder.dogImage.setImageResource(item.imageResourceId)79 holder.dogName.text = item.name 80 holder.dogAge.text = item.age 81 holder.dogHobbies.text = item.hobbies 82 83 84 // TODO: Get the data at the current position85 // TODO: Set the image resource for the current dog86 // TODO: Set the text for the current dog's name87 // TODO: Set the text for the current dog's age88 val resources = context?.resources 89 // TODO: Set the text for the current dog's hobbies by passing the hobbies to the90 // R.string.dog_hobbies string constant.91 // Passing an argument to the string resource looks like:92 // resources?.getString(R.string.dog_hobbies, dog.hobbies)93 }94}95

試したこと

chatGPTに聞きましたが、アダプターは渡されたレイアウトファイルによってレイアウトを判断しますが、異なるレイアウトファイルの中に同じID名があると、レイアウトファイルを一緒にしてしまうとのことでした。これが本当かどうかも知りたいです。詳しい方よろしくお願いします

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

コメントを投稿

0 コメント