recyclerview onclicklistener kotlin code example

Example 1: recyclerview onclicklistener

@Override
public void onClick(final View view) {
    int itemPosition = mRecyclerView.getChildLayoutPosition(view);
    String item = mList.get(itemPosition);
    Toast.makeText(mContext, item, Toast.LENGTH_LONG).show();
}

Example 2: recyclerview onclicklistener kotlin

interface CellClickListener {    fun onCellClickListener(data: Model)}

Example 3: recyclerview onclicklistener kotlin

val data = list[position]holder.itemView.setOnClickListener {    cellClickListener.onCellClickListener(data)}

Example 4: how to set onClickListener to RecyclerView kotlin

/* 
How to set up a click listener on RecyclerView in conjunction with
an adapter and a click listener on items
NOT only *RecyclerView itemClickListener*
*RecyclerView onClickListener* and *RecyclerView itemClickListener* together
often such a solution is needed to close pop-up messages 
when clicking anywhere on the screen with RecyclerView list 
This code is more like a crutch but works great for me
*/
private fun CreateRecyclerView(){
   val recycler_list = findViewBiId(R.id.myRecyclerViewId)

   recycler_list.setOnTouchListener { v, _ ->
      // if statement is return boolean value for OnTouchListener
      if (layoutAttachment.visibility == View.VISIBLE || menuButtons.visibility == View.VISIBLE) {
        closeOpenDialogs()
        v.performClick()
      } else {
        false
      }
   } 
  recycler_list.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
  recycler_list.setHasFixedSize(true)
  recycler_list.adapter = myAdapter
  recycler_list.itemAnimator = DefaultItemAnimator()
}

private fun closeOpenDialogs(){
// here do staff for you need click actions like
  if (layoutAttachment.visibility == View.VISIBLE || menuButtons.visibility == View.VISIBLE) {
    layoutAttachment.visibility == View.GONE
    menuButtons.visibility == View.GONE
  }
}

Example 5: recyclerview button onclicklistener

TextView textView;//declare global with in adapter class

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

      private ViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            textView = (TextView)view.findViewById(android.R.id.text1);

      }

      @Override
      public void onClick(View view) {
            Toast.makeText(view.getContext(), "position = " + getLayoutPosition(), Toast.LENGTH_SHORT).show();

         //go through each item if you have few items within recycler view
        if(getLayoutPosition()==0){
           //Do whatever you want here

        }else if(getLayoutPosition()==1){ 
           //Do whatever you want here         

        }else if(getLayoutPosition()==2){

        }else if(getLayoutPosition()==3){

        }else if(getLayoutPosition()==4){

        }else if(getLayoutPosition()==5){

        }

        //or you can use For loop if you have long list of items. Use its length or size of the list as 
        for(int i = 0; i

Example 6: recyclerview onclicklistener kotlin

class MainActivity : AppCompatActivity(), CellClickListener {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)        val recyclerView: RecyclerView = findViewById(R.id.recycler_view)        recyclerView.layoutManager = LinearLayoutManager(this)        recyclerView.adapter = Adapter(this, fetchList(), this)    }    private fun fetchList(): ArrayList {        val list = arrayListOf()        for (i in 0..9) {            val model = Model(R.drawable.ic_collections_black_24dp, "Title : $i", "Subtitle : $i")            list.add(model)        }        return list    }    override fun onCellClickListener() {        Toast.makeText(this,"Cell clicked", Toast.LENGTH_SHORT).show()    }}

Tags:

Misc Example