从 api + recyclerview 获取特定数据

Get specific data from api + recyclerview

我正在使用 forkify API 制作食谱搜索应用程序。我得到 such json(让我们以披萨食谱为例)。我制作了一个回收站并可搜索,但食谱本身作为 link 提供给带有该食谱的网站(参见 json 中的 source_url)。我为此制作了一个 webview,但有一个问题。我需要获取 source_url 并使其与我点击的食谱相匹配。我试图在 resycler 中创建一个附加元素,小的不可见文本视图,并将 source_url 放在那里,所以当我点击它时,它会获取文本并将其传递给一个变量并将其作为 url 加载到网络视图。 (非常愚蠢的解决方案,但我没有想到另一个。)这不是我的想法。基本上我的代码有效,但 url 并非从所有 textView 传递。我一直在观察它的行为,我只能假设它在垂直模式下每 4 个加载一次,在水平模式下每 2 个加载一次。而这不是我需要的。请帮我解决这个问题。下面是我的代码:

适配器:

@JvmOverloads
fun RecyclerView.affectOnItemClicks(
    onClick: ((position: Int, view: View) -> Unit)? = null,
    onLongClick: ((position: Int, view: View) -> Unit)? = null
) {
    this.addOnChildAttachStateChangeListener(
        RecyclerItemClickListener(
            this,
            onClick,
            onLongClick
        )
    )
}
class RecyclerAdapter(
    private val dataset: List<Recipe>
)
    : RecyclerView.Adapter<RecyclerAdapter.FoodHolder>() {

    inner class FoodHolder(view: View) : RecyclerView.ViewHolder(view) {
        fun bindRecipe(recipe: Recipe) {
            itemView.textView.text = recipe.title
            itemView.textView3.text = recipe.publisher
            itemView.imageView.load(recipe.image_url) {
                // placeholder image is the image used
                // when our image url fails to load.
                placeholder(R.drawable.ic_baseline_error_24)
            }
            itemView.helptv.text = recipe.source_url
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerAdapter.FoodHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.recyclerview_item_row, parent, false)

        return FoodHolder(view)
    }

    override fun onBindViewHolder(holder: RecyclerAdapter.FoodHolder, position: Int) {
        holder.bindRecipe(dataset.get(position))

    }

    override fun getItemCount(): Int = dataset.size
}

MainActivity 中的代码,我将文本视图中的文本放入辅助变量,然后切换到另一个激活:

    ConstandVar.browser_url = helptv.text.toString()            
val intent = Intent(this,BrowserActivity::class.java)
startActivity(intent)

布局回收视图:

<?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="wrap_content"
    android:padding="5dp">
    <com.google.android.material.card.MaterialCardView
        android:id="@+id/materialCardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="5dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginBottom="5dp"
        android:clickable="false"
        app:cardElevation="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:state_dragged="true">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/item_constraint"
            android:layout_width="match_parent"
            android:layout_height="120dp"
            android:padding="5dp">

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="5dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:srcCompat="@tools:sample/avatars" />

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="90dp"
                android:layout_marginTop="5dp"
                android:text="Title"
                android:textColor="#000000"
                android:textSize="22sp"
                android:textStyle="bold"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />


            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="publisher: The Pioner Women"
                android:textColor="#E57373"
                android:textSize="16sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@+id/imageView"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/helptv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="1sp"
                android:visibility="invisible"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>

接收json方法: 有趣的 getRecipe(回调:(列表)-> 单位){

        val apiService = AppModule.provideRetrofitInstance(ConstandVar.BASE_URL)
        apiService.getRecipe(food).enqueue(object : Callback<requestdata> {
            override fun onFailure(call: Call<requestdata>, t: Throwable) {
                Log.d("tag", "getRecipe Error")
            }

            override fun onResponse(call: Call<requestdata>, response: Response<requestdata>) {
                return response.body()!!.recipes?.let { callback(it) }!!
            }

        })

任何帮助将不胜感激,提前致谢!

请稍微改变一下,使适配器像那样 阅读大部分评论,

在 main activity

中创建回调侦听器和 return url