RecycleView + niceRatingBar 打分画星

博主分享了在Android Studio中遇到的mipmap图标加载异常,以及如何修复RecyclerView数据刷新问题。通过NiceRatingBar示例展示了评分控件的使用和刷新策略调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

碰到了一个很奇葩的问题,在mipmap中添加了名为icon_return_back的一个图标.png文件,结果在xml文件中找不到,src还是background都找不到,而且唯独在这一个地方找不到,其他的xml文件中都可以找到这个文件名,重启电脑重启android studio都不管用,最后卸载了android studio并安装了最新版本才可以。具体原因还不知道。

学到的东西主要有,@SuppressLint("ClickableViewAccessibility")这个玩意的使用,

注解,打个@SuppressLint("ClickableViewAccessibility")
这个警告是说,有可能会和点击事件发生冲突
如果你在touch中返回了true,那么就不会响应onClick事件了
你必须调用一下view.performClick(),才会触发
view.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
});

具体可以看https://blog.csdn.net/bingospunky/article/details/43735497这篇博客。

然后最主要的还是解决了一个RecycleView的不刷新和刷新错误的问题。

问题是代码在下拉或上划刷新的过程中使得让最上面或最下面的数据显示异常,在跳转下一页或者上一页之后之前的数据均消失不再显示。我要做的是让一个评分画星星的控件正常显示之前打好的星星,在上一页下一页的过程中已经打好的星星不会消失,正常显示。NiceRatingBar,你可以在Github找到它的源码:https://github.com/KaelLi1989/NiceRatingBar,实现效果如下:

关于RecycleView的介绍已经太多了,

https://www.jianshu.com/p/7a7d9301b2f1

https://www.jianshu.com/p/3e9aa4bdaefd?utm_source=desktop&utm_medium=timeline

https://blog.csdn.net/lmj623565791/article/details/45059587具体可以看看,在Adapter中添加数据,使用adapter.notifyDataSetChanged()来刷新,应该不是多难的玩意,但是我的数据就是显示不正常,而且很奇怪的是在页面之外的数据会显示不正常,但是页面之内的就没有问题,查看整个Adapter中的代码后,我发现在其中已经有了对数据的记录,应该是刷新的不正常。

@Override
protected void convert(@NonNull @NotNull BaseViewHolder helper, RuleBean.DataDTO.ChildrenDTOX.ChildrenDTO item) {}

重写了关于convert的代码,进到BaseQuickAdapter中可以看到convert的介绍

protected abstract void convert(@NonNull K helper, T item);

    /**
     * Optional implementation this method and use the helper to adapt the view to the given item.
     *
     * If {@link DiffUtil.Callback#getChangePayload(int, int)} is implemented,
     * then {@link BaseQuickAdapter#convert(BaseViewHolder, Object)} will not execute, and will
     * perform this method, Please implement this method for partial refresh.
     *
     * If use {@link RecyclerView.Adapter#notifyItemChanged(int, Object)} with payload,
     * Will execute this method.
     *
     *
     * @param helper   A fully initialized helper.
     * @param item     The item that needs to be displayed.
     * @param payloads payload info.
     */

重要的东西就是这个helper和item的作用,一个是完全初始化的helper,一个是需要展示的item,那这样应该是在helper和item的帮助下初始化需要显示的数据

NiceRatingBar niceRatingBar = helper.getView(R.id.nrb);
String ruleTotal = item.getRuleScore();

if (TextUtils.isEmpty(ruleTotal)) {
    ruleTotal = "0";
}
if ("0".equals(ruleTotal)) {
    niceRatingBar.setVisibility(View.GONE);
} else {
    niceRatingBar.setVisibility(View.VISIBLE);
    niceRatingBar.setTotalStar(Integer.parseInt(ruleTotal)); 
    niceRatingBar.setRating(Float.parseFloat(item.getReduceScore()));
}

首先通过helper得到画星星打分的控件niceRatingBar,用TextUtils判断打分的星星总数是否为空,不为空也不为0 的情况下,显示星星,用setRating来展示点击的星星个数,也就是点亮的星星。

helper.addOnClickListener(R.id.imgClear);
niceRatingBar.setOnRatingChangedListener(rating -> {
    item.setReduceScore(String.valueOf(rating));
    helper.setText(R.id.tvReduceScope, MessageFormat.format(mContext.getString(R.string.str_reduce_score), item.getReduceScore()));
    onClickNiceRebarResult.onClickResult();
    notifyReduceScore(helper, item);
});
helper.getView(R.id.imgClear).setOnClickListener(v -> {
    item.setReduceScore(String.valueOf(0));
    helper.setText(R.id.tvReduceScope, MessageFormat.format(mContext.getString(R.string.str_reduce_score), item.getReduceScore()));
    onClickNiceRebarResult.onClickResult();
    notifyReduceScore(helper, item);
});

之后设置监听事件对打分了的星星进行展示,  item.setReduceScore(String.valueOf(0)); 

helper.setText(R.id.tvReduceScope, MessageFormat.format(mContext.getString(R.string.str_reduce_score), item.getReduceScore()));获取点击的结果,有多少颗星星就显示扣了多少分。

notifyReduceScore(helper, item);是用来显示打分了之后序号标题的颜色改变。

这样一看就很清楚原因所在了,总的来说就是在星星数不为空的时候,先刷新了打分的结果,然后载进行打分,使得打分的结果并没有被刷新,没有被展示。

niceRatingBar.setRating(Float.parseFloat(item.getReduceScore()));

将这个放到最后,在打分之后再进行刷新。

niceRatingBar.setRatingStatus(RatingStatus.Disable);可以设置不允许打分,让分数只能展示。

测试通过,一行代码位置调整,一句代码都不用多写,我找了一天,真的菜啊。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值