--I wrote a custom view to put in the grid view with Kotlin.
Fragment.java
Adapter.java
CustomView.java
fragment_main.xml
item_fragment.xml
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)
        }
    }
}  
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
}
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)
    }
}
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>
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