如何将点击事件传递到帧布局中的重叠视图

时间:2018-11-14 06:20:09

标签: android android-recyclerview kotlin android-framelayout

在这里,我制作了一个 RecyclerView ,列表中的每一行都由frameLayout组成,两个主要的子布局(Background RelativeLayout和foreGround RelativeLayout)彼此重叠。

使用ItemTouchHelper.SimpleCallback,我要擦拭前景布局以显示背景布局。

我无法在background中的任何实体上获取ClickEvent。请找到代码

list_row.xml

<?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)
    }
}

InboxListAdapter.kt

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
        }
    }
}

Swiping the foreground layout

enter image description here

1 个答案:

答案 0 :(得分:2)


class MSSQLManager extends DatabaseManager {

    function __construct(){
        parent::__construct();

        $resultSet = new ResultSet();
        $resultSet->message = "MSSQLManager __construct.";
        $this->responseObj->AddResultSet($resultSet);
    }
    function Initialize(){
        //$this->responseObj->AddResultSet((object) array('message' => 'Initialized in MSSQLManager()'));
    }
    function CloseConnection(){
        //$this->responseObj->AddResultSet((object) array('message' => 'close mssql connection.'));
    }
}

// onCreatView片段/活动的OnCreate

//add value from child class
ResponseManager Object ( [message_list:protected] => Array ( [0] => MSSQLManager __construct. ) [error_message_list:protected] => Array ( ) [overall_status:protected] => [process_result_sets:protected] => Array ( [0] => ResultSet Object ( [message] => MSSQLManager __construct. [error_message] => [status] => [data] => [num_rows] => ) ) ) 

//get value from parent class
Array ( [0] => ResultSet Object ( [message] => MSSQLManager __construct. [error_message] => [status] => [data] => [num_rows] => ) ) 

//get value by object instance
array(0) { } 

//在片段/活动中实现了回调

open class RecyclerItemTouchHelper(dragDirs: Int, swipeDirs: Int, private val listener: RecyclerItemTouchHelperListener) : ItemTouchHelper.SimpleCallback(dragDirs, swipeDirs) {

override fun onMove(recyclerView: androidx.recyclerview.widget.RecyclerView, viewHolder: androidx.recyclerview.widget.RecyclerView.ViewHolder, target: androidx.recyclerview.widget.RecyclerView.ViewHolder): Boolean {
    return true
}

override fun onSelectedChanged(viewHolder: androidx.recyclerview.widget.RecyclerView.ViewHolder?, actionState: Int) {
    if (viewHolder != null) {
        val foregroundView = (viewHolder as InboxListAdapter.InboxItemHolder).viewForeground

        getDefaultUIUtil().onSelected(foregroundView)
    }
}

override fun onChildDrawOver(c: Canvas, recyclerView: androidx.recyclerview.widget.RecyclerView,
                             viewHolder: androidx.recyclerview.widget.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: androidx.recyclerview.widget.RecyclerView, viewHolder: androidx.recyclerview.widget.RecyclerView.ViewHolder) {
    val foregroundView = (viewHolder as InboxListAdapter.InboxItemHolder).viewForeground
    getDefaultUIUtil().clearView(foregroundView)
}

override fun onChildDraw(c: Canvas, recyclerView: androidx.recyclerview.widget.RecyclerView,
                         viewHolder: androidx.recyclerview.widget.RecyclerView.ViewHolder, dX: Float, dY: Float,
                         actionState: Int, isCurrentlyActive: Boolean) {
    val foregroundView = (viewHolder as InboxListAdapter.InboxItemHolder).viewForeground
    getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX, dY,
            actionState, isCurrentlyActive)

    listener.onChildDraw(viewHolder, viewHolder.adapterPosition, dX, dY)
}

override fun onSwiped(viewHolder: androidx.recyclerview.widget.RecyclerView.ViewHolder, direction: Int) {
    listener.onSwiped(viewHolder, direction, viewHolder.adapterPosition)
}

override fun convertToAbsoluteDirection(flags: Int, layoutDirection: Int): Int {
    return super.convertToAbsoluteDirection(flags, layoutDirection)
}

override fun getSwipeDirs(recyclerView: androidx.recyclerview.widget.RecyclerView, viewHolder: androidx.recyclerview.widget.RecyclerView.ViewHolder): Int {
    listener.getSwipeDirs(viewHolder)
    return super.getSwipeDirs(recyclerView, viewHolder)
}



interface RecyclerItemTouchHelperListener {
    fun onSwiped(viewHolder: androidx.recyclerview.widget.RecyclerView.ViewHolder, direction: Int, position: Int)
    fun onChildDraw(viewHolder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int, dX : Float, dY : Float)
    fun getSwipeDirs(viewHolder: androidx.recyclerview.widget.RecyclerView.ViewHolder)
}
}
相关问题