赞
踩
项目中涉及到 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);
- }
总结: 同理可修改 文字颜色背景色等其他的设置 随意发挥
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。