RecyclerView适配器让我生病 - 在app运行期间没有任何适配器方法的调用。数据在FileListPresenterImpl类中初始化,但没有任何调用。请帮忙。
class FileListAdapter(val fileList: List<File>):
RecyclerView.Adapter<FileListAdapter.ViewHolder>() {
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bindData(fileList[position])
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
var v = LayoutInflater.from(parent?.context).inflate(R.layout.row_item_file, parent, false)
return ViewHolder(v)
}
override fun getItemCount(): Int = fileList.size
class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
fun bindData(file: File) {
itemView.tvFileName.text = file.getFileName()
itemView.tvPath.text = file.savePath
}
}
}
class FileListFragment: ListFragment(), FileListView, ListFragment.RecyclerItemClickListener.OnItemClickListener {
lateinit private var addFileDialog: AddFileDialogFragment
lateinit private var listAdapter: FileListAdapter
lateinit private var listData: ArrayList<File>
val presenter = FileListPresenterImpl(this)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
return context.inflateLayout(R.layout.fragment_list)
}
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
addFileDialog = AddFileDialogFragment()
rvDataList.addOnItemTouchListener(object : RecyclerItemClickListener(activity, listener = this) {})
presenter.loadFileList()
fab.setOnClickListener { showNewFileAddDialog() }
}
...
override fun onFileListLoad(fileList: ArrayList<File>) {
listData = fileList
initAdapter()
}
private fun initAdapter() {
listAdapter = FileListAdapter(listData)
rvDataList.adapter = listAdapter
}
}
class FileListPresenterImpl(private val view: FileListView): FileListPresenter {
override fun loadFileList() {
//TODO load fileList from DB
view.onFileListLoad(loadTestFileList())
}
...
private fun loadTestFileList(): ArrayList<File> {
var testData = ArrayList<File>()
var downloadFolder : String = Environment.DIRECTORY_DOWNLOADS
val f: File = File(downloadFolder, "http://cdndl.zaycev.net/154044/4082702/vremya_i_steklo_-_navernopotomuchto_(zaycev.net).mp3")
testData.add(File(downloadFolder, "http://cdndl.zaycev.net/154044/4082702/vremya_i_steklo_-_navernopotomuchto_(zaycev.net).mp3"))
testData.add(File(downloadFolder, "http://cdndl.zaycev.net/871888/4212215/craig_david_and_sigala_-_ain_t_giving_up_(zaycev.net).mp3"))
testData.add(File(downloadFolder, "http://cdndl.zaycev.net/111639/4135024/albina_-_ne_nado_(zaycev.net).mp3"))
testData.add(File(downloadFolder, "http://cdndl.zaycev.net/126287/4087650/pika_-_patimeyker_(zaycev.net).mp3"))
testData.add(File(downloadFolder, "http://cdndl.zaycev.net/861221/4125726/lil_wayne_and_wiz_khalifa_and_imagine_dragons_and_logic_and_ty_dolla_sign_and_x_ambassadors_-_sucker_for_pain_(zaycev.net).mp3"))
testData.add(File(downloadFolder, "http://cdndl.zaycev.net/12757/4212221/the_black_eyed_peas_-_where_is_the_love_2016_remake_(zaycev.net).mp3"))
testData.add(File(downloadFolder, "http://cdndl.zaycev.net/118309/4214105/yolka_-_na_bolshom_vozdushnom_share_(zaycev.net).mp3"))
testData.add(File(downloadFolder, "http://cdndl.zaycev.net/126708/3989697/potap_i_nastya_-_umamy_(zaycev.net).mp3"))
testData.add(File(downloadFolder, "http://cdndl.zaycev.net/871784/4211700/philip_rossa_-_pam_pam_pam..._radio_edit_(zaycev.net).mp3"))
testData.add(File(downloadFolder, "http://cdndl.zaycev.net/123704/4087949/mot_-_na_dne_(zaycev.net).mp3"))
return testData
}
}
答案 0 :(得分:3)
问题解决了。我错过了将LayoutManager设置为recyclerView