Created
March 7, 2018 05:55
-
-
Save parthdave93/1b1f6871053a575e0e19d2c17eaf4514 to your computer and use it in GitHub Desktop.
Stackoverflow question about selecting item in recyclerview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:id="@+id/mainContainer" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:foreground="@drawable/item_selector" | |
android:padding="16dp"> | |
<TextView | |
android:id="@+id/left_text" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_toRightOf="@id/ivImage" | |
android:layout_centerVertical="true" | |
android:lines="1" | |
android:text="@string/app_name" | |
android:textColor="#000000" | |
android:textSize="11sp" /> | |
<TextView | |
android:id="@+id/right_text" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentRight="true" | |
android:layout_centerVertical="true" | |
android:lines="1" | |
android:text="@string/app_name" | |
android:textColor="#090909" | |
android:textSize="11sp" /> | |
<ImageView | |
android:id="@+id/ivImage" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_margin="10dp" | |
android:layout_alignParentLeft="true" | |
android:src="@drawable/ic_android_black_24dp" /> | |
</RelativeLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<selector xmlns:android="http://schemas.android.com/apk/res/android"> | |
<item android:drawable="@android:color/transparent" android:state_pressed="true" /> | |
<item android:drawable="@color/blue" android:state_selected="true" /> | |
<item android:drawable="@android:color/transparent" android:state_focused="true" /> | |
<item android:drawable="@android:color/transparent" /></selector> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
var arrayList = ArrayList<ItemModel>() | |
for (index in 0 .. 50){ | |
arrayList.add(ItemModel("test $index",false)) | |
} | |
recyclerview.adapter = ResAdapter(arrayList) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.parthdave93.dummytestapp | |
import android.support.v7.widget.RecyclerView | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
/** | |
* Created by parthdave on 07/03/18. | |
*/ | |
class ResAdapter(private val moviesList: List<ItemModel>) : RecyclerView.Adapter<ResAdapter.MyViewHolder>() { | |
override fun getItemCount(): Int { | |
return moviesList.size | |
} | |
inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) { | |
var mainContainer: View? = null | |
init { | |
mainContainer = view.findViewById(R.id.mainContainer) | |
} | |
} | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { | |
val itemView = LayoutInflater.from(parent.context) | |
.inflate(R.layout.item_cell, parent, false) | |
return MyViewHolder(itemView) | |
} | |
override fun onBindViewHolder(holder: MyViewHolder, position: Int) { | |
val movie = moviesList[position] | |
holder.mainContainer?.isSelected = movie.selected | |
holder.itemView.setOnLongClickListener { | |
movie.selected = !movie.selected | |
notifyItemChanged(position) | |
return@setOnLongClickListener true | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment