消息界面处理(RecyclerView操作)
有些问题大概可以分解为一个一个的具体的问题,为了解决某个具体的问题,不妨新建一个工程。面对问题,不要陷入纠结中不可自拔。实在不行,就关机,问题自然也就(暂时)消失了。“i can do this all day”,虽然是精神可嘉,但是在颓靡的时候,不要坚持哪怕是一秒~
这是一个demo
效果
代码部分
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="add"/> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rcy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button" /> <TextView android:id="@+id/view_count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginTop="100dp" android:layout_marginRight="50dp" android:text="0" android:textColor="#f00"/> </RelativeLayout>
recycler_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/item_room_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" android:textColor="#0ff"/> <TextView android:id="@+id/item_room_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" android:layout_marginLeft="15dp"/> <TextView android:id="@+id/item_room_sub_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:textColor="#00F"/> <Button android:id="@+id/item_room_clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="clear" /> </LinearLayout>
RoomInfo.kt
class RoomInfo (roomName:String,msg:String,subCount:Int){ var roomName=roomName var msg=msg var subCount=subCount }
MyRcyAdatper.kt
import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Button import android.widget.TextView import androidx.recyclerview.widget.RecyclerView class MyRcyAdapter():RecyclerView.Adapter<RecyclerView.ViewHolder>() { private var roomList:ArrayList<RoomInfo> = ArrayList() private var onClearButtonClickListener:OnClearButtonClickListener?=null constructor(onClearButtonClickListener:OnClearButtonClickListener) : this() { this.onClearButtonClickListener=onClearButtonClickListener } fun updateData(roomInfo:RoomInfo){ // this.strList.clear() // java.util.ConcurrentModificationException // for(room in this.roomList){ // if(roomInfo.roomName==room.roomName){ //// var index=roomList.indexOf(room) //// this.roomList.removeAt(index) // this.roomList.remove(room) // } // } var iterator=this.roomList.iterator() while (iterator.hasNext()){ var room=iterator.next() if(room.roomName==roomInfo.roomName){ iterator.remove() } } this.roomList.add(roomInfo) //同时要删除一些东西 notifyDataSetChanged() } // fun updateItemData(position: Int,str:String){ // strList.add(position,str) // //notifyItemChanged(position) // notifyItemRemoved(position) // notifyItemInserted(position) // } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { return MyRecyclerViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.recycler_item,parent,false)) } override fun getItemCount(): Int { return roomList.size } override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { var roomNameView=holder.itemView.findViewById<TextView>(R.id.item_room_name) var contentView=holder.itemView.findViewById<TextView>(R.id.item_room_content) var subContentView=holder.itemView.findViewById<TextView>(R.id.item_room_sub_content) var clearButton=holder.itemView.findViewById<Button>(R.id.item_room_clear) //逆序 roomNameView.text=roomList[roomList.size-1-position].roomName contentView.text=roomList[roomList.size-1-position].msg subContentView.text=roomList[roomList.size-1-position].subCount.toString() // clearButton.setOnClickListener { onClearButtonClickListener?.let{ it.onClick(roomList[roomList.size-1-position].roomName,roomList[roomList.size-1-position].msg) } } } interface OnClearButtonClickListener{ fun onClick(roomName:String,content:String) { } } class MyRecyclerViewHolder(view:View):RecyclerView.ViewHolder(view){ } }
MainActivity.kt
import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.recyclerview.widget.LinearLayoutManager import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { private var roomList:ArrayList<RoomInfo> = ArrayList() private var adapter:MyRcyAdapter?=null private var roomInfo:RoomInfo?=null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) var subCountsMap=HashMap<String,Int>() var totalCount=0 adapter= MyRcyAdapter(object:MyRcyAdapter.OnClearButtonClickListener{ override fun onClick(roomName: String,content:String) { totalCount -= subCountsMap[roomName]!! view_count.text=totalCount.toString() subCountsMap[roomName]=0 //subCountsMap和roomInfo关联,才能通过改变subCountsMap来改变roomInfo //或者也可以这样 roomInfo=RoomInfo(roomName,content,0) adapter?.updateData(roomInfo!!) } }) rcy.layoutManager=LinearLayoutManager(this) rcy.adapter=adapter //var subCounts=ArrayList<Int>() var i=0 var j=0 var subCount=1 button.setOnClickListener { // adapter.updateItemData(5,"new") if(subCountsMap.containsKey("room$i")){ var newCount=subCountsMap["room$i"]!!+1 subCountsMap.put("room$i",newCount) }else{ subCountsMap["room$i"] = subCount } roomInfo=RoomInfo("room$i","content$j",subCountsMap["room$i"]!!) if(i>8) i=0 else i++ j++ totalCount++ view_count.text=totalCount.toString() adapter?.updateData(roomInfo!!) } } }