Follow Android 007, get a full set of Free Android development learning materials

What is a ListView

ListView is a UI control for displaying lists. But the current mainstream is RecyclerView, more powerful, better use.

Based on the sample

rendering

Brief Introduction of the scheme

  1. Add to the activity’s corresponding layout fileListView
<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
Copy the code
  1. Add a new Adapter to display each row in the list (including the corresponding layout file)

See the ListViewAdapter and its layout file in the complete code below. Instantiate the Adapter in the Activity, set the data, and set the Adapter to the ListView

private fun initListView(a) {
    var dataList = getData()
    val adapter = ListViewAdapter(this)
    adapter.setData(dataList)
    listView.adapter = adapter
    // Hide the built-in delimiter and customize the delimiter in the layout corresponding to the ListViewAdapter for more flexibility.
    listView.divider = null
    // Add click events
    listView.setOnItemClickListener { adapterView, _, position, _ ->
        val text = adapterView.getItemAtPosition(position).toString()
        toast(text)
    }
}

private fun getData(a): List<String> {
    val dataList = ArrayList<String>()
    for (index in 0 until 100) {
        val text = "Data$index "
        dataList.add(text)
    }
    return dataList
}
Copy the code

The complete code

  1. The activity code: MainActivity
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?). {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initListView()
    }

    private fun initListView(a) {
        var dataList = getData()
        val adapter = ListViewAdapter(this)
        adapter.setData(dataList)
        listView.adapter = adapter
        // Hide the built-in delimiter and customize the delimiter in the layout corresponding to the ListViewAdapter for more flexibility.
        listView.divider = null
        // Add click events
        listView.setOnItemClickListener { adapterView, _, position, _ ->
            val text = adapterView.getItemAtPosition(position).toString()
            toast(text)
        }
    }

    private fun getData(a): List<String> {
        val dataList = ArrayList<String>()
        for (index in 0 until 100) {
            val text = "Data$index "
            dataList.add(text)
        }
        return dataList
    }

    /** * displays toast brief prompt */
    private fun toast(text: String) = Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
}
Copy the code
  1. MainActivity corresponds to the layout file activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
Copy the code
  1. ListView corresponds to the Adapter: ListViewAdapter
class ListViewAdapter(var context: Context) : BaseAdapter() {
    private var mDataList = mutableListOf<String>()

    /** * set data */
    fun setData(dataList: List<String>) {
        mDataList.clear()
        mDataList.addAll(dataList)
        // After data changes, notifyDataSetChanged needs to be called.
        notifyDataSetChanged()
    }

    override fun getView(position: Int, convertView: View? , viewGroup:ViewGroup?).: View {
        var viewHolder: ViewHolder
        var view: View
        // Consider convertView in order to reuse existing views and improve efficiency.
        if (convertView == null) {
            view = View.inflate(context, R.layout.item_layout, null);
            viewHolder = ViewHolder(view)
            view.tag = viewHolder
        } else {
            view = convertView
            viewHolder = view.tag as ViewHolder
        }
        // Get the current location data
        val item = getItem(position)
        // Display the user name
        if (item is String) {
            viewHolder.nameTv.text = item
        }
        return view
    }

    override fun getItem(position: Int): Any {
        return mDataList[position]
    }

    override fun getItemId(position: Int): Long {
        return 0
    }

    override fun getCount(a): Int {
        return mDataList.size
    }

    class ViewHolder(view: View) {
        var nameTv: TextView = view.findViewById(R.id.nameTv)
    }
}
Copy the code
  1. The ListViewAdapter corresponds to the layout file: item_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/nameTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        tools:text="Name" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginStart="15dp"
        android:layout_marginEnd="15dp"
        android:layout_marginTop="5dp"
        android:background="#E7E7E7" />
</LinearLayout>
Copy the code

Basic sample complete source code

Gitee.com/cxyzy1/List…


Android development tutorial series summary

Development language learning

Kotlin language basics

UI control learning series

UI control _TextView UI control _EditText UI control _Button UI control _ImageView UI control _RadioButton UI control _CheckBox UI control _ProgressBar

Follow the headlines to get the latest articles: