I'm currently developing an Android App thanks to Kotlin.
I'm using the default navigation drawer and fragment. I want to do something like this :
So I have two spinner, and the second one (on the second fragment) has an adapter.
My question is that : is there a way, to do this more easily ? I'm struggling with all those bundle between fragments and adapter but I truly believe that there a way more simple than what I'm doing...
First fragment :
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val v = inflater.inflate(R.layout.mes_informations, container, false)
val thisBundle = this.arguments
if(thisBundle != null){
val builder = StringBuilder("Extras:\n")
for (key in thisBundle.keySet()) {
val value = thisBundle.get(key)
builder.append(key).append(": ").append(value).append("\n")
}
selectedArret.text = thisBundle.get("Arret").toString()
}
return v
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val thisBundle = this.arguments
if(thisBundle != null){
val builder = StringBuilder("Extras:\n")
for (key in thisBundle.keySet()) {
val value = thisBundle.get(key)
builder.append(key).append(": ").append(value).append("\n")
}
Log.i(TAG, builder.toString())
}
bundle = Bundle()
spinnerDepartement.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
when (position) {
position -> departement = bundle.putString("departement", spinnerDepartement.selectedItem.toString())
else -> bundle.putString("departement", "Cher")
}
Log.i(TAG, spinnerDepartement.selectedItem.toString())
}
override fun onNothingSelected(parent: AdapterView<*>) {
}
}
val fragmentTransaction = fragmentManager?.beginTransaction()
val rechercheFragm = RechercherArret()
rechercheFragm.arguments = bundle
ligneReguliereLayout.setOnClickListener {
fragmentTransaction
?.replace(R.id.content_frame, rechercheFragm)
?.addToBackStack(null)
?.commit()
}
}
The second :
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
lignes = ArrayList()
val resultArgument = arguments
val queryFill = resources.getStringArray(R.array.fillSearchQuery2)
for(ligne in queryFill){
lignes.add(ligne)
}
adapter = ListAdapterCustom(view.context, R.layout.list_adapter, lignes, resultArgument)
listSearchView.adapter = adapter
search.queryHint = "Entrez un arrêt"
search.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextChange(newText: String): Boolean {
adapter.filter.filter(newText.trim())
if(newText.trim() != ""){
listSearchView.visibility = View.VISIBLE
}else{
listSearchView.visibility = View.GONE
}
return false
}
override fun onQueryTextSubmit(query: String): Boolean {
Toast.makeText(view.context, "Submit $query", Toast.LENGTH_SHORT).show()
return false
}
})
}
And the adapter :
class ListAdapterCustom(context: Context, resource: Int, list: ArrayList<String>, private val arguments: Bundle?) : ArrayAdapter<String>(context, resource, list) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val view = super.getView(position, convertView, parent)
val thisBundle = arguments
if(thisBundle != null){
val builder = StringBuilder("Extras:\n")
for (key in thisBundle.keySet()) {
val value = thisBundle.get(key)
builder.append(key).append(": ").append(value).append("\n")
}
Log.i("Extras", builder.toString())
}
val arret = view.findViewById<TextView>(R.id.arret)
arret.setOnClickListener {
val fragment = MesInformations()
val bundle = Bundle()
bundle.putString("Arret", arret.text.toString())
fragment.arguments = bundle
Snackbar.make(view, arret.text, Snackbar.LENGTH_SHORT).show()
val fragmentManager = (context as AppCompatActivity).supportFragmentManager
val fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack(null)
fragmentTransaction.commit()
fragmentTransaction.addToBackStack(null)
}
if (position % 2 == 1) {
view.setBackgroundResource(R.color.colorWhite)
} else {
view.setBackgroundResource(R.color.grayBackground)
}
return view
}
}
You can create a shared ViewModel
to communicate between fragments. Create a ViewModel
and access them using the hosting Activity
's context inside each of the fragments.
Here is an example copied from the ViewModel documentation here: https://developer.android.com/topic/libraries/architecture/viewmodel
class SharedViewModel : ViewModel() {
val selected = MutableLiveData<Item>()
fun select(item: Item) {
selected.value = item
}
}
class MasterFragment : Fragment() {
private lateinit var itemSelector: Selector
private lateinit var model: SharedViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
model = activity?.run {
ViewModelProviders.of(this).get(SharedViewModel::class.java)
} ?: throw Exception("Invalid Activity")
itemSelector.setOnClickListener { item ->
model.select(item) // <-- This will notify the `DetailFragment`
}
}
}
class DetailFragment : Fragment() {
private lateinit var model: SharedViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
model = activity?.run {
ViewModelProviders.of(this).get(SharedViewModel::class.java)
} ?: throw Exception("Invalid Activity")
model.selected.observe(this, Observer<Item> { item ->
// Update the UI
})
}
}
Here, the SharedViewModel
is accessed in both MasterFragment
and the DetailFragment
. Both are accessing the same instance of the SharedViewModel
because both of them access the ViewModel from the Activity
's Context:
ViewModelProviders.of(*ACTIVITY*).get(SharedViewModel::class.java)
Now you can include some LiveData
in the SharedViewModel
and both fragments can listen / update them, which is eventually reflected on the other fragment as well.