更换界面
前面的列表只是显示了数据,太难看了,现在来优化一下
创建adapter的item布局文件item_forecast.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
android:background="?attr/selectableItemBackground"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="48dp"
android:layout_height="48dp"
tools:src="@mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:orientation="vertical">
<TextView
android:id="@+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
tools:text="May 14, 2015"/>
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Caption"
tools:text="Light Rain"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="@+id/maxTemperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
tools:text="30"/>
<TextView
android:id="@+id/minTemperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Caption"
tools:text="15"/>
</LinearLayout>
</LinearLayout>
每一个item需要显示小图标,所以,需要在data类中加入图标url:
data class Forecast(val date: String, val description: String, val high: Int, val low: Int,val iconUrl: String)
更改ForecastDataMapper.kt
private fun convertForecastItemToDomain(forecast: Forecast): ModelForecast {
return ModelForecast(convertDate(forecast.dt), forecast.weather[0].description, forecast.temp.max.toInt(), forecast.temp.min.toInt(), generateIconUrl(forecast.weather[0].icon))
}
private fun generateIconUrl(iconCode: String): String = "http://openweathermap.org/img/w/$iconCode.png"
给天气列表item加上点击事件:
public interface OnItemClickListener {
operator fun invoke(forecast: Forecast)
}
提一下listener的调用,当方法名为invoke时,可以直接这样写
temClick.invoke(forecast)
itemClick(forecast)
在看看更改过的adapter:
class ForecastListAdapter(val weekForecast: ForecastList, val itemClick: OnItemClickListener) : RecyclerView.Adapter<ForecastListAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.ctx).inflate(R.layout.item_forecast, parent, false)
return ViewHolder(view, itemClick)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bindForecast(weekForecast[position])
}
override fun getItemCount(): Int = weekForecast.size()
class ViewHolder(val view: View, val itemClick: OnItemClickListener) : RecyclerView.ViewHolder(view) {
private val iconView: ImageView
private val dateView: TextView
private val descriptionView: TextView
private val maxTemperatureView: TextView
private val minTemperatureView: TextView
init {
iconView = view.find(R.id.icon)
dateView = view.find(R.id.date)
descriptionView = view.find(R.id.description)
maxTemperatureView = view.find(R.id.maxTemperature)
minTemperatureView = view.find(R.id.minTemperature)
}
fun bindForecast(forecast: Forecast) {
with(forecast) {
Picasso.get().load(iconUrl).into(iconView)
dateView.text = date
descriptionView.text = description
maxTemperatureView.text = "${high.toString()}"
minTemperatureView.text = "${low.toString()}"
itemView.setOnClickListener { itemClick(forecast) }
}
}
}
}
最后就是调用了,在MainActivity中:
doAsync() {
val result = RequestForecastCommand("94043").execute()
uiThread {
forecastList.adapter = ForecastListAdapter(result,
object : OnItemClickListener{
override fun invoke(forecast: Forecast) {
toast(forecast.date)
}
})
}
}
网友评论