您好,我是Kotlin的新手,我有近1个月的编程,我有一个回收商,我试图在回收商中重新订购卡片,并删除那些我不会需要通过刷它,我怎么能实现这一目标?可以用图书馆来完成吗?我找不到一个
这就是我所拥有的:
适配器
open class EmployeesAdapter(var context: Context, var employee:ArrayList<EmployeesTest>):BaseAdapter() {
private class ViewHolder(row: View?) {
var txt_user: TextView
var user_job: TextView
var img_user: ImageView
var checBox: CheckBox
init {
this.txt_user = row?.findViewById(R.id.txt_user) as TextView
this.img_user = row.findViewById(R.id.img_user) as ImageView
this.user_job = row.findViewById(R.id.user_job) as TextView
this.checBox = row.findViewById(R.id.checkBox) as CheckBox
}
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
var view: View
var viewHolder: ViewHolder
if (convertView == null) {
var layout = LayoutInflater.from(context)
view = layout.inflate(R.layout.employee_list_item, parent, false)
viewHolder = ViewHolder(view)
view.tag = viewHolder
} else {
view = convertView
viewHolder = view.tag as ViewHolder
}
var employees:EmployeesTest=getItem(position) as EmployeesTest
viewHolder.txt_user.text = employees.nombre
viewHolder.img_user.setImageResource(employees.profilePic)
viewHolder.user_job.text = employees.cargo
viewHolder.checBox.isChecked = false
return view
}
override fun getItem(position: Int): Any {
return employee[position]
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getCount(): Int {
return employee.count()
}
}
活动
class EditEmployeeFragment : Fragment() {
var tv_name: TextView? = null
var rel_main: RelativeLayout? = null
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
var view:View?=inflater!!.inflate(R.layout.edit_employee_fragment,container,false)
var arrReports: ArrayList<EmployeesTest>? = ArrayList()
arrReports?.add(EmployeesTest("Heber","Desarrollador",R.drawable.img_user))
arrReports?.add(EmployeesTest("Mata","Desarrollador",R.drawable.img_user))
arrReports?.add(EmployeesTest("Ernesto","Desarrollador",R.drawable.img_user))
arrReports?.add(EmployeesTest("Toño","Desarrollador",R.drawable.img_user))
var recyclerView:RecyclerView=view?.findViewById(R.id.recycler_edit_employees) as RecyclerView
recyclerView.layoutManager = LinearLayoutManager(this.context, LinearLayout.VERTICAL, false)
recyclerView.adapter = EditEmployeesAdapter( arrReports!!)
return view
}
}
谢谢你的帮助
答案 0 :(得分:0)
This可能有所帮助。这是一个如何实现您正在寻找的教程。这需要一些编码 - 但它是可行的和可维护的解决方案(我在几个项目中使用过它)
PS:是的,这个问题与Kotlin无关;)
答案 1 :(得分:0)
您需要 itemTouchHelper 。
<强>活动强>
recyclerView.addItemDecoration(DividerItemDecoration(this,DividerItemDecoration.VERTICAL)) recyclerView.layoutManager = LinearLayoutManager(this) recyclerView.adapter = simpleAdapter
val swipeHandler = object : SwipeToDeleteCallback(this) {
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
val adapter = recyclerView.adapter as SimpleAdapter
adapter.removeAt(viewHolder.adapterPosition)
}
}
val itemTouchHelper = ItemTouchHelper(swipeHandler)
itemTouchHelper.attachToRecyclerView(recyclerView)
<强> SwipeToDeleteCallback 强>
abstract class SwipeToDeleteCallback(context: Context) : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
private val deleteIcon = ContextCompat.getDrawable(context, R.drawable.ic_delete_white_24)
private val intrinsicWidth = deleteIcon.intrinsicWidth
private val intrinsicHeight = deleteIcon.intrinsicHeight
private val background = ColorDrawable()
private val backgroundColor = Color.parseColor("#f44336")
private val clearPaint = Paint().apply { xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR) }
override fun getMovementFlags(recyclerView: RecyclerView?, viewHolder: RecyclerView.ViewHolder?): Int {
/**
* To disable "swipe" for specific item return 0 here.
* For example:
* if (viewHolder?.itemViewType == YourAdapter.SOME_TYPE) return 0
* if (viewHolder?.adapterPosition == 0) return 0
*/
if (viewHolder?.adapterPosition == 10) return 0
return super.getMovementFlags(recyclerView, viewHolder)
}
override fun onMove(recyclerView: RecyclerView?, viewHolder: RecyclerView.ViewHolder?, target: RecyclerView.ViewHolder?): Boolean {
return false
}
override fun onChildDraw(
c: Canvas?, recyclerView: RecyclerView?, viewHolder: RecyclerView.ViewHolder,
dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean
) {
val itemView = viewHolder.itemView
val itemHeight = itemView.bottom - itemView.top
val isCanceled = dX == 0f && !isCurrentlyActive
if (isCanceled) {
clearCanvas(c, itemView.right + dX, itemView.top.toFloat(), itemView.right.toFloat(), itemView.bottom.toFloat())
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
return
}
// Draw the red delete background
background.color = backgroundColor
background.setBounds(itemView.right + dX.toInt(), itemView.top, itemView.right, itemView.bottom)
background.draw(c)
// Calculate position of delete icon
val deleteIconTop = itemView.top + (itemHeight - intrinsicHeight) / 2
val deleteIconMargin = (itemHeight - intrinsicHeight) / 2
val deleteIconLeft = itemView.right - deleteIconMargin - intrinsicWidth
val deleteIconRight = itemView.right - deleteIconMargin
val deleteIconBottom = deleteIconTop + intrinsicHeight
// Draw the delete icon
deleteIcon.setBounds(deleteIconLeft, deleteIconTop, deleteIconRight, deleteIconBottom)
deleteIcon.draw(c)
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
}
private fun clearCanvas(c: Canvas?, left: Float, top: Float, right: Float, bottom: Float) {
c?.drawRect(left, top, right, bottom, clearPaint)
}
}
看一下不错的例子
答案 2 :(得分:0)
val itemTouchHelperCallback =
object :
ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
return false
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
noteViewModel.delete(noteAdapter.getNoteAt(viewHolder.adapterPosition))
Toast.makeText(
this@MainActivity,
getString(R.string.note_deleted),
Toast.LENGTH_SHORT
).show()
}
}
val itemTouchHelper = ItemTouchHelper(itemTouchHelperCallback)
itemTouchHelper.attachToRecyclerView(rvNote)
答案 3 :(得分:0)
与Sana相同的答案,但使用SnackBar
允许对删除执行“ UNDO”操作:
归因:部分受https://www.journaldev.com/23164/android-recyclerview-swipe-to-delete-undo启发
val itemTouchHelperCallback =
object :
ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
) = false
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
val position = viewHolder.adapterPosition
val note: Note = noteAdapter.getNoteAt(position)
noteViewModel.delete(note)
Snackbar.make(
coordinator_layout, // The ID of your coordinator_layout
getString(R.string.note_deleted),
Snackbar.LENGTH_LONG
).apply {
setAction("UNDO") {
noteViewModel.create(note)
// If you're not using LiveData you might need to tell the adapter
// that an item was inserted: notifyItemInserted(position);
rvNote.scrollToPosition(position)
}
setActionTextColor(Color.YELLOW)
}.show()
}
}
ItemTouchHelper(itemTouchHelperCallback).apply {
attachToRecyclerView(rvNote)
}