33import android .content .Context ;
44import android .graphics .Rect ;
55import android .os .Handler ;
6+ import android .os .Looper ;
67import android .os .Message ;
8+ import android .support .annotation .DrawableRes ;
79import android .support .v4 .view .ViewCompat ;
810import android .support .v4 .view .ViewPager ;
911import android .util .AttributeSet ;
4042 * @since JDK1.8
4143 */
4244public class LoopViewPagerLayout extends RelativeLayout {
43- private FrameLayout indicatorFrameLayout ;
4445 private ViewPager loopViewPager ;
4546 private LinearLayout indicatorLayout ;
4647 private LinearLayout animIndicatorLayout ;
@@ -52,12 +53,16 @@ public class LoopViewPagerLayout extends RelativeLayout {
5253 private ArrayList <BannerInfo > bannerInfos ;//banner data
5354 private TextView animIndicator ;//Little red dot on the move
5455 private TextView [] indicators ;//Initializes the white dots
56+ @ DrawableRes
57+ private int normalBackground = R .drawable .indicator_normal_background ;
58+ @ DrawableRes
59+ private int selectedBackground = R .drawable .indicator_selected_background ;
5560 private static final int MESSAGE_LOOP = 5 ;
5661 private int loop_ms = 4000 ;//loop speed(ms)
5762 private int loop_style = -1 ; //loop style(enum values[-1:empty,1:depth 2:zoom])
5863 private IndicatorLocation indicatorLocation = IndicatorLocation .Center ; //Indicator Location(enum values[1:left,0:depth 2:right])
5964 private int loop_duration = 2000 ;//loop rate(ms)
60- private Handler handler = new Handler () {
65+ private Handler handler = new Handler (Looper . getMainLooper () ) {
6166 @ Override
6267 public void dispatchMessage (Message msg ) {
6368 super .dispatchMessage (msg );
@@ -117,7 +122,7 @@ private void initializeView() {
117122 addView (loopViewPager , loop_params );
118123
119124 //TODO FrameLayout
120- indicatorFrameLayout = new FrameLayout (getContext ());
125+ FrameLayout indicatorFrameLayout = new FrameLayout (getContext ());
121126 LayoutParams f_params = new LayoutParams (LinearLayout .LayoutParams .WRAP_CONTENT , ((int ) (20 * density )));
122127 f_params .addRule (RelativeLayout .CENTER_HORIZONTAL );//android:layout_centerHorizontal="true"
123128 f_params .addRule (RelativeLayout .ALIGN_PARENT_BOTTOM );//android:layout_alignParentBottom="true"
@@ -264,7 +269,7 @@ private void InitIndicator() {
264269 params .setMargins (0 , 0 , 0 , 0 );
265270 }
266271 indicators [i ].setLayoutParams (params );
267- indicators [i ].setBackgroundResource (R . drawable . indicator_normal_background );//设置默认的背景颜色
272+ indicators [i ].setBackgroundResource (getNormalBackground () );//设置默认的背景颜色
268273 indicatorLayout .addView (indicators [i ]);
269274 }
270275
@@ -275,7 +280,7 @@ private void InitLittleRed() {
275280 LinearLayout .LayoutParams params = new LinearLayout .LayoutParams (size , size );
276281 animIndicator = new TextView (getContext ());
277282 animIndicator .setGravity (Gravity .CENTER );
278- animIndicator .setBackgroundResource (R . drawable . indicator_selected_background );//设置选中的背景颜色
283+ animIndicator .setBackgroundResource (getSelectedBackground () );//设置选中的背景颜色
279284 animIndicatorLayout .addView (animIndicator , params );
280285 }
281286
@@ -348,6 +353,22 @@ public ViewPager getLoopViewPager() {
348353 return loopViewPager ;
349354 }
350355
356+ public int getNormalBackground () {
357+ return normalBackground ;
358+ }
359+
360+ public void setNormalBackground (@ DrawableRes int normalBackground ) {
361+ this .normalBackground = normalBackground ;
362+ }
363+
364+ public int getSelectedBackground () {
365+ return selectedBackground ;
366+ }
367+
368+ public void setSelectedBackground (@ DrawableRes int selectedBackground ) {
369+ this .selectedBackground = selectedBackground ;
370+ }
371+
351372 /**
352373 * OnPageChangeListener
353374 */
0 commit comments