如何将改装中的信息实现为视图Pager2布局小部件



所以我正在构建这个Carousel项目,它在应用程序上显示图像和一些文本。我已经通过建造一个原型来拆除这个结构。我也可以使用改装从我的终端上的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_width="match_parent"
android:layout_height="match_parent" android:background="#232323"
android:id="@+id/linearLayout">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager_ImageSlider"
android:layout_height="0dp"
android:layout_width="0dp"
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_width="wrap_content"
android:layout_height="wrap_content" 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_width="match_parent"
android:layout_height="match_parent" android:id="@+id/container" android:background="#232323">
<com.makeramen.roundedimageview.RoundedImageView
android:layout_width="match_parent"
android:layout_height="550dp" 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_width="0dp"
android:layout_height="wrap_content" 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'
} 

*这不是互联网许可,它已经存在了。

itemAdapter.setItemListItems(response.body((!(与其调用adapters方法,不如编写自己的方法来更新列表:
privatevalcarouselItems:list,使该列表可变,并编写方法:

public fun updateList(val items: List<CarouselItem>){
carouselItems.clear()
carouselItems.addAll(items)
notifyDataSetChanged()
}

相关内容

  • 没有找到相关文章

最新更新