问题描述
我正在将Retrofit和Glide用于简单的Image和textView。在我的“描述”片段中,我想将图像从api显示到textView下方的ImageView中。我的recyclerView图像工作正常,但是如何将来自Retrofit / api的单个图像显示到这个Description类中。
我添加了
val imageView:ImageView = view!!.findViewById(R.id.item_image)
但是我在此之后迷路了。
这是带有textview的片段,我想将ImageView放在文本描述下
class DescriptionFragment : Fragment() {
companion object {
fun newInstance() = DescriptionFragment()
}
override fun onCreateView(
inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?
): View {
return inflater.inflate(R.layout.main_fragment,container,false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val textView:TextView = view!!.findViewById(R.id.message)
val imageView:ImageView = view!!.findViewById(R.id.item_image)
// Get the arguments from the caller fragment/activity
val description = arguments?.getString("description")
description?.let {
textView.text = "Description : \n$description"
}
}
}
这是我想将图像加载到的xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.DescriptionFragment">
<ImageView
android:id="@+id/item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="41dp"
android:text="@string/full_repo_description"
app:layout_constraintBottom_toTopOf="@+id/message"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_margin="36dp"
android:text="DescriptionFragment"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是我从中获取avatar_url的数据类
data class owner(var avatar_url:String);
data class Items(var id: String,var name: String,var full_name: String,var description: String,var owner: owner)
这是我当前可以正常使用的回收站类
class RecyclerAdapter(private val context: Context) : RecyclerView.Adapter<RecyclerAdapter.MyViewHolder>() {
var itemList: List<Items> = listof()
override fun onCreateViewHolder(parent: ViewGroup,viewType: Int): MyViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.card_layout,parent,false)
return MyViewHolder(view)
}
override fun getItemCount(): Int {
return itemList.size
}
override fun onBindViewHolder(holder: MyViewHolder,position: Int) {
holder.repoId.text = itemList[position].id
holder.repoName.text = itemList[position].name
holder.repoFullName.text = itemList[position].full_name
Glide.with(context).load(itemList[position].owner.avatar_url)
.apply(RequestOptions().centerCrop())
.into(holder.repoImage)
holder.itemView.setonClickListener {
val intent = Intent(context,DescriptionActivity::class.java)
// To pass any data to next activity
intent.putExtra("description",itemList[position].description)
// start your next activity
context.startActivity(intent)
// Toast.makeText(context,itemList.get(position).id,Toast.LENGTH_SHORT).show()
}
}
fun setItemsList(itemList: List<Items>) {
this.itemList = itemList;
notifyDataSetChanged()
}
class MyViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {
val repoId: TextView = itemView!!.findViewById(R.id.item_id)
val repoName: TextView = itemView!!.findViewById(R.id.item_name)
val repoFullName: TextView = itemView!!.findViewById(R.id.item_fullname)
val repoImage: ImageView = itemView!!.findViewById(R.id.item_image)
}
}
这是我当前的mainActivity
class MainActivity : AppCompatActivity() {
lateinit var recyclerView: RecyclerView
lateinit var recyclerAdapter: RecyclerAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerView = findViewById(R.id.recyclerview)
recyclerAdapter = RecyclerAdapter(this)
recyclerview.layoutManager = linearlayoutmanager(this)
recyclerView.adapter = recyclerAdapter
val apiInterface = ApiInterface.create().getItems()
apiInterface.enqueue(object : Callback<List<Items>> {
override fun onResponse(call: Call<List<Items>>?,response: Response<List<Items>>?) {
if (response?.body() != null) {
Log.d("myTest",response.body().toString());
recyclerAdapter.setItemsList(response.body()!!)
}
}
override fun onFailure(call: Call<List<Items>>?,t: Throwable?) {
}
})
}
}
以及我通过翻新获取网址的方式
interface ApiInterface {
@GET("repos")
fun getItems() : Call<List<Items>>
companion object {
var BASE_URL = "https://api.github.com/orgs/square/"
fun create() : ApiInterface {
val retrofit = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.build()
return retrofit.create(ApiInterface::class.java)
}
}
}
解决方法
我强烈建议您使用数据绑定并添加绑定适配器以使用Glide加载图像
[更新2] 要动态显示图像(在这种情况下是从网络加载)时,请考虑对Imageview使用固定值大小(宽度和高度)
[更新]
仔细查看您的RecyclerAdapter类。您必须使用以下代码段将imageUrl放入Intent中:
intent.putExtra("imageUrl",itemList[position].owner.avatar_url)
然后在您的Description {Fragment / Activity}中,您必须获取该网址,然后让滑行完成其余操作:
val imageUrl = arguments?.getString("imageUrl")
imageUrl?.let { loadImageViaUrl(imageview,it) }
并如下定义loadImageViaUrl:
private fun loadImageViaUrl(view: ImageView,url: String) {
val options = RequestOptions()
.placeholder(R.drawable.network_image_placeholder) // use any image as placeholder
Glide.with(view.context)
.setDefaultRequestOptions(options)
.load(url)
.into(view)
}
这是使用Glide库的最基本方法。