How to create Custom Ratings bar in Android

前端 未结 13 1465
故里飘歌
故里飘歌 2020-11-22 06:28

Hello all i need to perform Ratings in my application... SO i need to create custom Ratings bar... Can Anyone Help me in this?

13条回答
  •  离开以前
    2020-11-22 07:06

    You can have 5 imageview with defalut image as star that is empty and fill the rating bar with half or full image base on rating.

     public View getView(int position, View convertView, ViewGroup parent) {
         LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         View grid=inflater.inflate(R.layout.griditem, parent, false);
         imageView=(ImageView)grid.findViewById(R.id.grid_prod);
         imageView.setImageResource(imgId[position]);
         imgoff =(ImageView)grid.findViewById(R.id.offer);
         tv=(TextView)grid.findViewById(R.id.grid_text);
         tv.setText(namesArr[position]);
         tv.setTextColor(Color.BLACK);
         tv.setPadding(0, 2, 0, 0);
       sta=(ImageView)grid.findViewById(R.id.imageView);
        sta1=(ImageView)grid.findViewById(R.id.imageView1);
        sta2=(ImageView)grid.findViewById(R.id.imageView2);
        sta3=(ImageView)grid.findViewById(R.id.imageView3);
        sta4=(ImageView)grid.findViewById(R.id.imageView4);
        Float rate=rateFArr[position];
    
    
       if(rate==5 || rate==4.5)
        {
            sta.setImageResource(R.drawable.full__small);
            sta1.setImageResource(R.drawable.full__small);
            sta2.setImageResource(R.drawable.full__small);
            sta3.setImageResource(R.drawable.full__small);
            if(rate==4.5)
            {
                sta4.setImageResource(R.drawable.half_small);
            }
            else
            {
                sta4.setImageResource(R.drawable.full__small);
            }
        }
        if(rate==4 || rate==3.5)
        {
            sta.setImageResource(R.drawable.full__small);
            sta1.setImageResource(R.drawable.full__small);
            sta2.setImageResource(R.drawable.full__small);
         if(rate==3.5)
            {
                sta3.setImageResource(R.drawable.half_small);
            }
            else
            {
                sta3.setImageResource(R.drawable.full__small);
            }
        }
        if(rate==3 || rate==2.5)
        {
            sta.setImageResource(R.drawable.full__small);
            sta1.setImageResource(R.drawable.full__small);
           if(rate==2.5)
            {
                sta2.setImageResource(R.drawable.half_small);
            }
            else
            {
                sta2.setImageResource(R.drawable.full__small);
            }
        }
        if(rate==2 || rate==1.5)
        {
        sta.setImageResource(R.drawable.full__small);
         if(rate==1.5)
            {
                sta1.setImageResource(R.drawable.half_small);
            }
            else
            {
                sta1.setImageResource(R.drawable.full__small);
            }
        }
        if(rate==1 || rate==0.5)
        {
            if(rate==1)
            sta.setImageResource(R.drawable.full__small);
            else
                sta.setImageResource(R.drawable.half_small);
    
        }
        if(rate>5)
        {
            sta.setImageResource(R.drawable.full__small);
            sta1.setImageResource(R.drawable.full__small);
            sta2.setImageResource(R.drawable.full__small);
            sta3.setImageResource(R.drawable.full__small);
            sta4.setImageResource(R.drawable.full__small);
        }
    
        // rb=(RatingBar)findViewById(R.id.grid_rating);
         //rb.setRating(rateFArr[position]);
            return grid;
        }
    

提交回复
热议问题