【问题标题】:How to add 2 buttons on each view in recyclerview如何在recyclerview的每个视图上添加2个按钮
【发布时间】:2020-04-10 11:41:31
【问题描述】:

我有一个可以运行的应用程序,我想稍微升级一下。现在,我正在从一个网页中获取一个 API,其中包含您可以访问的许多地方的列表,并且我在 main.js 的回收站视图中列出了该页面中的所有名称。当我点击一个视图时,我会被重定向到另一个页面。

但我现在想要的是每边都有 2 个按钮。

像这样:

如果您点击名称,您将被发送到信息页面,如果您按下 GPS 图标,您将被发送到谷歌地图(我已经实施了谷歌地图)。但我不太清楚该怎么做

这是我的适配器

class MainAdapter(val homeFeed : HomeFeed) : RecyclerView.Adapter<CustomViewHolder>() {

private var TAG = "Main"

    // numberOfItems
    override fun getItemCount(): Int {
        return homeFeed.features.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
        // how do we even create a view
        val layoutInflater = LayoutInflater.from(parent.context)
        val cellForRow = layoutInflater.inflate(R.layout.list_row, parent, false)

        return CustomViewHolder(cellForRow)
    }

    override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
        val feature = homeFeed.features.get(position)

        holder.view.textView_place_name.text = feature.properties.name
        holder.feature = feature
    }
}

class CustomViewHolder(val view: View, var feature: Feature? = null): RecyclerView.ViewHolder(view) {
    companion object{
        val FEATURE_NAME_KEY = "FEATURE_TITLE"
        val FEATURE_ID_KEY = "FEATURE_ID"
    }

    init {

        view.setOnClickListener {
            println("clicking on a view : MainAdapter")

            val intent = Intent(view.context, DetailActivity::class.java)

            intent.putExtra(FEATURE_NAME_KEY,feature?.properties?.name)
            intent.putExtra(FEATURE_ID_KEY, feature?.properties?.id)

            view.context.startActivity(intent)
        }
   }
}// end CustomViewHolder

这是我的“观点”:

<?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">

<ImageView
    android:id="@+id/imageView_navigation_link"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:paddingTop="8dp"
    android:paddingRight="8dp"
    android:paddingBottom="8dp"
    app:layout_constraintBottom_toBottomOf="@+id/textView_place_name"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="@+id/textView_place_name"
    app:srcCompat="@drawable/ic_map_marker" />

<TextView
    android:id="@+id/textView_place_name"
    android:background="@drawable/rd_corners_list_row"
    android:layout_width="350dp"
    android:layout_height="0dp"
    android:paddingLeft="20dp"
    android:paddingTop="8dp"
    android:paddingRight="8dp"
    android:paddingBottom="8dp"
    android:text="Place Name"
    android:textColor="@color/black"
    android:textSize="20sp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

正是这个“imageView_navigation_link”我想添加第二个按钮,但我不知道如何更改我的适配器来实现这一点。

如果您能帮助我使其与 textView_place_name 一起可点击,我将不胜感激。

【问题讨论】:

    标签: android-studio kotlin button android-recyclerview view


    【解决方案1】:
    override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
        val feature = homeFeed.features.get(position)
    
        holder.view.textView_place_name.text = feature.properties.name
        holder.view.imageView_navigation_link.setOnclickListener{
            // do your logic here
        }
    
        holder.feature = feature
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-15
      • 1970-01-01
      • 2011-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多