I'm want to do this :
- Display a list of item in a list view (done)
- Set a background for the even an odd position (done)
- When clicking on an element, toggle subitems related to the clicked element.
As you know, ArrayAdapter by default works with TextView only (as root element).
So how can I achieve this ? (I've seen this but still a TextView required after changes).
Here's my custom adapter :
class ListAdapterScolaire (context: Context, resource: Int, list: ArrayList<String>, private val arguments: Bundle?, private val forScol : Boolean) : ArrayAdapter<String>(context, resource, list) {
private val TAG = ListAdapterScolaire::class.java.simpleName
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val view = super.getView(position, convertView, parent)
val arret = view.findViewById<TextView>(R.id.arret)
// val rootView = view.findViewById<LinearLayout>(R.id.root_View)
//
// val list = getViewsByTag(rootView, "toToggleAction")
//
// list.forEach{it ->
// it.setOnClickListener{ v: View ->
// val viewParent = v.parent
// if (viewParent is LinearLayout) {
// val taggedView : View? = viewParent.findViewWithTag<View>("toToggle")
//
// taggedView?.visibility = when {
// taggedView?.visibility == View.GONE -> View.VISIBLE
// else -> View.GONE
// }
// }
// }
// }
arret.setOnClickListener { //part to code for the toggle
}
if (position % 2 == 1) {
view.setBackgroundResource(R.color.colorWhite)
} else {
view.setBackgroundResource(R.color.grayBackground)
}
return view
}
How i call it in my code :
adapterScolaire = ListAdapterScolaire(view.context, R.layout.list_adapter, lignesScolaires, resultArgument, true)
And my xml for my item :
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/arret"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:layout_margin="0dp"
android:text="Nom arret"
/>