第一步:布局
<?xml version="1.0" encoding="utf-8"?> <com.ddmh.magic.camera.ui.widget.SwipeMenuLayout 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="60dp"android:layout_marginTop="12dp"android:clickable="true"app:ios="false"app:leftSwipe="true"app:swipeEnable="true"><androidx.constraintlayout.widget.ConstraintLayoutandroid:id="@+id/cst_out"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/tv_team_name_short"android:layout_width="60dp"android:layout_height="match_parent"android:background="@drawable/bg_5493f7_4radius"android:gravity="center"android:text="xxxx"android:textColor="#ffffffff"android:textSize="18sp"app:layout_constraintLeft_toLeftOf="parent" /><TextViewandroid:id="@+id/tv_top_tips"android:layout_width="32dp"android:layout_height="18dp"android:background="@drawable/bg_e0e0e0_4radius"android:gravity="center"android:text="置顶"android:textColor="#ff878f99"android:textSize="10sp"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/tv_team_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="8dp"android:ellipsize="end"android:maxWidth="250dp"android:singleLine="true"android:text="xxx"android:textColor="#ff1d2129"android:textSize="16sp"app:layout_constraintBottom_toTopOf="@+id/tv_team_day"app:layout_constraintHorizontal_weight="1"app:layout_constraintLeft_toRightOf="@+id/tv_team_name_short"app:layout_constraintTop_toTopOf="@+id/tv_team_name_short" /><TextViewandroid:id="@+id/tv_admin"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="8dp"android:background="@drawable/bg_ff615d_2radius"android:paddingLeft="7dp"android:paddingTop="5dp"android:paddingRight="7dp"android:paddingBottom="5dp"android:text="主xx员"android:textColor="#ffff615d"android:textSize="10sp"app:layout_constraintLeft_toRightOf="@+id/tv_team_name"app:layout_constraintTop_toTopOf="@+id/tv_team_name" /><ImageViewandroid:id="@+id/tv_team_day"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/icon_today"app:layout_constraintBottom_toBottomOf="@+id/tv_team_name_short"app:layout_constraintLeft_toLeftOf="@+id/tv_team_name"app:layout_constraintTop_toBottomOf="@+id/tv_team_name" /><TextViewandroid:id="@+id/tv_takephots_peoples"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="@dimen/dp_4"android:text="xxxx"android:textColor="#ff878f99"android:textSize="12sp"app:layout_constraintBottom_toBottomOf="@+id/tv_team_day"app:layout_constraintLeft_toRightOf="@+id/tv_team_day"app:layout_constraintTop_toTopOf="@+id/tv_team_day" /><TextViewandroid:id="@+id/tv_pic_num"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:text="xxx"android:textColor="#ff878f99"android:textSize="12sp"app:layout_constraintBottom_toBottomOf="@+id/tv_takephots_peoples"app:layout_constraintLeft_toRightOf="@+id/tv_takephots_peoples"app:layout_constraintTop_toTopOf="@+id/tv_takephots_peoples" /></androidx.constraintlayout.widget.ConstraintLayout><LinearLayoutandroid:id="@+id/ll_top"android:layout_width="90dp"android:layout_height="match_parent"android:background="@color/color_gray"android:gravity="center"><TextViewandroid:id="@+id/tv_top"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:text="置顶"android:textColor="@color/white"android:textSize="16sp" /></LinearLayout> </com.ddmh.magic.camera.ui.widget.SwipeMenuLayout>
第二步:适配器
class TeamItemAdapter(var list: ArrayList<TeamListBean>?) :BaseQuickAdapter<TeamListBean, BaseViewHolder>(R.layout.item_team_member, list) {override fun convert(holder: BaseViewHolder, item: TeamListBean) {holder.setText(R.id.tv_team_name_short, CommonUtils.getBeforeTwoStr(item.teamName))holder.setText(R.id.tv_team_name, item.teamName)holder.setText(R.id.tv_takephots_peoples, "已xx数:2")holder.setGone(R.id.tv_top_tips, !item.topFlag)holder.setText(R.id.tv_pic_num, "照片:2")when (item.roleType) {1 -> {holder.setText(R.id.tv_admin, "主管理员")holder.setTextColor(R.id.tv_admin,AppContext.appContext.resources.getColor(R.color.color_ff615d))holder.setBackgroundResource(R.id.tv_admin, R.drawable.bg_ff615d_2radius)}2 -> {holder.setText(R.id.tv_admin, "管理员")holder.setTextColor(R.id.tv_admin,AppContext.appContext.resources.getColor(R.color.color_f2a649))holder.setBackgroundResource(R.id.tv_admin, R.drawable.bg_1af2a649_2radius)}3 -> {holder.setText(R.id.tv_admin, "成员")holder.setTextColor(R.id.tv_admin,AppContext.appContext.resources.getColor(R.color.font_39ba81))holder.setBackgroundResource(R.id.tv_admin, R.drawable.bg_39ba81_2radius)}}var tvTop = holder.getView<TextView>(R.id.tv_top)tvTop.text = if (item.topFlag) {"取消置顶"} else {"置顶"}if (item.topFlag) {holder.setBackgroundColor(R.id.ll_top,AppContext.appContext.resources.getColor(R.color.color_ff615d))} else {holder.setBackgroundColor(R.id.ll_top,AppContext.appContext.resources.getColor(R.color.font_c9cdd4))}} }
第三步:代码
mTeamAdapter.addChildClickViewIds(R.id.tv_top, R.id.cst_out) mTeamAdapter.setOnItemChildClickListener { adapter, view, position ->val bean = adapter.data[position] as TeamListBeanwhen (view.id) {R.id.tv_top -> {bean.topFlag = !bean.topFlagadapter.notification}R.id.cst_out -> {}}}