草庐IT

Android RatingBar 本身画错了

coder 2023-12-02 原文

我需要强制 重绘 RatingBar 控件。

在评级栏和样式出现许多问题后,我设法使几乎所有功能都正常工作。

我在 ListView 项目中使用它。由于它的工作原理,人们不得不对其外观和行为进行一些“战斗”。我最终使用了我在 SO 上找到的解决方案,其中一个人将其设置为一个指标,但自己手动计算评分栏上的点击对应的分数。代码总是在遍历代码时产生正确的结果,但第一次控制绘画本身是错误的。这是我在 getView“第一部分”中的代码:

  final RatingBar rating = (RatingBar)view.findViewById(R.id.listitem_catalog_rating);

  rating.setOnTouchListener(new OnTouchListener() {
    @Override

    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
             float touchPositionX = event.getX();
             float width = rating.getWidth();
             float starsf = (touchPositionX / width);
             starsf = starsf * param_final__data.score_max; // 5
             int starsint = (int) starsf + param_final__data.score_min;                                   
             byte starsbyte = (byte) starsint; 
             param_final__data.score_cur = starsbyte;
             starsf = starsint; 
             rating.setRating(starsf);
             rating.setVisibility(View.INVISIBLE);
             // force repaint and set visible - how?
        }
        else
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
          param_final__view.setPressed(true);
        }
        else
        if (event.getAction() == MotionEvent.ACTION_CANCEL) {
          param_final__view.setPressed(false);
        }
        return true;      
    }                
  });

问题是。当评级栏最初显示时,第一次点击它的任何地方都会让它自己画出来,就好像总是选择最高分一样。但是,如果隐藏控件并再次显示它 - 它会正确绘制所有内容。然而,这是“自然的用户交互延迟”——例如通过单击切换可见性状态的按钮。如果我尝试使用 invalidate 或 setvisibility 指令在代码中强制重绘,什么也不会发生。

这是代码“第 2 部分”,我在显示时在 getView 中初始化评级栏:

              rating.setIsIndicator(true);
              rating.setVisibility(View.VISIBLE);
              rating.setStepSize(1);
              //rating.setMax(data.score_max);
              rating.setNumStars(data.score_max);
              rating.setRating(data.score_cur);

这是它的 XML:

   <RatingBar
        android:id="@+id/listitem_catalog_rating"        
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"        
        android:numStars="1"
        android:stepSize="1.0"
        android:rating="1.0" 
        style="@style/MicRatingBar"         
        android:visibility="gone"
        android:layout_marginTop="10dip"               
        android:layout_marginBottom="10dip"
        />

...

<style name="MicRatingBar" parent="@android:style/Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/micratingstars</item>
    <item name="android:minHeight">34dip</item>
    <item name="android:maxHeight">34dip</item>               
</style>

...

<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+android:id/background" android:drawable="@drawable/star_background" />
    <item android:id="@+android:id/secondaryProgress" android:drawable="@drawable/star_secondaryprogress" />
    <item android:id="@+android:id/progress" android:drawable="@drawable/star_progress" /> </layer-list>

作为引用,这些是一些让我走到现在的计算器:

(但不幸的是没有解决我的具体问题。)

我尝试了很多不同的组合,就我而言,此处发布的代码最接近所需的行为和外观。就问题而言,“首秀”的分数绘制不正确。

我尝试使用例如invalidate,但我相信它的内部标志使其忽略他使请求无效。

最佳答案

我认为问题在于这些语句的顺序:

// Fine
rating.setIsIndicator(true);

// Fine
rating.setVisibility(View.VISIBLE);

// Problem - Although not documented, I think this should be 
// called after `setNumStars(int)`
rating.setStepSize(1);

// Switch with the statement above
rating.setNumStars(data.score_max);

// Fine
rating.setRating(data.score_cur);

那么,试试这个顺序:

rating.setIsIndicator(true);
rating.setVisibility(View.VISIBLE);
rating.setNumStars(data.score_max);
rating.setStepSize(1);
rating.setRating(data.score_cur);

至于//force repaint and set visible - how?,这不应该是必需的。 setRating(float) 应该强制更新。只需从代码的 ACTION_UP 部分删除 rating.setVisibility(View.INVISIBLE);。如果 RatingBar 仍然没有更新,请尝试 rating.requestLayout()。但是,请进一步阅读以获得更清洁的解决方案。

你说过 int starsint = (int) starsf + param_final__data.score_min; 得到了正确的值。我猜 param_final__data.score_cur = starsbyte; 会更新您的 ListView 数据。那为什么不直接调用 notifyDataSetChanged(),让适配器用正确的值更新 View 呢?不过,需要更改语句的顺序。

关于Android RatingBar 本身画错了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27139844/

有关Android RatingBar 本身画错了的更多相关文章

  1. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  2. ruby - 我的 Ruby IRC 机器人没有连接到 IRC 服务器。我究竟做错了什么? - 2

    require"socket"server="irc.rizon.net"port="6667"nick="RubyIRCBot"channel="#0x40"s=TCPSocket.open(server,port)s.print("USERTesting",0)s.print("NICK#{nick}",0)s.print("JOIN#{channel}",0)这个IRC机器人没有连接到IRC服务器,我做错了什么? 最佳答案 失败并显示此消息::irc.shakeababy.net461*USER:Notenoughparame

  3. ruby - 获取 ruby​​ 函数对象本身 - 2

    在Ruby中,一切都应该是一个对象。但是我有一个很大的问题是要以通常的方式定义函数对象,比如deff"foo"end与Python不同,f是函数结果,而不是函数本身。因此,f()、f、ObjectSpace.f都是"foo"。此外,f.methods仅返回字符串方法列表。如何访问函数对象本身? 最佳答案 您只需使用method方法。这将返回与该方法匹配的Method实例。一些例子:>>deff>>"foo">>end=>nil>>f=>"foo">>method(:f)=>#>>method(:f).methods=>[:==,:e

  4. ruby - 如何编写仅包含特定文件夹和文件夹本身的 Albacore zip 任务? - 2

    我正在尝试使用Albacore的ZipTask压缩rake构建的工件.我正在构建的解决方案包含三个项目,这些项目的工件需要单独压缩,但这里只提及ASP.NETMVC项目。这是解决方案的目录结构:rakefile.rbsolution.slnsrc/(otherprojectsthatarenotrelevant)website/(variousfoldersIdon'twantincludedintheartifacts)bin/Content/Scripts/Views/Default.aspxGlobal.asaxweb.config起初我写了这个任务:website_direct

  5. ruby - 我可以将一个本身需要一个 block 的 block 传递给 ruby​​ 中的 instance_exec 吗? - 2

    我期待代码foo=proc{puts"foo"}instance_exec(1,2,3,&foo)do|*args,&block|puts*argsblock.callputs"bar"end输出123foobar但是报错bothblockargandactualblockgiven我可以将一个本身需要一个block的block传递给ruby​​中的instance_exec吗? 最佳答案 &foo尝试将foo作为block传递给instance_exec,而您已经传递了一个显式block。省略与号发送foo就像任何其他参数一样(除

  6. ruby - 用ruby字符串中的值替换变量,字符串本身存储在变量中? - 2

    我有一个包含字符串的变量,在运行时我要替换存储在该字符串中的一些变量。例如..my_string="CongratsyouhavejoinedgroupName."groupName="*Nameofmygroup*"putsmy_string输出:-"Congratsyouhavejoined*nameofthegroup*"问题是:my_string="Congratsyouhavejoined#{groupName}"expectsgroupNamealreadyexists..butinmycaseihavetodefinemy_stringbeforevariableinit

  7. ruby-on-rails - 模型方法应该调用 'save' 本身吗? - 2

    假设我们在模型中有一个方法只需要调用已保存的记录可能会更新模型本身,因此之后需要再次保存模型“保存”调用是否应该像下面的代码一样发生在方法内部defresultsave!ifnew_record?#dosomefunkystuffherethatmayalsochangethemodelstate#...#Andcalculatethereturnvaluesearch_result="foo"#Let'ssay"foo"isthevaluewecalculatedsave!ifchanged?search_result#returnend还是应该由外部观察者(Controller)负

  8. ruby-on-rails - 如何为 belongs_to 本身的 Rails 模型编写迁移 - 2

    模型场景:Anodecanbelongtoaparentnodeandcanhavechildnodes.模型/节点.rbclassNodedb/migrations/20131031144907_create_nodes.rbclassCreateNodes然后我想迁移以添加关系:classAddNodesToNodes如何在迁移中添加has_many关系? 最佳答案 您已完成所有需要做的事情。您可以在此页面中找到更多信息:来源:http://guides.rubyonrails.org/association_basics.ht

  9. ruby - 为什么 Fortran 中的单元测试框架依赖于 Ruby 而不是 Fortran 本身? - 2

    总结:FRUIT只能与Fortran编译器一起使用,尽管使用Ruby可以增强其功能。查看以下作者AndrewChen的回答。===========================================似乎Fortran的可用单元测试框架(XUnit)包括:有趣的http://nasarb.rubyforge.org/水果http://sourceforge.net/projects/fortranxunit/胡言乱语http://flibs.sourceforge.net/ObjexxFTK(商业)http://www.objexx.com/ObjexxFTK.html在他们

  10. ruby - 在 Ruby 中,为什么在启动 irb 之后出现 foo.nil?说未定义的错误,@foo.nil?给出 "true"和 @@wah.nil?又报错了? - 2

    在Ruby1.8.7和1.9.2中相同:$irbruby-1.8.7-p302>foo.nil?NameError:undefinedlocalvariableormethod`foo'for#from(irb):1ruby-1.8.7-p302>@bar.nil?=>trueruby-1.8.7-p302>@@wah.nil?NameError:uninitializedclassvariable@@wahinObjectfrom(irb):3为什么实例变量与局部变量和类变量的处理方式不同? 最佳答案 在Ruby中,大多数未初始化

随机推荐