图片的路径===http://127.0.0.1:8090/image?name=takeout/imgs/seller/3.jpg
HomeRvAdapter.kt加载图片的方法,其中服务端的url是“takeout/imgs/seller/3.jpg”通过拼接“http://127.0.0.1:8090/image?name=”的方法来请求图片
package com.example.takeout.ui.adapter
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RatingBar
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.daimajia.slider.library.SliderLayout
import com.daimajia.slider.library.SliderTypes.TextSliderView
import com.example.takeout.R
import com.heima.takeout.model.beans.Seller
import com.squareup.picasso.Picasso
import org.jetbrains.anko.find
class HomeRvAdapter(val context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
//定义常量
companion object {
val TYPE_TITLE = 0
val TYPE_SELLER = 1
}
val host = "http://127.0.0.1:8090/image?name="
var mDatas: ArrayList<Seller> = ArrayList()
fun setData(data: ArrayList<Seller>) {
this.mDatas = data
notifyDataSetChanged()
}
/**
* 不同position对应不同类型
*/
override fun getItemViewType(position: Int): Int {
if (position == 0) {
return TYPE_TITLE
} else {
return TYPE_SELLER
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val viewType = getItemViewType(position)
when (viewType) {
TYPE_TITLE -> (holder as TitleHolder).bindData("我是title----------------------------------------")
TYPE_SELLER -> (holder as SellerHolder).bindData(mDatas[position - 1])
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
when (viewType) {
TYPE_TITLE -> return TitleHolder(View.inflate(context, R.layout.item_title, null))
TYPE_SELLER -> return SellerHolder(View.inflate(context, R.layout.item_seller, null))
else -> return TitleHolder(View.inflate(context, R.layout.item_home_common, null))
}
}
override fun getItemCount(): Int {
if (mDatas.size > 0) {
return mDatas.size + 1
} else {
return 0
}
}
//内部类,商家的holder
inner class SellerHolder(item: View) : RecyclerView.ViewHolder(item) {
val tvTitle: TextView
val ivLogo: ImageView
val rbScore: RatingBar
val tvSale: TextView
val tvSendPrice: TextView
init {
tvTitle = item.find(R.id.tv_title)
ivLogo = item.find(R.id.seller_logo)
rbScore = item.find(R.id.ratingBar)
tvSale = item.find(R.id.tv_home_sale)
tvSendPrice = item.find(R.id.tv_home_send_price)
}
fun bindData(seller: Seller) {
tvTitle.text = seller.name
//图片路径http://127.0.0.1:8090/image?name=takeout/imgs/seller/3.jpg
println("seller.ensure====" + seller.ensure)
Picasso.with(context).load(host + seller.ensure).into(ivLogo)
rbScore.rating = seller.score.toFloat()
tvSale.text = "月售${seller.sale}单"
tvSendPrice.text = "¥${seller.sendPrice}起送/配送费¥${seller.deliveryFee}"
}
}
//存放图片的url和名称
var url_maps: HashMap<String, Int> = HashMap()
//内部类,title的holder
inner class TitleHolder(item: View) : RecyclerView.ViewHolder(item) {
val sliderLayout: SliderLayout
init {
sliderLayout = item.findViewById(R.id.slider)
}
fun bindData(data: String) {
if (url_maps.size == 0) {
url_maps.put("Hannibal", R.mipmap.pic1);
url_maps.put("Big Bang Theory", R.mipmap.pic2);
url_maps.put("House of Cards", R.mipmap.pic3);
url_maps.put("Game of Thrones", R.mipmap.pic4);
for ((key, value) in url_maps) {
val textSlideView: TextSliderView = TextSliderView(context)
textSlideView.description(key).image(value)
sliderLayout.addSlider(textSlideView)
}
}
}
}
}
ServlertConfig.java中新增配置请求图片的servelet
handler.addServlet(new ServletHolder(new TakeoutImgServlet()), "/takeoutImg");
TakeoutImgServlet.java请求的图片的方法:http://127.0.0.1:8090/image?name=takeout/imgs/seller/3.jpg其中takeout/imgs/seller/3.jpg是url
package cn.ldw.servlet;
import android.os.Environment;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TakeoutImgServlet extends BaseServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setStatus(HttpServletResponse.SC_OK);
String name = req.getParameter("name");
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "Apaches/" + name;
File file = new File(path);
long length = file.length();
resp.setContentLength((int) length);
OutputStream out = resp.getOutputStream();
FileInputStream stream = new FileInputStream(file);
int count = -1;
byte[] buffer = new byte[1024];
while ((count = stream.read(buffer)) != -1) {
out.write(buffer, 0, count);
out.flush();
}
stream.close();
out.close();
}
}
效果图如下: