RecyclerView 点击Item 改变背景色

项目中涉及到 RecyclerView item  的切换,需要改变选中的背景  ,本着不麻烦ui小姐姐的原则 自己实现

效果图:

基本思路是这样的:

在Fragment或者Activity中做个标记currentPosition
在点击了Item的时候将position记录下来,并刷新适配器
然后在Adapter中设置回调监听,继承的BaseQuickAdapter,在convert()方法中设置回调监听
最后在Fragment或者Activity使用监听判断当前位置是否和传入的position是否一致
一致就设置选中的背景,不一致就要修改Item中的背景

具体实现:

Fragment或者Activity中:

private int   currentPosition = 0;
 
mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //这里赋值
                currentPosition = position;
                //每点击一次item就刷新适配器
                mAdapter.notifyDataSetChanged();

            }


        });

        mAdapter.setItemSelectedCallBack(new AmountInfoAdapter.ItemSelectedCallBack() {

            @Override
            public void convert(BaseViewHolder holder, int position) {

                //初始化组件
                ImageView bg = holder.getView(R.id.img_withdrawal_select);

                //判断传入的position是否和当前一致
                if (position == currentPosition) {
                    bg.setBackgroundResource(R.mipmap.icon_wytx_xuanzhong);

                } else {
                    bg.setBackgroundResource(R.drawable.profit_withdrawal_amount_shape);

                }

            }
        });

adapter中:

 @Override
    protected void convert(BaseViewHolder helper, AmountInfo item) {

        helper.setText(R.id.tv_withdrawal_amount, "¥" + item.getAmount());
        if (mCallBack != null) {
            mCallBack.convert(helper, helper.getLayoutPosition());
        }

    }


    public void setItemSelectedCallBack(ItemSelectedCallBack CallBack) {
        this.mCallBack = CallBack;
    }

    public interface ItemSelectedCallBack {
        void convert(BaseViewHolder holder, int position);
    }

总结: 同理可修改 文字颜色背景色等其他的设置  随意发挥

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
要实现RecyclerViewitem平移背景的效果,可以通过自定义RecyclerViewItemDecoration来实现。以下是实现的步骤: 1. 在res/drawable文件夹下创建一个selector文件,用来设置RecyclerView item背景变化。例如,创建一个名为background_item.xml的文件,代码如下: ```xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <shape android:shape="rectangle"> <solid android:color="@color/colorPrimaryDark" /> </shape> </item> <item> <shape android:shape="rectangle"> <solid android:color="@color/colorPrimary" /> </shape> </item> </selector> ``` 这里定义了两个状态,一个是按下状态,一个是普通状态,分别设置了不同的背景。 2. 在RecyclerView的Adapter中,设置ItemView的背景为上述selector文件,例如: ```kotlin override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false) view.setBackgroundResource(R.drawable.background_item) return ViewHolder(view) } ``` 3. 自定义一个ItemDecoration,并在其onDraw方法中实现item背景平移效果。例如: ```kotlin class CustomItemDecoration : RecyclerView.ItemDecoration() { private val mPaint = Paint() private val mOffset = 20 // 偏移量 private val mTopLeftRectF = RectF() private val mBottomRightRectF = RectF() init { mPaint.color = Color.WHITE mPaint.isAntiAlias = true } override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) { super.onDraw(c, parent, state) val childCount = parent.childCount for (i in 0 until childCount) { val child = parent.getChildAt(i) val params = child.layoutParams as RecyclerView.LayoutParams // 计算矩形区域 val left = child.left - params.leftMargin - mOffset val top = child.top - params.topMargin - mOffset val right = child.right + params.rightMargin + mOffset val bottom = child.bottom + params.bottomMargin + mOffset mTopLeftRectF.set(left.toFloat(), top.toFloat(), child.left.toFloat(), child.top.toFloat()) mBottomRightRectF.set(child.right.toFloat(), child.bottom.toFloat(), right.toFloat(), bottom.toFloat()) // 绘制背景 c.drawRoundRect(mTopLeftRectF, 10f, 10f, mPaint) c.drawRoundRect(mBottomRightRectF, 10f, 10f, mPaint) } } } ``` 这里设置了一个偏移量mOffset,用于控制背景平移的程度,可以根据实际需求进行调整。 4. 最后,将自定义的ItemDecoration设置给RecyclerView即可: ```kotlin recyclerView.addItemDecoration(CustomItemDecoration()) ``` 这样,点RecyclerViewitem时就能实现背景平移效果了。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值