[Kotlin / Android] Create a custom view

What is written

--I wrote a custom view to put in the grid view with Kotlin.

What to prepare

  1. Fragment.java

  2. Adapter.java

  3. CustomView.java

  4. fragment_main.xml

  5. item_fragment.xml

  6. Fragment.java

Fragment.java



/**
 *Temperature control Fragment screen class
 */
class AdjustTempFragment : Fragment() {
    /**
     *Grid view
     */
    private lateinit var gridView: GridView
    /**
     *Some kind of information retention array
     */
    private lateinit var values: Array<HogeEnum>
    /**
     *Grid adapter
     */
    private lateinit var gridAdapter: GridAdapter

    /**
     *Companion object
     */
    companion object {
        /**
         *context
         */
        lateinit var context: Context

        /**
         *Instance generation method
         */
        fun createInstance(context: Context): Fragment {
            val fragment = Fragment()
            this.context = context
            return fragment
        }
    }

    /**
     *Initialization method
     */
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        return inflater.inflate(R.layout.fragment_main, container, false)
    }

    /**
     *Main method
     *-View drawing process
     *・ Setting screen transition processing
     */
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //Draw custom view
        this.gridView = view.findViewById(R.id.grid)
        this.values = HogeEnum.values()
        this.adapter = Adapter(this.context, this.values)
        this.gridView.adapter = this.adapter
        //Transition to the setting screen
        val image: ImageView = view.findViewById(R.id.setting_button)
        image.setOnClickListener {
            val intent = Intent(activity, SettingActivity::class.java)
            startActivity(intent)
        }
    }
}  
  1. Adapter.java

Adapter.java


class Adapter : BaseAdapter {
    override fun getItemId(p0: Int): Long {
        return p0.toLong()
    }
    /**
     *context
     */
    private lateinit var context: Context
    /**
     *Some kind of information retention array
     */
    private var values: Array<HogeEnum>
    /**
     *constructor
     */
    constructor(con: Context?) {
        if (con != null) {
            this.context = con
        }
    }
    /**
     *Number of elements in the array
     */
    override fun getCount(): Int {
        return this.values.size
    }
    /**
     *Array elements
     */
    override fun getItem(p0: Int): Any {
        return this.values[p0]
    }
    /**
     *View drawing method
     */
    override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
        val itemLayoutView = p1 ?: View.inflate(this.context, R.layout.item_fragment, null)
        var viewHolder = itemLayoutView.tag
        if (viewHolder == null) {
            viewHolder = ViewHolder()
            viewHolder.customView = itemLayoutView.custom_view as CustomView
            (itemLayoutView.custom_view as CustomView).initView(
                this.values[p0].index()
            )
            itemLayoutView.tag = viewHolder
        }
        return itemLayoutView
    }
}
/**
 *Viewholder class
 */
class ViewHolder {
    lateinit var customView: CustomView
}
  1. CustomView.java

CustomView.java


class CustomView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    /**
     *Initialization method
     */
    @SuppressLint("ResourceAsColor")
    fun initView() {
        var lp = LayoutParams(200, 200)
        var imageView = ImageView(context)
        imageView.layoutParams = lp
        this.addView(imageView)
    }
}
  1. fragment_main.xml

fragment_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
        <GridView
            android:id="@+id/grid"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:numColumns="3">
        </GridView>
</LinearLayout>
  1. item_fragment.xml

item_fragment.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <jp.co.hoge.project.views.CustomView
        android:id="@+id/custom_view"
        android:layout_width="200dp"
        android:layout_height="200dp">
    </jp.co.hoge.project.views.CustomView>
</LinearLayout>

Recommended Posts

[Kotlin / Android] Create a custom view
Let's create a custom tab view in SwiftUI 2.0
Android ripple effect (Custom View)
[Android] Create a calendar using GridView
Android view
[Android] Create a square view while maintaining the aspect ratio: SquareLayout
Docker Compact Manual (4: Create a custom image)
[Android] Create a sliding menu without using NavigationView
[Android] Inherit ImageView to create a new class
[Android / Kotlin] UI summary
[Java] Create a filter
Create a Chat app with WebSocket (Tyrus) + libGDX + Kotlin
[Android / Kotlin] Detailed notes 2
[Kotlin / Android] Listener rewrite
Customize list view on Android
Creating a Scala custom ExecutionContext
Create a java method [Memo] [java11]
[Java] Create a temporary file
Create a VS Code Plugin.
Create a fortune using Ruby
How to create a method
Create a name input function
Create a clear time ranking in Firebase's Realtime Database (Android app)
I tried to create a simple map app in Android Studio
A newcomer tries to summarize the Android view (beginner Android application development)
Fall when animating a View containing an Android Recycler View with Transition
Determining if a custom keyboard is enabled in Android Studio (Java)