如何根据位置项kotlin在列表视图中显示特定的文本视图

时间:2018-11-18 07:26:04

标签: android listview kotlin

我是的新手,并且构建了一个简单的应用,该应用可以显示通过JSON数据安排的航班。我使用了ListView,一切正常。但我想在TextView中添加ListView。例如,当用户向下滚动ListView时,我想根据在TextView中的位置显示ListView

例如,此ListViewListView的中间显示日期

image

那么我可以根据位置选择我要在ListView中显示的元素的位置吗?

例如,在项目位置36之后,我显示了所需的元素。

override fun getCount(): Int {
    return 36
}

我为ListAdapter编写的代码

class ListAdapteDep (val context: MainActivity, val list: ArrayList<FlightShdu>): BaseAdapter() {
    @SuppressLint("ViewHolder", "NewApi")
    override fun getView(p0: Int, convertView: View?, parent: ViewGroup?): View {

        val view : View = LayoutInflater.from(context).inflate(R.layout.dep_list,parent,false)
        val list = list[p0]
        val LogoAriline = view.findViewById(R.id.logo_image) as ImageView
        val status = view.findViewById(R.id.status_id) as AppCompatTextView
        val Airport = view.findViewById(R.id.airportid) as AppCompatTextView
        val code = view.findViewById(R.id.code_id) as AppCompatTextView
        val TimeFlight = view.findViewById(R.id.time_id) as AppCompatTextView
        view.callsign_id.text=list.Callsign
        view.airline_id.text=list.Airline
        code.text = list.code
        view.status_id.text=list.Stauts
        status.text= list.Stauts
        TimeFlight.text = getDateTime(list.TimeFlight)


        Airport.text= list.Airport
        view.model_id.text=list.Model

        //the text view l want to show depending on postion l want in item count

        val date = view.findViewById(R.id.time_id) as AppCompatTextView

        date.text = getDateTime(list.date)




        return view
    }

    private fun getDateTime(s: String): String? {
        try {
            val sdf = SimpleDateFormat("KK:mm a")
            val netDate = Date(s.toLong() * 1000)
            return sdf.format(netDate)
        } catch (e: Exception) {
            return e.toString()
        }
    }

    override fun getItem(p0: Int): Any {
        return list [p0]
    }

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

    override fun getCount(): Int {
        return 36
    }    
}

1 个答案:

答案 0 :(得分:1)

您可以使用getView方法检查位置,然后可以根据当前位置动态渲染视图。