如何在RecyclerView中存储多个链接,然后在适配器类中使用这些链接?

huangapple go评论93阅读模式
英文:

How I can store multiple links in RecyclerView and than use those links in Adaptor class

问题

以下是您要翻译的内容:

如何存储不同的网址,每个网址在用户点击时执行不同的操作

这是数据模型类

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.laptopguide.databinding.ActivityMainBinding
import com.example.laptopguide.databinding.ActivityWindowsLaptopBinding

class WindowsLaptop : AppCompatActivity() {

    private lateinit var binding: ActivityWindowsLaptopBinding

    private lateinit var laptopAdapter: LaptopAdapter
    private lateinit var dataList: ArrayList<ItemsViewModel>
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityWindowsLaptopBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
        supportActionBar?.hide()

        dataList = ArrayList<ItemsViewModel>()
        dataList.add(ItemsViewModel(R.drawable.images, "Ideapad Gaming 3", "i5 第12代 (16 GB/512 GB SSD)", "54999", "立即购买"))
        dataList.add(ItemsViewModel(R.drawable.images, "Ideapad Gaming 3", "i5 第12代 (16 GB/512 GB SSD)", "54999", "立即购买"))
        dataList.add(ItemsViewModel(R.drawable.images, "Ideapad Gaming 3", "i5 第12代 (16 GB/512 GB SSD)", "54999", "立即购买"))
        dataList.add(ItemsViewModel(R.drawable.images, "Ideapad Gaming 3", "i5 第12代 (16 GB/512 GB SSD)", "54999", "立即购买"))
        dataList.add(ItemsViewModel(R.drawable.images, "Ideapad Gaming 3", "i5 第12代 (16 GB/512 GB SSD)", "54999", "立即购买"))
        dataList.add(ItemsViewModel(R.drawable.images, "Ideapad Gaming 3", "i5 第12代 (16 GB/512 GB SSD)", "54999", "立即购买"))
        dataList.add(ItemsViewModel(R.drawable.images, "Ideapad Gaming 3", "i5 第12代 (16 GB/512 GB SSD)", "54999", "立即购买"))
        dataList.add(ItemsViewModel(R.drawable.images, "Ideapad Gaming 3", "i5 第12代 (16 GB/512 GB SSD)", "54999", "立即购买"))

        laptopAdapter = LaptopAdapter(dataList, this)
        binding.rvLaptop.layoutManager = LinearLayoutManager(this)
        binding.rvLaptop.adapter = laptopAdapter
    }
}

就像这是一款笔记本电脑指南应用程序,用户可以查看笔记本电脑,当用户决定购买笔记本电脑时,我想通过链接将用户重定向到亚马逊,所以每台笔记本电脑都有自己的URL,我如何存储和使用这些URL以供特定按钮使用

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AlphaAnimation
import androidx.core.content.ContextCompat.startActivity
import androidx.recyclerview.widget.RecyclerView
import com.example.laptopguide.databinding.LaptopDesignBinding

class LaptopAdapter(var dataList: ArrayList<ItemsViewModel>, var context: Context) : RecyclerView.Adapter<LaptopAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LaptopAdapter.ViewHolder {
        var binding = LaptopDesignBinding.inflate(LayoutInflater.from(context), parent, false)
        return ViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: LaptopAdapter.ViewHolder, position: Int) {
        anima(holder.itemView)
        holder.binding.ivImage.setImageResource(dataList[position].laptopImage)
        holder.binding.tvName.text = dataList[position].laptopName
        holder.binding.tvSpecs.text = dataList[position].laptopSpecs
        holder.binding.tvPrice.text = dataList[position].laptopPrice
        holder.binding.btnBuy.text = dataList[position].laptopBuy
        holder.binding.btnShare.setOnClickListener {
            var intent = Intent(Intent.ACTION_SEND)
            intent.type = "text/plain"
            intent.putExtra(Intent.EXTRA_TEXT, "分享此内容")
            val chooser = Intent.createChooser(intent, "使用以下方式分享此笔记本电脑")
            context.startActivity(chooser)
        }
        holder.binding.btnBuy.setOnClickListener(View.OnClickListener() {
            val intent = Intent(android.content.Intent.ACTION_VIEW)
            intent.data = Uri.parse("")
            context.startActivity(intent)
        })
    }

    inner class ViewHolder(var binding: LaptopDesignBinding) : RecyclerView.ViewHolder(binding.root)

    fun anima(view: View) {
        var animation = AlphaAnimation(0.0f, 1.0f)
        animation.duration = 1500
        view.startAnimation(animation)
    }
}
英文:

how i can store different urls and each url acts differently when clicked by the user

This is data model class

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.laptopguide.databinding.ActivityMainBinding
import com.example.laptopguide.databinding.ActivityWindowsLaptopBinding

class WindowsLaptop : AppCompatActivity() {

        private lateinit var binding: ActivityWindowsLaptopBinding
        
        private lateinit var laptopAdapter: LaptopAdapter
        private lateinit var dataList:ArrayList&lt;ItemsViewModel&gt;
        override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityWindowsLaptopBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
        supportActionBar?.hide()

        dataList = ArrayList&lt;ItemsViewModel&gt;()
        dataList.add(ItemsViewModel(R.drawable.images, &quot;Ideapad Gaming 3&quot;, &quot;i5 12th Generation (16 GB/512 GB SSD)&quot;, &quot;54999&quot;, &quot;Buy Now&quot;))
        dataList.add(ItemsViewModel(R.drawable.images, &quot;Ideapad Gaming 3&quot;, &quot;i5 12th Generation (16 GB/512 GB SSD)&quot;, &quot;54999&quot;, &quot;Buy Now&quot;))
        dataList.add(ItemsViewModel(R.drawable.images, &quot;Ideapad Gaming 3&quot;, &quot;i5 12th Generation (16 GB/512 GB SSD)&quot;,&quot;54999&quot;, &quot;Buy Now&quot;))
        dataList.add(ItemsViewModel(R.drawable.images, &quot;Ideapad Gaming 3&quot;, &quot;i5 12th Generation (16 GB/512 GB SSD)&quot;,&quot;54999&quot;, &quot;Buy Now&quot;))
        dataList.add(ItemsViewModel(R.drawable.images, &quot;Ideapad Gaming 3&quot;, &quot;i5 12th Generation (16 GB/512 GB SSD)&quot;,&quot;54999&quot;, &quot;Buy Now&quot;))
        dataList.add(ItemsViewModel(R.drawable.images, &quot;Ideapad Gaming 3&quot;, &quot;i5 12th Generation (16 GB/512 GB SSD)&quot;,&quot;54999&quot;, &quot;Buy Now&quot;))
        dataList.add(ItemsViewModel(R.drawable.images, &quot;Ideapad Gaming 3&quot;, &quot;i5 12th Generation (16 GB/512 GB SSD)&quot;,&quot;54999&quot;, &quot;Buy Now&quot;))
        dataList.add(ItemsViewModel(R.drawable.images, &quot;Ideapad Gaming 3&quot;, &quot;i5 12th Generation (16 GB/512 GB SSD)&quot;, &quot;54999&quot;, &quot;Buy Now&quot;))
                


        laptopAdapter = LaptopAdapter(dataList, this)
        binding.rvLaptop.layoutManager = LinearLayoutManager(this)
        binding.rvLaptop.adapter = laptopAdapter
    }
}

Like this is a laptop guide application user can see the laptop and when user decide to buy a laptop i want to redirect user to amazon via the link so every laptop have their own url so how i can store and use those urls for specific button

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AlphaAnimation
import androidx.core.content.ContextCompat.startActivity
import androidx.recyclerview.widget.RecyclerView
import com.example.laptopguide.databinding.LaptopDesignBinding

class LaptopAdapter(var dataList:ArrayList&lt;ItemsViewModel&gt;, var context:Context):RecyclerView.Adapter&lt;LaptopAdapter.ViewHolder&gt;() {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LaptopAdapter.ViewHolder {

        var binding = LaptopDesignBinding.inflate(LayoutInflater.from(context), parent, false)
        return ViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: LaptopAdapter.ViewHolder, position: Int) {

        anima(holder.itemView)
        holder.binding.ivImage.setImageResource(dataList.get(position).laptopImage)
        holder.binding.tvName.text = dataList.get(position).laptopName
        holder.binding.tvSpecs.text = dataList.get(position).laptopSpecs
        holder.binding.tvPrice.text = dataList.get(position).laptopPrice
        holder.binding.btnBuy.text = dataList.get(position).laptopBuy
        holder.binding.btnShare.setOnClickListener {
            var intent = Intent(Intent.ACTION_SEND)
            intent.type = &quot;text/plain&quot;
            intent.putExtra(Intent.EXTRA_TEXT,&quot;Share this&quot;)
            val chooser =Intent.createChooser(intent, &quot;Share This Laptop Using....&quot;)
            context.startActivity(chooser)
        }
        holder.binding.btnBuy.setOnClickListener(View.OnClickListener(){
            val intent = Intent(android.content.Intent.ACTION_VIEW)
            intent.data = Uri.parse(&quot;&quot;)
            context.startActivity(intent)
        })
    }

    inner class ViewHolder(var binding:LaptopDesignBinding): RecyclerView.ViewHolder(binding.root)


    fun anima (view:View){

            var animation= AlphaAnimation(0.0f, 1.0f)
            animation.duration = 1500
            view.startAnimation(animation)
        }

}

答案1

得分: 2

在您的ItemsViewModel类中添加一个url字段

data class ItemsViewModel(
    ...
    val url: String
)

然后在适配器中,您可以通过以下方式获取每个项目的url:

holder.binding.btnBuy.setOnClickListener(View.OnClickListener() {
    val intent = Intent(android.content.Intent.ACTION_VIEW)
    intent.data = Uri.parse(dataList.get(position).url)
    context.startActivity(intent)
})
英文:

Add a url field in your ItemsViewModel class

data class ItemsViewModel(
    ...
    val url: String
)

and then in adapter you can get this url per item with just dataList.get(position).url like:

holder.binding.btnBuy.setOnClickListener(View.OnClickListener(){
    val intent = Intent(android.content.Intent.ACTION_VIEW)
    intent.data = Uri.parse(dataList.get(position).url)
    context.startActivity(intent)
})

huangapple
  • 本文由 发表于 2023年7月13日 13:05:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676089.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定