Last active
November 28, 2017 10:11
-
-
Save maiconhellmann/281702af65288415ae0901172eaafc74 to your computer and use it in GitHub Desktop.
RecyclerView/RxEvent/ViewHolder/Adapter example in Kotlin
This file contains 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
<?xml version="1.0" encoding="utf-8"?> | |
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="64dp" | |
xmlns:tools="http://schemas.android.com/tools"> | |
<TextView | |
android:id="@+id/textView" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
tools:text="Avaria 001" | |
android:textSize="20sp" | |
android:layout_gravity="center_vertical" | |
android:layout_marginLeft="16dp"/> | |
<ImageButton | |
android:id="@+id/imageViewDelete" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:src="@drawable/ic_delete_black_24dp" | |
android:layout_gravity="right|center_vertical" | |
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored" | |
android:layout_marginRight="8dp"/> | |
</FrameLayout> |
This file contains 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
import android.support.v7.widget.RecyclerView | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import kotlinx.android.synthetic.main.item_container.view.* | |
import rx.Observable | |
import rx.subjects.PublishSubject | |
import javax.inject.Inject | |
class RecyclerViewAdapter | |
@Inject | |
constructor() : RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewAdapterViewHolder>() { | |
private val clickSubject = PublishSubject.create<YourObject>() | |
val clickEvent: Observable<YourObject> = clickSubject | |
var dataList: MutableList<YourObject> = emptyList<YourObject>().toMutableList() | |
set(value) { | |
field = value | |
notifyDataSetChanged() | |
} | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewAdapterViewHolder { | |
val itemView = LayoutInflater.from(parent.context) | |
.inflate(R.layout.item_row, parent, false) | |
return RecyclerViewAdapterViewHolder(itemView) | |
} | |
override fun onBindViewHolder(holder: RecyclerViewAdapterViewHolder, position: Int) { | |
val data = dataList[position] | |
holder.textViewIso.text = data.fieldname | |
} | |
override fun getItemCount(): Int = dataList.size | |
inner class RecyclerViewAdapterViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { | |
val textViewUnidade = itemView.textTest | |
init { | |
itemView.setOnClickListener{ | |
clickSubject.onNext(dataList[layoutPosition]) | |
} | |
} | |
} | |
} |
This file contains 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
<?xml version="1.0" encoding="utf-8"?> | |
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/recyclerView" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
app:layoutManager="android.support.v7.widget.LinearLayoutManager" | |
tools:listitem="@layout/item_row"/> | |
/> | |
</FrameLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment