// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
A simple [Fragment] subclass.
*/
class TestListFragment : ListFragment() {
var textView: TextView? = null
var list = arrayOf("항목1", "항목2","항목3","항목4","항목5")
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
var view = inflater!!.inflate(R.layout.fragment_test_list, container, false)
textView = view.findViewById(R.id.textView)
var adapter = ArrayAdapter<String>(
activity. android.R.layout.simple_list_item_1, list //오류
)
listAdapter = adapter
return view
Frag 내에 ListView를 사용할 경우 편리
id가 @android:id/list (안드로이드 OS가 알고있는 내용)
Main Activity
class MainActivity : AppCompatActivity() {
}
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:id="@+id/container">
import android.os.Bundle import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ArrayAdapter import android.widget.ListView import android.widget.TextView import androidx.fragment.app.ListFragment
// TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private const val ARG_PARAM1 = "param1" private const val ARG_PARAM2 = "param2"
/**
*/ class TestListFragment : ListFragment() {
var textView: TextView? = null var list = arrayOf("항목1", "항목2","항목3","항목4","항목5")
override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { // Inflate the layout for this fragment var view = inflater!!.inflate(R.layout.fragment_test_list, container, false) textView = view.findViewById(R.id.textView)
}
override fun onListItemClick(l: ListView, v: View, position: Int, id: Long) { super.onListItemClick(l, v, position, id)
}
}
<?xml version="1.0" encoding="utf-8"?> <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="match_parent" android:orientation="vertical" tools:context=".TestListFragment">