Created
July 17, 2016 14:44
-
-
Save nosix/e818f13c7bed7bf0aa9bec4eb06279db to your computer and use it in GitHub Desktop.
RecylerView for Android (SDK 22) in Kotlin 1.0.2
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"?> | |
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="horizontal" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<TextView | |
android:id="@+id/name" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" /> | |
</LinearLayout> |
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"?> | |
<android.support.v7.widget.RecyclerView | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
app:layoutManager="LinearLayoutManager" | |
tools:context=".MainActivity" | |
tools:listitem="@layout/item" /> |
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
package xxx.view | |
import android.app.Activity | |
import android.os.Bundle | |
import android.support.v4.app.Fragment | |
import android.support.v7.widget.RecyclerView | |
import android.support.v7.widget.helper.ItemTouchHelper | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import xxx.R | |
class XXXFragment : Fragment() { | |
private var adapter: XXXRecyclerViewAdapter? = null | |
override fun onAttach(activity: Activity) { | |
super.onAttach(activity) | |
if (activity is XXXListener) { | |
adapter = XXXRecyclerViewAdapter() | |
} | |
} | |
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, | |
savedInstanceState: Bundle?): View { | |
return inflater.inflate(R.layout.list, container, false).let { | |
it as RecyclerView | |
it.adapter = adapter | |
it | |
} | |
} | |
override fun onDetach() { | |
super.onDetach() | |
adapter = null | |
} | |
fun update(names: List<String>) = adapter?.let { it.names = names } | |
} |
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
package xxx.view | |
import android.graphics.Color | |
import android.graphics.PorterDuff | |
import android.support.v7.widget.RecyclerView | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import android.widget.ImageButton | |
import android.widget.TextView | |
import xxx.R | |
class XXXRecyclerViewAdapter() : RecyclerView.Adapter<XXXRecyclerViewAdapter.ViewHolder>() { | |
var names = listOf<String>() | |
set(names) { | |
field = names | |
notifyDataSetChanged() | |
} | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | |
val view = LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false) | |
return ViewHolder(view) | |
} | |
override fun getItemCount(): Int { | |
return names.size | |
} | |
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | |
holder.name = names[position] | |
} | |
class ViewHolder(val view: View) : RecyclerView.ViewHolder(view) { | |
val nameView = view.findViewById(R.id.name) as TextView | |
var name: String | |
get() = nameView.text.toString() | |
set(name) { | |
nameView.text = name | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment