Android 如何将单击事件传递到framelayout中的重叠视图
这里我做了一个RecyclerView,列表中的每一行都由frameLayout组成,其中有两个主要的子布局(Background RelativeLayout和foreGround RelativeLayout)相互重叠 使用ItemTouchHelper.SimpleCallback,我正在滑动前景布局以显示背景布局 我无法在后台的任何实体上获取ClickEvent。请查找代码 list_row.xmlAndroid 如何将单击事件传递到framelayout中的重叠视图,android,android-recyclerview,kotlin,android-framelayout,Android,Android Recyclerview,Kotlin,Android Framelayout,这里我做了一个RecyclerView,列表中的每一行都由frameLayout组成,其中有两个主要的子布局(Background RelativeLayout和foreGround RelativeLayout)相互重叠 使用ItemTouchHelper.SimpleCallback,我正在滑动前景布局以显示背景布局 我无法在后台的任何实体上获取ClickEvent。请查找代码 list_row.xml <?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- android:paddingStart="?listPreferredItemPaddingLeft"
android:paddingTop="@dimen/padding_list_row"
android:paddingEnd="?listPreferredItemPaddingRight"
android:paddingBottom="@dimen/padding_list_row"-->
<RelativeLayout
android:id="@+id/view_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/mailSwipeMenu">
<LinearLayout
android:id="@+id/right_swipe_options_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center"
android:orientation="horizontal"
android:visibility="gone">
<RelativeLayout
android:id="@+id/unread_btn_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/padding_10"
android:layout_marginEnd="@dimen/padding_10"
android:gravity="center">
<ImageView
android:id="@+id/mark_unread_icon"
android:layout_width="@dimen/icon_delete"
android:layout_height="@dimen/icon_delete"
android:layout_centerHorizontal="true"
android:layout_marginBottom="@dimen/padding_6"
android:src="@drawable/icon_read_button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/mark_unread_icon"
android:layout_centerHorizontal="true"
android:text="@string/mark_as_unread"
android:textColor="#fff"
android:textSize="@dimen/text_icon" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/spam_btn_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/padding_10"
android:layout_marginEnd="@dimen/padding_10"
android:gravity="center">
<ImageView
android:id="@+id/report_spam_icon"
android:layout_width="@dimen/icon_delete"
android:layout_height="@dimen/icon_delete"
android:layout_centerHorizontal="true"
android:layout_marginBottom="@dimen/padding_6"
android:src="@drawable/icon_spam_button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/report_spam_icon"
android:layout_centerHorizontal="true"
android:text="@string/report_spam"
android:textColor="#fff"
android:textSize="@dimen/text_icon" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/move_btn_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/padding_10"
android:layout_marginEnd="@dimen/padding_10"
android:gravity="center">
<ImageView
android:id="@+id/move_mail_icon"
android:layout_width="@dimen/icon_delete"
android:layout_height="@dimen/icon_delete"
android:layout_centerHorizontal="true"
android:layout_marginBottom="@dimen/padding_6"
android:src="@drawable/icon_move_button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/move_mail_icon"
android:layout_centerHorizontal="true"
android:text="@string/move_mail"
android:textColor="#fff"
android:textSize="@dimen/text_icon" />
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:id="@+id/left_swipe_options_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="@dimen/padding_10"
android:gravity="center"
android:visibility="gone">
<ImageView
android:id="@+id/delete_icon"
android:layout_width="@dimen/icon_delete"
android:layout_height="@dimen/icon_delete"
android:layout_centerHorizontal="true"
android:layout_marginBottom="@dimen/padding_6"
android:src="@drawable/icon_delete_button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/delete_icon"
android:layout_centerHorizontal="true"
android:text="@string/delete"
android:textColor="#fff"
android:textSize="@dimen/text_icon" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/view_foreground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_list_row_inbox"
android:orientation="vertical"
android:padding="@dimen/padding_10">
<LinearLayout
android:id="@+id/message_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingStart="72dp"
android:paddingEnd="@dimen/padding_list_row">
<TextView
android:id="@+id/from"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:textColor="@color/from"
android:textSize="@dimen/msg_text_primary"
android:textStyle="bold" />
<TextView
android:id="@+id/txt_primary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:textColor="@color/subject"
android:textSize="@dimen/msg_text_secondary"
android:textStyle="bold" />
<TextView
android:id="@+id/txt_secondary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:textColor="@color/message"
android:textSize="@dimen/msg_text_secondary" />
</LinearLayout>
<RelativeLayout
android:id="@+id/select_icon_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/icon_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="@dimen/icon_width_height"
android:layout_height="@dimen/icon_width_height"
android:src="@drawable/bg_circle" />
<ImageView
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:srcCompat="@drawable/icon_check_white_24dp" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/icon_front"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/icon_profile"
android:layout_width="@dimen/icon_width_height"
android:layout_height="@dimen/icon_width_height" />
<TextView
android:id="@+id/icon_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="@android:color/white"
android:textSize="@dimen/text_icon" />
</RelativeLayout>
</RelativeLayout>
<TextView
android:id="@+id/timestamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:textColor="@color/timestamp"
android:textSize="@dimen/text_timestamp"
android:textStyle="bold" />
<ImageView
android:id="@+id/icon_star"
android:layout_width="@dimen/icon_star"
android:layout_height="@dimen/icon_star"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:tint="@color/icon_tint_normal" />
</RelativeLayout>
</FrameLayout>
RecyclerItemTouchHelper.kt
open class RecyclerItemTouchHelper(dragDirs: Int, swipeDirs: Int, private val listener: RecyclerItemTouchHelperListener) : ItemTouchHelper.SimpleCallback(dragDirs, swipeDirs) {
override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
return true
}
override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
if (viewHolder != null) {
val foregroundView = (viewHolder as InboxListAdapter.InboxItemHolder).viewForeground
getDefaultUIUtil().onSelected(foregroundView)
}
}
override fun onChildDrawOver(c: Canvas, recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float,
actionState: Int, isCurrentlyActive: Boolean) {
val foregroundView = (viewHolder as InboxListAdapter.InboxItemHolder).viewForeground
getDefaultUIUtil().onDrawOver(c, recyclerView, foregroundView, dX, dY,
actionState, isCurrentlyActive)
}
override fun clearView(recyclerView: RecyclerView?, viewHolder: RecyclerView.ViewHolder) {
val foregroundView = (viewHolder as InboxListAdapter.InboxItemHolder).viewForeground
getDefaultUIUtil().clearView(foregroundView)
listener.onForegroundViewRestored(viewHolder,viewHolder.adapterPosition)
}
override fun onChildDraw(c: Canvas, recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float,
actionState: Int, isCurrentlyActive: Boolean) {
val foregroundView = (viewHolder as InboxListAdapter.InboxItemHolder).viewForeground
listener.onSwipeStart(viewHolder, if (dX < 0) ItemTouchHelper.LEFT else ItemTouchHelper.RIGHT, viewHolder.adapterPosition)
getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX * .9F, dY,
actionState, isCurrentlyActive)
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
listener.onSwiped(viewHolder, direction, viewHolder.adapterPosition)
}
override fun convertToAbsoluteDirection(flags: Int, layoutDirection: Int): Int {
return super.convertToAbsoluteDirection(flags, layoutDirection)
}
interface RecyclerItemTouchHelperListener {
fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int, position: Int)
fun onSwipeStart(viewHolder: RecyclerView.ViewHolder, direction: Int, position: Int)
fun onForegroundViewRestored(viewHolder: RecyclerView.ViewHolder, position: Int)
}
}
class InboxListAdapter() : RecyclerView.Adapter<InboxListAdapter.InboxItemHolder>() {
var mContext: Context? = null
private var messages = mutableListOf<InboxItemModel>()
var listener: MessageAdapterListener? = null
private var selectedItems: SparseBooleanArray? = null
private var animationItemsIndex: SparseBooleanArray? = null
private var reverseAllAnimations = false
private var currentSelectedIndex = -1
constructor(context: Context, messages: MutableList<InboxItemModel>, listener: MessageAdapterListener) : this() {
this.mContext = context
this.messages = messages
this.listener = listener
selectedItems = SparseBooleanArray()
animationItemsIndex = SparseBooleanArray()
}
inner class InboxItemHolder(view: View) : RecyclerView.ViewHolder(view), View.OnLongClickListener ,View.OnClickListener {
var viewForeground : RelativeLayout = view.findViewById<View>(R.id.view_foreground) as RelativeLayout
var viewBackGround : RelativeLayout = view.findViewById<View>(R.id.view_background) as RelativeLayout
var viewLeftSwipe : RelativeLayout = view.findViewById<View>(R.id.left_swipe_options_layout) as RelativeLayout
var viewRightSwipe : LinearLayout = view.findViewById<View>(R.id.right_swipe_options_layout) as LinearLayout
var spamButtonLayout : RelativeLayout = view.findViewById<View>(R.id.spam_btn_layout) as RelativeLayout
var moveButtonLayout : RelativeLayout = view.findViewById<View>(R.id.move_btn_layout) as RelativeLayout
var unreadButtonLayout : RelativeLayout = view.findViewById<View>(R.id.unread_btn_layout) as RelativeLayout
var deleteImageView : ImageView = view.findViewById<View>(R.id.delete_icon) as ImageView
var from: TextView = view.findViewById<View>(R.id.from) as TextView
var subject: TextView = view.findViewById<View>(R.id.txt_primary) as TextView
var message: TextView = view.findViewById<View>(R.id.txt_secondary) as TextView
var iconText: TextView = view.findViewById<View>(R.id.icon_text) as TextView
var timestamp: TextView = view.findViewById<View>(R.id.timestamp) as TextView
var iconImp: ImageView = view.findViewById<View>(R.id.icon_star) as ImageView
var imgProfile: ImageView = view.findViewById<View>(R.id.icon_profile) as ImageView
var messageContainer: LinearLayout = view.findViewById<View>(R.id.message_container) as LinearLayout
var iconContainer: RelativeLayout = view.findViewById<View>(R.id.select_icon_container) as RelativeLayout
var iconBack: RelativeLayout = view.findViewById<View>(R.id.icon_back) as RelativeLayout
var iconFront: RelativeLayout = view.findViewById<View>(R.id.icon_front) as RelativeLayout
init {
view.setOnLongClickListener(this)
spamButtonLayout.setOnClickListener(this)
moveButtonLayout.setOnClickListener(this)
unreadButtonLayout.setOnClickListener(this)
viewLeftSwipe.setOnClickListener(this)
deleteImageView.setOnClickListener(this)
}
override fun onLongClick(view: View): Boolean {
listener!!.onRowLongClicked(adapterPosition)
view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS)
return true
}
override fun onClick(p0: View?) {
when(p0!!.id){
R.id.spam_btn_layout -> listener!!.onSpamButtonClicked(adapterPosition)
R.id.unread_btn_layout -> listener!!.onReadButtonClicked(adapterPosition)
R.id.move_btn_layout -> listener!!.onMoveButtonClicked(adapterPosition)
R.id.left_swipe_options_layout -> listener!!.onDeleteButtonClicked(adapterPosition)
R.id.delete_icon -> listener!!.onDeleteButtonClicked(adapterPosition)
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): InboxItemHolder {
val itemView = LayoutInflater.from(parent.context)
.inflate(R.layout.list_row_inbox, parent, false)
return InboxItemHolder(itemView)
}
override fun onBindViewHolder(holder: InboxItemHolder, position: Int) {
val message = messages[position]
holder.from.text = message.sender
holder.subject.text = message.subject
holder.message.text = message.subject
holder.timestamp.text = message.timeStamp
holder.itemView.isActivated = selectedItems!!.get(position, false)
[![enter image description here][1]][1]
// apply click events
applyClickEvents(holder, position)
}
private fun applyClickEvents(holder: InboxItemHolder, position: Int) {
holder.iconContainer.setOnClickListener { listener!!.onIconClicked(position) }
holder.iconImp.setOnClickListener { listener!!.onIconImportantClicked(position) }
holder.messageContainer.setOnClickListener { listener!!.onMessageRowClicked(position) }
holder.messageContainer.setOnLongClickListener { view ->
listener!!.onRowLongClicked(position)
view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS)
true
}
}
}
开放类RecyclerItemTouchHelper(dragDirs:Int,swipeDirs:Int,私有val侦听器:RecyclerItemTouchHelperListener):ItemTouchHelper.SimpleCallback(dragDirs,swipeDirs){
override-fun-onMove(recyclerView:recyclerView,viewHolder:recyclerView.viewHolder,target:recyclerView.viewHolder):布尔值{
返回真值
}
覆盖选定更改的乐趣(视图持有者:RecyclerView.viewHolder?,操作状态:Int){
if(viewHolder!=null){
val foregroundView=(viewHolder作为InboxListAdapter.InboxItemHolder)。viewForeground
getDefaultUIUtil().onSelected(foregroundView)
}
}
覆盖有趣的OnChildRawOver(c:Canvas,recyclerView:recyclerView,
viewHolder:RecyclerView.viewHolder,dX:Float,dY:Float,
actionState:Int,isCurrentlyActive:Boolean){
val foregroundView=(viewHolder作为InboxListAdapter.InboxItemHolder)。viewForeground
getDefaultUIUtil().onDrawOver(c,recyclerView,foregroundView,dX,dY,
actionState,当前处于活动状态)
}
覆盖有趣的clearView(recyclerView:recyclerView?,viewHolder:recyclerView.viewHolder){
val foregroundView=(viewHolder作为InboxListAdapter.InboxItemHolder)。viewForeground
getDefaultUIUtil().clearView(foregroundView)
listener.onForegroundViewRestored(viewHolder、viewHolder.adapterPosition)
}
覆盖有趣的OnChildraw(c:Canvas,recyclerView:recyclerView,
viewHolder:RecyclerView.viewHolder,dX:Float,dY:Float,
actionState:Int,isCurrentlyActive:Boolean){
val foregroundView=(viewHolder作为InboxListAdapter.InboxItemHolder)。viewForeground
listener.onswipstart(viewHolder,if(dX<0)ItemTouchHelper.LEFT else ItemTouchHelper.RIGHT,viewHolder.adapterPosition)
getDefaultUIUtil().onDraw(c,recyclerView,foregroundView,dX*.9F,dY,
actionState,当前处于活动状态)
}
覆盖(viewHolder:RecyclerView.viewHolder,方向:Int){
listener.onsweed(视图保持器、方向、视图保持器.适配器位置)
}
覆盖fun convertToAbsoluteDirection(标志:Int,布局方向:Int):Int{
返回super.converttoabsolutiondirection(标志、布局方向)
}
接口RecyclerItemTouchHelperListener{
视图持有者:RecyclerView.viewHolder,方向:Int,位置:Int)
趣味onSwipeStart(viewHolder:RecyclerView.viewHolder,方向:Int,位置:Int)
fun onForegroundViewRestored(视图持有者:RecyclerView.viewHolder,位置:Int)
}
}
InboxListAdapter.kt
open class RecyclerItemTouchHelper(dragDirs: Int, swipeDirs: Int, private val listener: RecyclerItemTouchHelperListener) : ItemTouchHelper.SimpleCallback(dragDirs, swipeDirs) {
override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
return true
}
override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
if (viewHolder != null) {
val foregroundView = (viewHolder as InboxListAdapter.InboxItemHolder).viewForeground
getDefaultUIUtil().onSelected(foregroundView)
}
}
override fun onChildDrawOver(c: Canvas, recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float,
actionState: Int, isCurrentlyActive: Boolean) {
val foregroundView = (viewHolder as InboxListAdapter.InboxItemHolder).viewForeground
getDefaultUIUtil().onDrawOver(c, recyclerView, foregroundView, dX, dY,
actionState, isCurrentlyActive)
}
override fun clearView(recyclerView: RecyclerView?, viewHolder: RecyclerView.ViewHolder) {
val foregroundView = (viewHolder as InboxListAdapter.InboxItemHolder).viewForeground
getDefaultUIUtil().clearView(foregroundView)
listener.onForegroundViewRestored(viewHolder,viewHolder.adapterPosition)
}
override fun onChildDraw(c: Canvas, recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float,
actionState: Int, isCurrentlyActive: Boolean) {
val foregroundView = (viewHolder as InboxListAdapter.InboxItemHolder).viewForeground
listener.onSwipeStart(viewHolder, if (dX < 0) ItemTouchHelper.LEFT else ItemTouchHelper.RIGHT, viewHolder.adapterPosition)
getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX * .9F, dY,
actionState, isCurrentlyActive)
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
listener.onSwiped(viewHolder, direction, viewHolder.adapterPosition)
}
override fun convertToAbsoluteDirection(flags: Int, layoutDirection: Int): Int {
return super.convertToAbsoluteDirection(flags, layoutDirection)
}
interface RecyclerItemTouchHelperListener {
fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int, position: Int)
fun onSwipeStart(viewHolder: RecyclerView.ViewHolder, direction: Int, position: Int)
fun onForegroundViewRestored(viewHolder: RecyclerView.ViewHolder, position: Int)
}
}
class InboxListAdapter() : RecyclerView.Adapter<InboxListAdapter.InboxItemHolder>() {
var mContext: Context? = null
private var messages = mutableListOf<InboxItemModel>()
var listener: MessageAdapterListener? = null
private var selectedItems: SparseBooleanArray? = null
private var animationItemsIndex: SparseBooleanArray? = null
private var reverseAllAnimations = false
private var currentSelectedIndex = -1
constructor(context: Context, messages: MutableList<InboxItemModel>, listener: MessageAdapterListener) : this() {
this.mContext = context
this.messages = messages
this.listener = listener
selectedItems = SparseBooleanArray()
animationItemsIndex = SparseBooleanArray()
}
inner class InboxItemHolder(view: View) : RecyclerView.ViewHolder(view), View.OnLongClickListener ,View.OnClickListener {
var viewForeground : RelativeLayout = view.findViewById<View>(R.id.view_foreground) as RelativeLayout
var viewBackGround : RelativeLayout = view.findViewById<View>(R.id.view_background) as RelativeLayout
var viewLeftSwipe : RelativeLayout = view.findViewById<View>(R.id.left_swipe_options_layout) as RelativeLayout
var viewRightSwipe : LinearLayout = view.findViewById<View>(R.id.right_swipe_options_layout) as LinearLayout
var spamButtonLayout : RelativeLayout = view.findViewById<View>(R.id.spam_btn_layout) as RelativeLayout
var moveButtonLayout : RelativeLayout = view.findViewById<View>(R.id.move_btn_layout) as RelativeLayout
var unreadButtonLayout : RelativeLayout = view.findViewById<View>(R.id.unread_btn_layout) as RelativeLayout
var deleteImageView : ImageView = view.findViewById<View>(R.id.delete_icon) as ImageView
var from: TextView = view.findViewById<View>(R.id.from) as TextView
var subject: TextView = view.findViewById<View>(R.id.txt_primary) as TextView
var message: TextView = view.findViewById<View>(R.id.txt_secondary) as TextView
var iconText: TextView = view.findViewById<View>(R.id.icon_text) as TextView
var timestamp: TextView = view.findViewById<View>(R.id.timestamp) as TextView
var iconImp: ImageView = view.findViewById<View>(R.id.icon_star) as ImageView
var imgProfile: ImageView = view.findViewById<View>(R.id.icon_profile) as ImageView
var messageContainer: LinearLayout = view.findViewById<View>(R.id.message_container) as LinearLayout
var iconContainer: RelativeLayout = view.findViewById<View>(R.id.select_icon_container) as RelativeLayout
var iconBack: RelativeLayout = view.findViewById<View>(R.id.icon_back) as RelativeLayout
var iconFront: RelativeLayout = view.findViewById<View>(R.id.icon_front) as RelativeLayout
init {
view.setOnLongClickListener(this)
spamButtonLayout.setOnClickListener(this)
moveButtonLayout.setOnClickListener(this)
unreadButtonLayout.setOnClickListener(this)
viewLeftSwipe.setOnClickListener(this)
deleteImageView.setOnClickListener(this)
}
override fun onLongClick(view: View): Boolean {
listener!!.onRowLongClicked(adapterPosition)
view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS)
return true
}
override fun onClick(p0: View?) {
when(p0!!.id){
R.id.spam_btn_layout -> listener!!.onSpamButtonClicked(adapterPosition)
R.id.unread_btn_layout -> listener!!.onReadButtonClicked(adapterPosition)
R.id.move_btn_layout -> listener!!.onMoveButtonClicked(adapterPosition)
R.id.left_swipe_options_layout -> listener!!.onDeleteButtonClicked(adapterPosition)
R.id.delete_icon -> listener!!.onDeleteButtonClicked(adapterPosition)
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): InboxItemHolder {
val itemView = LayoutInflater.from(parent.context)
.inflate(R.layout.list_row_inbox, parent, false)
return InboxItemHolder(itemView)
}
override fun onBindViewHolder(holder: InboxItemHolder, position: Int) {
val message = messages[position]
holder.from.text = message.sender
holder.subject.text = message.subject
holder.message.text = message.subject
holder.timestamp.text = message.timeStamp
holder.itemView.isActivated = selectedItems!!.get(position, false)
[![enter image description here][1]][1]
// apply click events
applyClickEvents(holder, position)
}
private fun applyClickEvents(holder: InboxItemHolder, position: Int) {
holder.iconContainer.setOnClickListener { listener!!.onIconClicked(position) }
holder.iconImp.setOnClickListener { listener!!.onIconImportantClicked(position) }
holder.messageContainer.setOnClickListener { listener!!.onMessageRowClicked(position) }
holder.messageContainer.setOnLongClickListener { view ->
listener!!.onRowLongClicked(position)
view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS)
true
}
}
}
类InboxListAdapter():RecyclerView.Adapter(){
var mContext:上下文?=null
private var messages=mutableListOf()
变量侦听器:MessageAdapterListener?=null
私有变量selectedItems:SparseBooleanArray?=null
私有变量animationItemsIndex:SparseBooleanArray?=null
私有var reverseAllAnimations=false
私有变量currentSelectedIndex=-1
构造函数(context:context,messages:MutableList,listener:MessageAdapterListener):this(){
this.mContext=上下文
this.messages=消息
this.listener=listener
selectedItems=SparseBooleanArray()
animationItemsIndex=SparseBooleanArray()
}
InboxItemHolder内部类(视图:视图):RecyclerView.ViewHolder(视图),view.OnLongClickListener,view.OnClickListener{
var viewForeground:RelativeLayout=view.findViewById(R.id.view\u前台)作为RelativeLayout
var viewBackGround:RelativeLayout=view.findViewById(R.id.view\u background)作为RelativeLayout
var viewLeftSwipe:RelativeLayout=view.findViewById(R.id.left\u swipe\u options\u layout)作为RelativeLayout
var viewRightSwipe:LinearLayout=view.findViewById(R.id.right\u swip\u options\u layout)作为LinearLayout
var spamButtonLayout:RelativeLayout=view.findViewById(R.id.spam\u btn\u布局)作为RelativeLayout
var moveButtonLayout:RelativeLayout=view.findViewById(R.id.move\u btn\u布局)作为RelativeLayout
var unreadButtonLayout:RelativeLayout=view.findViewById(R.id.unread\u btn\u布局)作为RelativeLayout
var deleteImageView:ImageView=view.findViewById(R.id.delete_图标)作为ImageView
变量from:TextView=view.findViewById(R.id.from)作为TextView
变量subject:TextView=view.findViewById(R.id.txt\u primary)作为TextView
var消息:TextView=view.findViewById(R.id.txt_secondary)作为TextView
var iconText:TextView=view.findViewById(R.id.icon\u text)作为TextView
var timestamp:TextView=view.findViewById(R.id.timestamp)作为TextView
var iconImp:ImageView=view.findViewById(R.id.icon\u star)作为ImageView
var imgProfile:ImageView=view.findviewbyd(R.id.icon\u配置文件)作为ImageView
var messageConta
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int, position: Int) {
if (viewHolder is InboxListAdapter.InboxItemHolder) {
if (direction == ItemTouchHelper.LEFT) {
val item = mInboxListAdapter!!.getItem(position)
// mInboxListAdapter!!.removeItem(position)
// Toast.makeText(context, "Deleted", Toast.LENGTH_SHORT).show()
mPresenter.deleteMail(item, mFolderName!!, mFolderId!!, 20, 0, "", mTrashfolderId, position)
}
mFlagSwiped = true
mEditPosition = viewHolder.adapterPosition
val spamBtnView = viewHolder.spamButtonLayout
val unreadBtnView = viewHolder.unreadButtonLayout
val moveBtnView = viewHolder.moveButtonLayout
mItemClickRegionRect = Rect(viewHolder.itemView.left,
viewHolder.itemView.top + recyclerView_inbox.y.toInt(),
viewHolder.itemView.right,
viewHolder.itemView.bottom + recyclerView_inbox.y.toInt())
mSpamBtnClickRegionRect = Rect(spamBtnView.left + mItemClickRegionRect!!.left,
spamBtnView.top + mItemClickRegionRect!!.top,
spamBtnView.right + mItemClickRegionRect!!.left,
spamBtnView.bottom + mItemClickRegionRect!!.top)
mUnreadBtnClickRegionRect = Rect(unreadBtnView.left + mItemClickRegionRect!!.left,
unreadBtnView.top + mItemClickRegionRect!!.top,
unreadBtnView.right + mItemClickRegionRect!!.left,
unreadBtnView.bottom + mItemClickRegionRect!!.top)
mMoveBtnClickRegionRect = Rect(moveBtnView.left + mItemClickRegionRect!!.left,
moveBtnView.top + mItemClickRegionRect!!.top,
moveBtnView.right + mItemClickRegionRect!!.left,
moveBtnView.bottom + mItemClickRegionRect!!.top)
}
}
override fun onChildDraw(viewHolder:RecyclerView.ViewHolder, position: Int, dX: Float, dY: Float) {
if (viewHolder is InboxListAdapter.InboxItemHolder) {
if (dX >= resources.displayMetrics.widthPixels) {
if (mSwipeState != SWIPE_STATE_RIGHT) {
mPositionSwiped = position
mSwipeState = SWIPE_STATE_RIGHT
Toast.makeText(context, "SWIPE_STATE_RIGHT", Toast.LENGTH_SHORT).show()
}
} else if (dX <= -resources.displayMetrics.widthPixels) {
if (mSwipeState != SWIPE_STATE_LEFT) {
mSwipeState = SWIPE_STATE_LEFT
Toast.makeText(context, "SWIPE_STATE_LEFT", Toast.LENGTH_SHORT).show()
}
} else if (dX == 0f) {
if (mSwipeState != SWIPE_STATE_MID) {
mSwipeState = SWIPE_STATE_MID
Toast.makeText(context, "SWIPE_STATE_MID", Toast.LENGTH_SHORT).show()
}
}
when (dX > 0) {
true -> {
viewHolder.viewBackGround.setBackgroundColor(ContextCompat.getColor(context!!, R.color.mailSwipeMenu))
viewHolder.viewRightSwipe.visibility = View.VISIBLE
viewHolder.viewLeftSwipe.visibility = View.GONE
}
false -> {
viewHolder.viewBackGround.setBackgroundColor(ContextCompat.getColor(context!!, R.color.mailSwipeDelete))
viewHolder.viewRightSwipe.visibility = View.GONE
viewHolder.viewLeftSwipe.visibility = View.VISIBLE
}
}
}
}
override fun getSwipeDirs(viewHolder: RecyclerView.ViewHolder) {
val position = viewHolder.adapterPosition
if (position != mEditPosition) {
if (mEditPosition != -1 && mFlagSwiped) {
mInboxListAdapter!!.notifyItemChanged(mEditPosition)
mFlagSwiped = false
mEditPosition = position
}
}
}