0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Kotlin/Android】カスタムビューを作成する

Posted at

書いてあること

  • Kotlinでグリッドビューの中に入れる、カスタムビューを書いてみました。

用意するもの

  1. Fragment.java
  2. Adapter.java
  3. CustomView.java
  4. fragment_main.xml
  5. item_fragment.xml

1. Fragment.java

Fragment.java

/**
 * 温度調整Fragment画面クラス
 */
class AdjustTempFragment : Fragment() {
    /**
     * グリッドビュー
     */
    private lateinit var gridView: GridView
    /**
     * なにかしらの情報保持配列
     */
    private lateinit var values: Array<HogeEnum>
    /**
     * グリッドアダプター
     */
    private lateinit var gridAdapter: GridAdapter

    /**
     * コンパニオンオブジェクト
     */
    companion object {
        /**
         * コンテキスト
         */
        lateinit var context: Context

        /**
         * インスタンス生成メソッド
         */
        fun createInstance(context: Context): Fragment {
            val fragment = Fragment()
            this.context = context
            return fragment
        }
    }

    /**
     * 初期化メソッド
     */
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        return inflater.inflate(R.layout.fragment_main, container, false)
    }

    /**
     * メインメソッド
     * ・ビューの描画処理
     * ・設定画面遷移処理
     */
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //カスタムビューを描画
        this.gridView = view.findViewById(R.id.grid)
        this.values = HogeEnum.values()
        this.adapter = Adapter(this.context, this.values)
        this.gridView.adapter = this.adapter
        //設定画面へ遷移
        val image: ImageView = view.findViewById(R.id.setting_button)
        image.setOnClickListener {
            val intent = Intent(activity, SettingActivity::class.java)
            startActivity(intent)
        }
    }
}  

2. Adapter.java

Adapter.java
class Adapter : BaseAdapter {
    override fun getItemId(p0: Int): Long {
        return p0.toLong()
    }
    /**
     * コンテキスト
     */
    private lateinit var context: Context
    /**
     * なにかしらの情報保持配列
     */
    private var values: Array<HogeEnum>
    /**
     * コンストラクタ
     */
    constructor(con: Context?) {
        if (con != null) {
            this.context = con
        }
    }
    /**
     * 配列の要素数
     */
    override fun getCount(): Int {
        return this.values.size
    }
    /**
     * 配列の要素
     */
    override fun getItem(p0: Int): Any {
        return this.values[p0]
    }
    /**
     * ビュー描画メソッド
     */
    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
    }
}
/**
 * ビューホルダークラス
 */
class ViewHolder {
    lateinit var customView: CustomView
}

3. CustomView.java

CustomView.java
class CustomView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    /**
     * 初期化メソッド
     */
    @SuppressLint("ResourceAsColor")
    fun initView() {
        var lp = LayoutParams(200, 200)
        var imageView = ImageView(context)
        imageView.layoutParams = lp
        this.addView(imageView)
    }
}

4. 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>

5. 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>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?