Kotlin-recycleview简单使用和删除

效果图:

Kotlin-recycleview简单使用和删除_第1张图片

1.

activity_main 页面主布局

2.

list_item 页面子布局





    

    

        

        

    


    

3.添加适配器,添加list数据源,左滑可以删除数据,

 删除数据实现思路:removeAt数据源 然后调用notify方法刷新adapter即可

package org.mightyfrog.android.minimal.recyclerview

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.helper.ItemTouchHelper
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

/**
 * RecyclerView swipe-and-dimiss, drag-and-drop sample code.
 *
 * @author Shigehiro Soejima
 */
class MainActivity : AppCompatActivity() {
    //参考网址: https://github.com/mightyfrog/RecyclerView
    companion object {
        private val list = ArrayList(100)

        init {
            for (i in 0..10) {
                list.add("#$i")
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        recyclerView.also { rv ->
            rv.layoutManager = LinearLayoutManager(this) as RecyclerView.LayoutManager?
            rv.adapter = MyAdapter()
            setupItemTouchHelper()
        }
    }


    private fun setupItemTouchHelper() {
        val helper = ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(
                ItemTouchHelper.UP or ItemTouchHelper.DOWN,
                ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {
            override fun onMove(recyclerView: RecyclerView, selected: RecyclerView.ViewHolder,
                                target: RecyclerView.ViewHolder): Boolean {
                val from = selected.adapterPosition
                val to = target.adapterPosition
                Collections.swap(list, from, to)
                recyclerView.adapter?.notifyItemMoved(from, to)

                return true
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                list.removeAt(viewHolder.adapterPosition)
                recyclerView.adapter?.notifyItemRemoved(viewHolder.adapterPosition)
            }
        })
        helper.attachToRecyclerView(recyclerView)
    }

    private class MyAdapter : RecyclerView.Adapter() {

        override fun getItemCount(): Int {
            return list.size
        }

        override fun onBindViewHolder(viewHolder: MyViewHolder, i: Int) {
            viewHolder.tv.text = list[i]
            viewHolder.button.setOnClickListener {
               Log.e("TAG","wang button: "+i)
            }
        }

        override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): MyViewHolder {
            val view = LayoutInflater.from(viewGroup.context)
                    .inflate(R.layout.list_item, viewGroup, false)
            return MyViewHolder(view)
        }
    }

    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tv: TextView = itemView.findViewById(R.id.text_view)
        val button: Button = itemView.findViewById(R.id.button)
    }
}

 

end

你可能感兴趣的:(android,学习)