我正在开发一个新的应用程序,并且应用程序崩溃,并且从Logcat得到以下错误
致命异常:java.lang.ClassCastException android.widget.FrameLayout $ LayoutParams无法转换为 android.support.v7.widget.RecyclerView $ LayoutParams android.support.v7.widget.RecyclerView.getChildViewHolderInt (RecyclerView.java:4609) android.support.v7.widget.RecyclerView.getChildAdapterPosition (RecyclerView.java:4628) com.empowered.healo.ui.screens.favourite.FavouriteExercisesFragment $ initList $ 1.getItemOffsets (最爱练习片段.kt:69) android.support.v7.widget.RecyclerView $ ItemDecoration.getItemOffsets (RecyclerView.java:10521)com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1374)
在我的FavoriteExercisesFragment.java文件下面
class FavouriteExercisesFragment : BaseMvpFragment(), FavoriteExerciseView {
val adapter: FavoriteExerciseAdapter by lazy {
FavoriteExerciseAdapter(context!!)
}
val presenter: FavoriteExercisePresenter = FavoriteExercisePresenter(this)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
rootView = inflater.inflate(R.layout.fragment_favorite_ex, container, false)
return rootView
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
super.onViewCreated(view, savedInstanceState)
}
initToolbar()
initList()
presenter.getFavoriteExercise()
}
private fun initList() {
rvExercise.layoutManager = LinearLayoutManager(context)
rvExercise.adapter = adapter
val helper = ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP or ItemTouchHelper.DOWN, ItemTouchHelper.RIGHT) {
override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
adapter.onItemMove(viewHolder.adapterPosition, target.adapterPosition)
return true
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
val position = viewHolder.adapterPosition
val item = adapter.data[position]
adapter.onItemDismiss(viewHolder.adapterPosition)
presenter.deleteFromFavorite(item, position)
showEmptyData(adapter.data.isEmpty())
}
})
rvExercise.addItemDecoration(object : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, itemPosition: Int, parent: RecyclerView) {
super.getItemOffsets(outRect,itemPosition, parent)
val position = parent.getChildPosition(view!!)
if (position == 0) {
outRect.top = 58.dp
} else {
outRect.top = 0
}
}
})
helper.attachToRecyclerView(rvExercise)
adapter.helper = helper
adapter.actions = object : FavoriteExerciseAdapter.AdapterAction {
override fun playExercise(exercise: Exercise) {
val file = exercise.files.firstOrNull { it.mainFile }
val uri = file?.getFile() ?: ""
context?.startActivityFromCenter(VideoActivity.newInstance(context, uri), rootView, VideoActivity.REQUEST_CODE)
}
}
}
private fun initToolbar() {
toolbar.setOnNavigateIconClickListener { router.exit() }
toolbar.setOnToolIconClickListener { openNavDrawer() }
}
override fun setFavoriteExercises(exercises: ArrayList<Exercise>) {
showEmptyData(exercises.isEmpty())
adapter.addData(exercises)
}
override fun showDeleteMessage(item: Exercise, position: Int) {
val snack = Snackbar.make(rootView, R.string.message_remove_from_favorite, Snackbar.LENGTH_LONG)
snack.setAction(R.string.action_undo) {
presenter.addToFavorite(item)
adapter.addItemOnPosition(item, position)
showEmptyData(adapter.data.isEmpty())
}
snack.show()
}
private fun showEmptyData(show: Boolean) {
tvEmpty.visibility = if (show) View.VISIBLE else View.GONE
emptyIcon.visibility = if (show) View.VISIBLE else View.GONE
}
}
在FavoriteExerciseAdapter.kt之下
FavoriteExerciseAdapter类(val上下文:Context):RecyclerView.Adapter(){
var data: ArrayList<Exercise> = ArrayList()
var actions: AdapterAction? = null
var helper: ItemTouchHelper? = null
override fun onCreateViewHolder(p0: ViewGroup, viewType: Int): FavoriteViewHolder =
FavoriteViewHolder(LayoutInflater.from(context).inflate(R.layout.item_favorite_exercises, p0, true))
override fun onBindViewHolder(holder: FavoriteViewHolder, position: Int) {
val item = data[position]
val preview = item.files.firstOrNull { !it.mainFile }
if (preview != null)
Glide.with(holder.itemView)
.load(preview.getFile())
.into(holder.mainImage)
else holder.mainImage.setImageResource(0)
holder.tvName.text = item.title?.getText
holder.play.setOnClickListener {
actions?.playExercise(item)
}
holder.drag.setOnTouchListener { _, event ->
if (event.action == MotionEvent.ACTION_DOWN) {
helper?.startDrag(holder)
}
false
}
}
override fun getItemCount(): Int = data.size
class FavoriteViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val mainImage: ImageView = itemView.findViewById(R.id.mainImage)
val tvName: TextView = itemView.findViewById(R.id.tvName)
val play: ImageView = itemView.findViewById(R.id.icon_play)
val drag: View = itemView.findViewById(R.id.drag)
}
fun addData(data: ArrayList<Exercise>) {
this.data = data
notifyDataSetChanged()
}
fun addItemOnPosition(item: Exercise, position: Int) {
data.add(position, item)
notifyItemInserted(position)
}
fun onItemDismiss(position: Int) {
data.removeAt(position)
notifyItemRemoved(position)
}
fun onItemMove(fromPosition: Int, toPosition: Int): Boolean {
if (fromPosition < toPosition) {
for (i in fromPosition until toPosition) {
Collections.swap(data, i, i + 1)
}
} else {
for (i in fromPosition downTo toPosition + 1) {
Collections.swap(data, i, i - 1)
}
}
notifyItemMoved(fromPosition, toPosition)
return true
}
interface AdapterAction {
fun playExercise(exercise: Exercise)
}
}
我尝试遵循Android RecyclerView finding out first and last view on ItemDecoration并没有解决问题