I am trying to setup a scrollView
where inside I have a Grid layout
with few rating bars
in it. When I click/touch on these the avarege value is sent to a final Rating Bar
which is outside to the scrollView
.
Everything is working however, each ratingbar
need to be clicked/touched two times in order to send its value.
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)
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
.
1) import android.widget.RatingBar.OnRatingBarChangeListener;
then:
Valutazione[0].setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
onClickRate1();
}
});