Giter Site home page Giter Site logo

xlhratingbar's Introduction

you can custom star style

How to use

add the library as dependency

Add the library as dependency to your build.gradle file.

dependencies {
	//other dependencies...
	compile 'com.xingliuhua:xlhratingbar_lib:2.1'
}

implement interface

implement IRatingViewand to custom your star UI.of course,you can see SimpleRatingView

public interface IRatingView {
    /**
     * get image resource state by posi and state
     *
     * @param posi
     * @param state
     * @return
     */
    int getStateRes(int posi, int state);

    /**
     * @param rating
     * @param numStars
     * @param position from 0
     * @return
     */
    int getCurrentState(float rating, int numStars, int position);

    /**
     * get a ImageView,you can set LinearLayout.LayoutParams for your ImageView
     *
     * @param context
     * @param posi    from 0
     * @return
     */
    ImageView getRatingView(Context context,int numStars, int posi);

    /**
     * not selected
     */
    public static final int STATE_NONE = 0;
    /**
     * select half
     */
    public static final int STATE_HALF = 1;
    /**
     * selected
     */
    public static final int STATE_FULL = 2;

}

SimpleRatingView:

public class SimpleRatingView implements IRatingView {

    @Override
    public int getStateRes(int posi, int state) {
        int id = R.drawable.ic_star_border_red_400_36dp;
        switch (state) {
            case STATE_NONE:
                id = R.drawable.ic_star_border_red_400_36dp;
                break;
            case STATE_HALF:
                id = R.drawable.ic_star_half_red_400_36dp;
                break;
            case STATE_FULL:
                id = R.drawable.ic_star_red_400_36dp;
                break;
            default:
                break;
        }
        return id;
    }

    @Override
    public int getCurrentState(float rating, int numStars, int position) {
        position++;
        float dis = position - rating;
        if (dis <= 0) {
            return STATE_FULL;
        }
        if (dis == 0.5) {
            return STATE_HALF;
        }
        if (dis > 0.5) {
            return STATE_NONE;
        }
        return 0;
    }


    @Override
    public ImageView getRatingView(Context context, int numStars, int posi) {
        ImageView imageView = new ImageView(context);
        return imageView;
    }
}

use XLHRatingBar in xml

<com.example.xlhratingbar_lib.XLHRatingBar
		xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        app:numStars="5"
        app:rating="5"
        app:ratingViewClass="com.example.xlhratingbar.SimpleRatingView"
        />

you can set star num ,rating and ratingView class name

set view

you can set rating view by code or xml set view by code:

 xlhRatingBar.setRatingView(new SimpleRatingView5());

other:

 xlhRatingBar.setNumStars(7);
 xlhRatingBar.setRating(5);
 xlhRatingBar.setOnRatingChangeListener(new XLHRatingBar.OnRatingChangeListener() {
            @Override
            public void onChange(float rating, int numStars) {
                Toast.makeText(getApplicationContext(), "rating:" + rating, Toast.LENGTH_SHORT).show();
            }
 });
findViewById(R.id.btn_get_rating).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "rating:" + xlhRatingBar.getRating(), Toast.LENGTH_SHORT).show();
            }
});

xlhratingbar's People

Contributors

xingliuhua avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.