在Kotlin中创建自定义微调框适配器

时间:2019-02-05 16:58:49

标签: android-studio kotlin

我是Kotlin和android逻辑的新手 我想做的是用自定义适配器制作微调器,以显示带有如下文本的图像 the result i want 但是我有很多未解决的参考资料,所以我认为我做错了什么

我有两个片段,微调器位于第二个片段内 custome_spinner.xml是自定义布局文件

android studio 3.1.2

kotlin_version ='1.2.30'

gradle:3.1.2

 customeSpinnerAdapter.kt
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter

class customeSpinnerAdapter : BaseAdapter{
    override fun getItem(p0: Int): Any {
        return flag.length
    }

    override fun getItemId(p0: Int): Long {
        return null
    }

    override fun getCount(): Int {
        return 0
    }

    val country:String
    val flag:String
     fun customeSpinnerAdapter(context:Context,name:String,image:String){
        this.country = name
        this.flag = image

        val inflater  = LayoutInflater.from(context)
    }
    override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
        val view = inflater.inflate(R.layout.custome_spinner)
        spinner_image.setImageResource(flag)
        spinner_country.setText(country)
        return view
    }
}
custome_spinner.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="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/spinner_image"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:padding="5dp"
        android:src="@drawable/us" />

    <TextView
        android:id="@+id/spinner_country"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:layout_gravity="center"
        android:text="Custom Text"
        android:textColor="#000" />

</LinearLayout>
 the widget inside fragment_second.xml 

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="368dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="12dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/from_textView" />

1 个答案:

答案 0 :(得分:0)

您的问题出在customeSpinnerAdapter.kt

  customeSpinnerAdapter.kt
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter

class customeSpinnerAdapter : BaseAdapter{
    override fun getItem(p0: Int): Any {
        return flag.length
    }

    override fun getItemId(p0: Int): Long {
        return null
    }

    override fun getCount(): Int {
        return 0
    }

    val country:String
    val flag:String
    //Add this
    val inflater : LayoutInflater

    fun customeSpinnerAdapter(context:Context,name:String,image:String){
        this.country = name
        this.flag = image

        //val inflater  = LayoutInflater.from(context)
        inflater  = LayoutInflater.from(context)
    }
    override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
        val view = inflater.inflate(R.layout.custome_spinner)
        spinner_image.setImageResource(flag)
        spinner_country.setText(country)
        return view
    }
}