Created
July 17, 2016 15:26
-
-
Save nosix/45ece1dfb06124affba35368eb7f820e to your computer and use it in GitHub Desktop.
dismiss DialogFragment 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
package xxx.view | |
import android.app.Activity | |
import android.app.AlertDialog | |
import android.app.Dialog | |
import android.app.DialogFragment | |
import android.os.Bundle | |
import android.support.v7.widget.RecyclerView | |
import xxx.R | |
class XXXDialogFragment() : DialogFragment() { | |
interface XXXDialogListener { | |
fun onXXX(name: String) | |
} | |
companion object { | |
private val TAG = NewSettingDialogFragment::class.qualifiedName | |
private val ARG_NAMES = "names" | |
fun show(activity: Activity, names: Array<String>) { | |
XXXDialogFragment().apply { | |
arguments = Bundle().apply { | |
putCharSequenceArray(ARG_NAMES, names) | |
} | |
}.show(activity.fragmentManager, TAG) | |
} | |
} | |
private var adapter: XXXRecyclerViewAdapter? = null | |
override fun onAttach(activity: Activity) { | |
super.onAttach(activity) | |
if (activity is XXXDialogListener) { | |
adapter = XXXRecyclerViewAdapter(activity) | |
} | |
} | |
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog? { | |
val names = arguments.getCharSequenceArray(ARG_NAMES) | |
val view = activity.layoutInflater.inflate(R.layout.dialog, null) as RecyclerView | |
view.adapter = adapter | |
return AlertDialog.Builder(activity) | |
.setTitle("Sample Dialog") | |
.setView(view) | |
.create() | |
.apply { | |
setCanceledOnTouchOutside(false) | |
adapter?.let { | |
it.dismiss = { this.dismiss() } | |
it.names = names | |
} | |
} | |
} | |
override fun onDetach() { | |
super.onDetach() | |
adapter = null | |
} | |
} |
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.support.v7.widget.RecyclerView | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import android.widget.TextView | |
import xxx.R | |
class XXXRecyclerViewAdapter(val listener: XXXDialogFragment.XXXDialogListener) : | |
RecyclerView.Adapter<XXXRecyclerViewAdapter.ViewHolder>() { | |
var dismiss: (() -> Unit) = {} | |
var names: List<String> = listOf() | |
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 = items.size | |
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | |
holder.name = names[position] | |
holder.nameView.setOnClickListener { | |
listener.onXXX(holder.name) | |
dismiss.invoke() | |
} | |
} | |
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