kotlin Recycler View 簡單應用自定義adapter ,包括介面
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Base Activity
abstract class BaseActivity : Activity(){ override fun onCreate(savedInstanceState: Bundle?) { super--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------.onCreate(savedInstanceState) initView() } abstract fun initView() protected fun toast(cxt : Context,text_content : String,toast_time : Int = Toast.LENGTH_SHORT){ Toast.makeText(cxt,text_content,toast_time).show() } }
MainActivity
class MainActivity : BaseActivity() { private var recycler_view :RecyclerView? = null private val data = arrayOf( "" ) private var list : MutableList<String>? = null private var adapter : MyAdapter ? =null override fun initView() { setContentView(R.layout.activity_main--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------) recycler_view = findViewById(R.id.recycle_view)as RecyclerView recycler_view!!.layoutManager = LinearLayoutManager(this) list= ArrayList<String>() for (i in data.indices){ list!!.add(data[i]) } adapter = MyAdapter(this,list!!) recycler_view!!.adapter = adapter adapter!!.getListener(MyListener { v, position ->toast(this,"hello$position") }) } }
Adapter
class MyAdapter(private val ctx : Context,private val list:List<String>) : Adapter<MyViewHolder>() { private var listener : MyListener? = null private val layout_inflater :LayoutInflater init { layout_inflater = LayoutInflater.from(ctx) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { val view :View = layout_inflater.inflate(R.layout.item,null) val holder : MyViewHolder = MyViewHolder(view,listener) return holder } override fun onBindViewHolder(holder: MyViewHolder, position: Int) { holder.text_view.text = list.get(position) } override fun getItemCount(): Int { return list.size } class MyViewHolder(itemView: View,private var item : MyListener?) : RecyclerView.ViewHolder(itemView),View.OnClickListener{ val text_view : TextView init { text_view = itemView.findViewById(R.id.item_text) as TextView text_view.setOnClickListener(this) } override fun onClick(v: View?) { item?.itemListener(v,position) } } fun getListener(item: MyListener){ listener = item } }
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
監聽介面
public interface MyListener { void itemListener(View v,int position); }
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
事例簡單,沒多少技巧性
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------