Implement Adapter and ViewHolder
Together, these two classes define how data is displayed. The ViewHolder is the wrapper for the View of each element in the list. The Adapter creates the ViewHolder as needed and sets up the data office for these views, a process known as “binding.” To define the Adapter, you need to override the following three methods.
- OnCreateViewHolder: This method is called whenever RecyclerView needs to create a new ViewHolder. This method creates and initializes the ViewHolder and its associated View, but does not populate the data.
- OnBindViewHolder: RecyclerView invoicates this method to associate a ViewHolder with data.
- GetItemCount: RecyclerView will invoke this method to obtain the size of the data set.
class CustomAdapter(private val dataSet: Array<String? >) : recyclerVievie.adapter < customAdapter.viewholder >() {// Create ItemViewHolder 2021/7/5 class ViewHolder(view: view) : RecyclerView.ViewHolder(view) { val textView: TextView init { textView = view.findViewById(R.id.textView) } } // Create new views (invoked by the layout manager) override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder { // Create a new view, which defines the UI of the list item val view = LayoutInflater.from(viewGroup.context) .inflate(R.layout.text_row_item, viewGroup, false) return ViewHolder(view) } // Replace the contents of a view (invoked by the layout manager) override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) { // Get element from your dataset at this position and replace the // contents of the view with that element viewHolder.textView.text = dataSet[position] } // Return the size of your dataset (invoked by the layout manager) override fun getItemCount() = dataSet.size }Copy the code
Here is an example of a layout file corresponding to a ViewHolder:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:gravity="center_vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="element_text"/>
</FrameLayout>
Copy the code
The following is an example of an application in an Activity
class MainActivity : AppCompatActivity() { lateinit var recyclerview:RecyclerView protected var mRecyclerView: RecyclerView? = null protected var mAdapter: CustomAdapter? = null protected var mLayoutManager: RecyclerView.LayoutManager? = null lateinit var mDataset: Array<String? > private enum class LayoutManagerType { GRID_LAYOUT_MANAGER, LINEAR_LAYOUT_MANAGER } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) initdataset() mRecyclerView = findViewById(R.id.recyclerview) mLayoutManager = GridLayoutManager(this,2) mAdapter = CustomAdapter(mDataset) mRecyclerView? .adapter = mAdapter mRecyclerView? .layoutManager = mLayoutManager } fun initdataset(){ mDataset = arrayOfNulls(100) for (i in 0 until 100) { mDataset[i] = "This is element #$i" } } }Copy the code