前言:有新闻了怎么能少得了美女,我们使用Gank的接口实现一个美女列表,封装我们的图片 加载库Picasso,使用起来更加的方便,很值得期待!!!
此篇文章完成后效果展示:
1.编写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/mGridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"/>
</LinearLayout>
2.编写Kotlin交互代码:
package com.zrc.smartbutler.fragment
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.kymjs.rxvolley.RxVolley
import com.kymjs.rxvolley.client.HttpCallback
import com.zrc.smartbutler.R
import com.zrc.smartbutler.adapter.GridAdapter
import com.zrc.smartbutler.entity.GirlData
import com.zrc.smartbutler.utils.L
import com.zrc.smartbutler.utils.StaticClass
import kotlinx.android.synthetic.main.fragment_girl.*
import kotlinx.android.synthetic.main.fragment_girl.view.*
import org.json.JSONException
import org.json.JSONObject
/**
*项目名: SmartButler
*包名: com.zrc.smartbutler.fragment
*文件名: GirlFragment
*创建者: 张如成
*创建时间: 2020/5/6 9:12
*描述: 美女社区
*/
class GirlFragment: Fragment() {
/**
* 1.监听点击事件
* 2.提示框
* 3.加载图片
* 4.PhotoView
*/
private val mListUrl = ArrayList<GirlData>()
private var mAdapter:GridAdapter? = null
//提示框
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View = inflater.inflate(R.layout.fragment_girl,null)
findView(view)
return view
}
//初始化View
private fun findView(view: View) {
//解析
RxVolley.get(StaticClass().GIRL_URL,object :HttpCallback(){
override fun onSuccess(t: String?) {
L().i(t)
parsingJson(t!!,view)
}
})
//监听点击事件
mGridView.setOnClickListener {
}
}
//解析Json
private fun parsingJson(t: String,view: View) {
try {
val jsonObject = JSONObject(t)
val jsonArray = jsonObject.getJSONArray("results")
for (i in 0 until jsonArray.length()) {
val json = jsonArray[i] as JSONObject
val url = json.getString("url")
val data = GirlData(url)
mListUrl.add(data)
}
mAdapter = GridAdapter(activity!!,R.layout.girl_item,mListUrl)
//设置适配器
view.mGridView.adapter = mAdapter
} catch (e: JSONException) {
e.printStackTrace()
}
}
}
3.适配器
package com.zrc.smartbutler.adapter
import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.ArrayAdapter
import android.widget.ImageView
import android.widget.TextView
import com.squareup.picasso.Picasso
import com.zrc.smartbutler.R
import com.zrc.smartbutler.entity.GirlData
import com.zrc.smartbutler.entity.WeChatData
import com.zrc.smartbutler.utils.L
import com.zrc.smartbutler.utils.PicassoUtils
/**
*项目名: SmartButler
*包名: com.zrc.smartbutler.adapter
*文件名: GridAdapter
*创建者: 张如成
*创建时间: 2020/5/16 11:05
*描述: 妹子适配器
*/
class GridAdapter (activity: Activity, val resourceId:Int, data:ArrayList<GirlData>) :ArrayAdapter<GirlData>(activity,resourceId,data){
var width:Int = 0
var height:Int = 0
var wm:WindowManager?=null
//获取屏幕的宽
init {
wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
width = wm!!.defaultDisplay.width
height = wm!!.defaultDisplay.height
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val view: View
val viewHolder:ViewHolder
if(convertView==null){
view = LayoutInflater.from(context).inflate(resourceId,parent,false)
val iv_img: ImageView = view.findViewById(R.id.imageView)
viewHolder = ViewHolder(iv_img)
view.tag = viewHolder
}else{
view = convertView
viewHolder = view.tag as ViewHolder
}
val fruit = getItem(position)
if(fruit!=null){
//解析图片
PicassoUtils().loadImageViewSize(context,fruit.imgUrl,width/2,height/5,viewHolder.imageView)
}
return view
}
inner class ViewHolder(val imageView: ImageView)
}
至此,美女图片加载实现!!!