简体   繁体   English

Android Studio-RatingBar setOnTouchListener无法正常工作

[英]Android studio - RatingBar setOnTouchListener not working properly

I am trying to setup a scrollView where inside I have a Grid layout with few rating bars in it. 我正在尝试设置一个scrollView ,在其中我有一个Grid layout其中没有几个rating bars When I click/touch on these the avarege value is sent to a final Rating Bar which is outside to the scrollView . 当我单击/触摸这些时,求值将被发送到scrollView外部的最终Rating Bar

Everything is working however, each ratingbar need to be clicked/touched two times in order to send its value. 一切正常,但是每个ratingbar需要单击/触摸两次才能发送其值。

Any Idea why is this happening? 知道为什么会这样吗? If I move the grid layout outside from the scrollView the rating bars work as expected(on one click/touch) 如果我将grid layoutscrollView移到外部,则rating bars将按预期工作(单击/触摸一次)

Layout 布局

the Code I am using to send the value from rating bar is: 我用来从评分栏发送值的代码是:

    Valutazione[0].setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    onClickRate1();
                    return false;
                }
            });

            Valutazione[1].setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    onClickRate1();
                    return false;
                }
            });

            Valutazione[2].setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    onClickRate1();
                    return false;
                }
            });

....

public void onClickRate1()
    {

        RatingBar Valutazione[] =
                {(RatingBar) findViewById(R.id.ratingBar1), (RatingBar) findViewById(R.id.ratingBar2),
                        (RatingBar) findViewById(R.id.ratingBar3), (RatingBar) findViewById(R.id.ratingBar4),
                        (RatingBar) findViewById(R.id.ratingBar5), (RatingBar) findViewById(R.id.ratingBar6),
                        (RatingBar) findViewById(R.id.ratingBar7), (RatingBar) findViewById(R.id.ratingBar8),
                        (RatingBar) findViewById(R.id.ratingBar9), (RatingBar) findViewById(R.id.ratingBar10)
                };
        RatingBar rateTot =(RatingBar) findViewById(R.id.ratingBartot);

        float Valore[]={0,1,2,3,4,5,6,7,8,9, 10};
        Valore[0]=0;Valore[1]=0;
        Valore[2]=0;Valore[3]=0;
        Valore[4]=0;Valore[5]=0;
        Valore[6]=0;Valore[7]=0;
        Valore[8]=0;Valore[9]=0;
        Valore[10]=0;

        int j=0, divisore=0;

        TextView finalResult = (TextView) findViewById(R.id.textrate);


        while(j<=9) {

            Valore[j] = Valutazione[j].getRating();

            if(Valore[j] > 0) divisore++;

            j++;
        }

        Valore[10] = ( Valore[0] +  Valore[1] +  Valore[2] +  Valore[3] +  Valore[4] +  Valore[5] +  Valore[6] + Valore[7] +  Valore[8] + Valore[9]) / divisore;


        totalRating(rateTot, Valore[10], finalResult);

    }



    private void totalRating(RatingBar totalBar, float valoreTotale, TextView totalText) {

        totalBar.setRating(valoreTotale);
}

Instead of setOnTouchListener use setOnRatingBarChangeListener . 代替setOnTouchListener使用setOnRatingBarChangeListener

1) import android.widget.RatingBar.OnRatingBarChangeListener; 1)导入android.widget.RatingBar.OnRatingBarChangeListener;

then: 然后:

Valutazione[0].setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {

                onClickRate1();
            }

        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM