У меня есть Recyclerview
элементы в MainActivity
.
Когда я нажимаю recyclerview
элемент в mainactivity
, переходите к detailactivity
. И тогда я хочу применить titleText
и цену товара (realValueText) в recyclerview
, который я нажал в detailactivity
. Как я могу сделать ? Давайте посмотрим acitivity
и adapter
.
и вы увидите detailactivity
. наконец, я хочу сделать детальный просмотр,
productTitle==titleText , productPoint==realValueText.
MainFragment
class MainFragment: Fragment(), SwipeRefreshLayout.OnRefreshListener {
var itemList: MutableList<ItemDataVo> = arrayListOf()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View? { return inflater!!.inflate(R.layout.fragmenta, container, false)}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
itemList.clear()
swipeRefreshLo.setOnRefreshListener(this)
itemList.add(ItemDataVo("item1", 49000, 29000, "r_300_x_300_3"))
itemList.add(ItemDataVo("item2", 49000, 29000, "main_cup"))
itemList.add(ItemDataVo("item3", 49000, 29000, "main_perfume"))
itemList.add(ItemDataVo("item4", 49000, 29000, "main_fan"))
itemList.add(ItemDataVo("item5", 49000, 29000, "main_bear"))
itemList.add(ItemDataVo("item6", 49000, 29000, "main_glass"))
val itemRecyclerAdapter= ItemRecyclerAdapter(context!!,itemList)
recycler_view.adapter=itemRecyclerAdapter
recycler_view.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(context)
}
override fun onRefresh() {
swipeRefreshLo.isRefreshing = false
}
}
ItemDataVo
class ItemDataVo(title: String, realValue: Int, pointValue: Int, imageView: String, viewType: Int = 0) {
var viewType = viewType
var title :String = title
var realValue:Int = realValue
var pointValue:Int =pointValue
var image :String = imageView
}
ItemRecyclerAdapter
class ItemRecyclerAdapter(val context: Context, private var dataList: MutableList<ItemDataVo>) : RecyclerView.Adapter<ItemRecyclerAdapter.Holder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
val view = LayoutInflater.from(context).inflate(R.layout.item_1_col_list, parent, false)
return Holder(view)
}
override fun onBindViewHolder(holder: Holder, position: Int) {
holder?.bind(dataList[position],context)
}
override fun getItemCount(): Int = dataList.size
override fun getItemViewType(position: Int): Int = dataList[position].viewType
inner class Holder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {
var titleText = itemView?.findViewById(R.id.titleText) as TextView
var realValueText = itemView?.findViewById(R.id.realValueText) as TextView
var pointValueText = itemView?.findViewById(R.id.pointValueText) as TextView
var imageView = itemView?.findViewById(R.id.imageView)as ImageView
var itemLayout = itemView?.findViewById(R.id.item_layout) as ConstraintLayout
fun bind(data:ItemDataVo, context: Context) {
if(data.image !="") {
val resourceId = context.resources.getIdentifier(data.image, "drawable", context.packageName)
imageView?.setImageResource(resourceId)
}else {
imageView?.setImageResource(R.mipmap.ic_launcher)
}
pointValueText?.text=data.pointValue.toString() + "P"
realValueText?.text=data.realValue.toString()
titleText?.text=data.title
itemLayout?.setOnClickListener {
val i = Intent(it.context, DetailItemViewActivity::class.java)
it.context.startActivity(i)
}
}
}
}
DetailItemViewActivity
class DetailItemViewActivity : AppCompatActivity(), SwipeRefreshLayout.OnRefreshListener {
var Detailslist: MutableList<DetailItemDataVo> = arrayListOf()
@SuppressLint("ResourceType")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_details)
var productTitle = findViewById<TextView>(R.id.productTitle)
var productpoint = findViewById<TextView>(R.id.productPoint)