如何将 Retrofit 中的信息实现到 viewPager2 布局小部件中
Posted
技术标签:
【中文标题】如何将 Retrofit 中的信息实现到 viewPager2 布局小部件中【英文标题】:How to implement information from Retrofit into a viewPager2 layout widgets 【发布时间】:2021-07-23 23:34:21 【问题描述】:所以我正在构建这个 Carousel 项目,该项目在应用程序上显示图像和一些文本。我已经通过构建原型得到了结构。我也可以使用 Retrofit 从我的终端上的 RestAPI 调用信息。但是我真正坚持的地方是试图让信息显示在容器中,我希望它显示在哪里。我尝试了很多方法来接近它,但无济于事。下面是我现在拥有的代码,它显示了我放入其中的图像和文本。随着改造类。我已经做了三天了,我不知道该怎么办。如果有人有想法或可以提供帮助,我将非常感激。
API(改造)
package com.examples.carousel.api
import com.examples.carousel.CarouselItem
import com.examples.carousel.utli.Constants.Companion.BASE_URL
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
interface FlickrApi
@GET("services/rest/?method=flickr.interestingness.getList" +
"&api_key=(private)" +
"&format=json" +
"&nojsoncallback=1" +
"&extras=url_s")
fun getCarouselItem() : Call<List<CarouselItem>>
companion object
fun create() : FlickrApi
val retrofit = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.build()
return retrofit.create(FlickrApi::class.java)
模型(注释掉的代码是我想要从 api 获得的信息)
package com.examples.carousel
data class CarouselItem internal constructor(
// var title: String,
// var id: String,
// var url_s: String
var image: Int,
var title2: String
)
适配器
package com.examples.carousel
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.bumptech.glide.Glide
import com.makeramen.roundedimageview.RoundedImageView
class ItemAdapter internal constructor(carouselItems: MutableList<CarouselItem>, viewPager2: ViewPager2) : RecyclerView.Adapter<ItemAdapter.ItemPagerViewHolder>()
private val carouselItems: List<CarouselItem>
private val viewPager2: ViewPager2
init
this.carouselItems = carouselItems
this.viewPager2 = viewPager2
class ItemPagerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
private val photo: RoundedImageView = itemView.findViewById(R.id.picture)
private val title: TextView = itemView.findViewById(R.id.photo_name)
fun image(carouselItem: CarouselItem)
photo.setImageResource(carouselItem.image)
fun text(carouselItem: CarouselItem)
title.text = carouselItem.title2
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemPagerViewHolder
return ItemPagerViewHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.carousel_item_container,
parent,
false
)
)
override fun onBindViewHolder(holder: ItemPagerViewHolder, position: Int)
holder.image(carouselItems[position])
holder.text(carouselItems[position])
if (position == carouselItems.size - 2)
viewPager2.post(runnable)
override fun getItemCount(): Int
return carouselItems.size
private val runnable = Runnable
carouselItems.addAll(carouselItems)
notifyDataSetChanged()
主活动
package com.examples.carousel
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.CompositePageTransformer
import androidx.viewpager2.widget.MarginPageTransformer
import androidx.viewpager2.widget.ViewPager2
import com.examples.carousel.api.FlickrApi
import com.makeramen.roundedimageview.RoundedImageView
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import kotlin.math.abs
class CarouselActivity : AppCompatActivity()
lateinit var viewPager: ViewPager2
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_carousel)
viewPager = findViewById(R.id.viewPager_ImageSlider)
supportActionBar?.hide()
val multipleItems: MutableList<CarouselItem> = ArrayList()
multipleItems.add(CarouselItem(R.drawable.bcw_38,"Image 1"))
multipleItems.add(CarouselItem(R.drawable.bcw_39,"Image 2"))
multipleItems.add(CarouselItem(R.drawable.bcw_40,"Image 3"))
multipleItems.add(CarouselItem(R.drawable.bcw_45,"Image 4"))
multipleItems.add(CarouselItem(R.drawable.bcw_50,"Image 5"))
multipleItems.add(CarouselItem(R.drawable.bcw_53,"Image 6"))
viewPager.adapter = ItemAdapter(multipleItems,viewPager)
viewPager.clipToPadding = false
viewPager.clipChildren = false
viewPager.offscreenPageLimit = 3
viewPager.getChildAt(0).overScrollMode = RecyclerView.OVER_SCROLL_NEVER
val compositePageTransformer = CompositePageTransformer()
compositePageTransformer.addTransformer(MarginPageTransformer(30))
compositePageTransformer.addTransformer page, position ->
val r = 1 - abs(position)
page.scaleY = 0.85f + r * 0.25f
viewPager.setPageTransformer(compositePageTransformer)
//
// val apiInterface = FlickrApi.create().getCarouselItem()
//
// apiInterface.enqueue(object: Callback<List<CarouselItem>>
// override fun onFailure(call: Call<List<CarouselItem>>, t: Throwable)
//
//
//
// override fun onResponse(call: Call<List<CarouselItem>>, response: Response<List<CarouselItem>>)
//
// if (response.body() != null)
// itemAdapter.setItemListItems(response.body()!!)
//
//
//
// )
XML 文件:
ViewPager2 布局
<?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:layout_
android:layout_ android:background="#232323"
android:id="@+id/linearLayout">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager_ImageSlider"
android:layout_
android:layout_
android:paddingStart="70dp"
android:paddingEnd="70dp"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent" android:layout_marginTop="80dp"/>
<TextView
android:text="@string/osa_playlist"
android:layout_
android:layout_ android:id="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="40dp"
android:textColor="@color/white" android:textSize="25sp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
容器
<?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:layout_
android:layout_ android:id="@+id/container" android:background="#232323">
<com.makeramen.roundedimageview.RoundedImageView
android:layout_
android:layout_ app:layout_constraintTop_toTopOf="parent" android:id="@+id/picture"
android:adjustViewBounds="true"
app:riv_corner_radius="12dp" tools:layout_editor_absoluteX="0dp"/>
<TextView
android:layout_
android:layout_ android:id="@+id/photo_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="8dp"
android:layout_marginStart="8dp" android:layout_marginRight="8dp" android:layout_marginEnd="8dp"
android:textStyle="bold"
tools:text="TEXT TEXT" android:textSize="36sp" android:textColor="@color/white" android:gravity="center"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintTop_toBottomOf="@+id/picture" app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="230dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
依赖关系
dependencies
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
//For the Carousel Container
implementation 'androidx.viewpager2:viewpager2:1.0.0'
implementation 'com.makeramen:roundedimageview:2.3.0'
//For Retrofit and Gson
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
// implementation 'com.squareup.retrofit2:converter-scalars:2.5.0'
// implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.okhttp3:okhttp:4.9.0'
testImplementation 'junit:junit:4.13.2'
// Coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
*它不是互联网权限,它已经存在。
【问题讨论】:
【参考方案1】:itemAdapter.setItemListItems(response.body()!!) 不要调用适配器方法,而是编写自己的方法来更新列表: 私人 val carouselItems:列表, 使此列表可变并写入方法:
public fun updateList(val items: List<CarouselItem>)
carouselItems.clear()
carouselItems.addAll(items)
notifyDataSetChanged()
【讨论】:
所以这将在适配器中。然后在我假设的 onResponse 方法中调用它? @OEThe11 是的,完全正确 您介意澄清一下如何准确地实现更新列表功能吗?: 将字段 carouselItems 的类型更改为 MutableList以上是关于如何将 Retrofit 中的信息实现到 viewPager2 布局小部件中的主要内容,如果未能解决你的问题,请参考以下文章
如何将图像另存为在 retrofit2 中发送到 wcf Web 服务的流中的图像
如何使用 Retrofit 将参数传递给 POST 请求,然后序列化请求?
Android实战----Android Retrofit是怎么将回调函数放到UI线程(主线程)中的(源码分析)
如何将 AsyncTask 转换为 Retrofit 以从 Android 中的 WCF Webservice 获取数据