【发布时间】:2019-05-27 13:29:52
【问题描述】:
该应用有两个选项卡的底部导航,一个用于任务,一个用于笔记。
我一开始没有错误,因为它是通过加载任务开始的。但是当我按下笔记标签时它崩溃了。
从主列表的位置减去 1,因为我在顶部(数组中的第一个位置)添加了一个按钮,认为这会更正它。
BaseRecyclerAdapter 类
abstract class BaseRecyclerAdapter<T>(
protected val masterList: MutableList<T> = mutableListOf()
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun getItemCount(): Int = masterList.size + 1
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
if (holder is TaskAdapter.AddButtonViewHolder) {
holder.onBind(Unit)
} else {
(holder as BaseViewHolder<T>).onBind(masterList[position - 1])
}
}
override fun getItemViewType(position: Int): Int =
if (position == 0) {
TYPE_ADD_BUTTON
} else {
TYPE_INFO
}
abstract class BaseViewHolder<E>(val view: View) : RecyclerView.ViewHolder(view) {
abstract fun onBind(data: E)
}
abstract class AddButtonViewHolder(view: View) : BaseViewHolder<Unit>(view)
companion object {
const val TYPE_ADD_BUTTON = 0
const val TYPE_INFO = 1
}
}
NotesAdapter
class NoteAdapter(notesList: MutableList<Note> = mutableListOf()) : BaseRecyclerAdapter<Note>(notesList) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder =
if(viewType == TYPE_ADD_BUTTON){
AddButtonViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.view_add_button, parent, false))
} else {
NoteViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_note, parent, false))
}
override fun getItemViewType(position: Int): Int =
if(position == 0){
TYPE_ADD_BUTTON
}else {
TYPE_INFO
}
override fun getItemCount(): Int = masterList.size + 1
class NoteViewHolder(view: View) :BaseViewHolder<Note>(view){
override fun onBind(data: Note) {
(view as NoteView).initView(data)
}
}
class AddButtonViewHolder(view: View): BaseRecyclerAdapter.AddButtonViewHolder(view){
override fun onBind(data: Unit) {
view.buttonText.text = view.context.getText(R.string.add_button_note)
}
}
}
笔记列表片段
class NotesListFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_notes_list, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
recyclerViewNote.layoutManager = LinearLayoutManager(context)
val adapter = NoteAdapter(mutableListOf(
Note("Ben Mohammad", null),
Note("Joesph and and Maria", null)
))
recyclerViewNote.adapter = adapter
}
companion object {
fun newInstance() = NotesListFragment()
}
}
笔记视图
class NoteView @JvmOverloads constructor (context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 1) :
ConstraintLayout(context, attrs, defStyleAttr){
fun initView(note: Note){
description.text = note.description
}
}
我使用了嵌套的自定义视图这就是我想要做的......显然试图让它非常 OOP 风格被它卡住......
class TaskAdapter(
tasksList: MutableList<Task> = mutableListOf()
) : BaseRecyclerAdapter<Task>(tasksList) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder =
if (viewType == TYPE_INFO) {
TaskViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_task, parent, false))
} else {
AddButtonViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.view_add_button, parent, false))
}
class TaskViewHolder(view: View) : BaseViewHolder<Task>(view) {
override fun onBind(data: Task) {
(view as TaskView).initView(data)
}
}
class AddButtonViewHolder(view: View) : BaseRecyclerAdapter.AddButtonViewHolder(view) {
override fun onBind(data: Unit) {
view.buttonText.text = view.context.getText(R.string.add_button_task)
}
}
}
任务列表片段
class TasksListFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_tasks_list, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
recyclerView.layoutManager = LinearLayoutManager(context)
val adapter = TaskAdapter(
mutableListOf(
Task(
"Testing one!!",
mutableListOf(
Todo("Test 1!!"),
Todo("Test 2!!", true)
)
),
Task("Testing two!!")
)
)
recyclerView.adapter = adapter
}
companion object {
fun newInstance() = TasksListFragment()
}
}
任务视图
class TaskView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 1) :
ConstraintLayout(context, attrs, defStyleAttr) {
lateinit var task: Task
fun initView(task: Task) {
this.task = task
titleView.text = task.title
task.todos.forEach {
todo ->
val todoView =
(LayoutInflater.from(context).inflate(R.layout.view_todo, todoContainer, false) as TodoView).apply {
initView(todo) {
if (isTaskcomplete()) {
createstrikeThrough()
} else {
removeStrikeThrough()
}
}
}
todoContainer.addView(todoView)
}
}
fun isTaskcomplete(): Boolean = task.todos.filter { !it.isComplete }.isEmpty()
private fun createstrikeThrough() {
titleView.apply {
paintFlags = paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
}
}
private fun removeStrikeThrough() {
titleView.apply {
paintFlags = paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
}
}
}
【问题讨论】:
-
请阅读minimal reproducible example,重点是minimal部分。人们不太愿意浏览分布在各种文件中的数百行其他人的代码来调试它。通过尝试编写重现此问题所需的最小代码,让我们的志愿者感到高兴并更愿意为您提供帮助。
标签: android android-studio kotlin