Skip to content

Commit 346c7cf

Browse files
author
Edwin Wu
committed
custom indicator background
New API mLoopViewPagerLayout.setNormalBackground(R.drawable.normal_background);/ /默认指示器颜色 mLoopViewPagerLayout.setSelectedBackground(R.drawable.selected_backgroun d);//选中指示器颜色
1 parent e62722c commit 346c7cf

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

app/src/main/java/com/edwin/loopviewpager/fragment/EmptyLoopViewPagerFragment.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ protected void initData() {
5151
mLoopViewPagerLayout.setLoop_duration(1000);//滑动的速率(毫秒)
5252
mLoopViewPagerLayout.setLoop_style(LoopStyle.Empty);//轮播的样式-默认empty
5353
mLoopViewPagerLayout.setIndicatorLocation(IndicatorLocation.Center);//指示器位置-中Center
54+
mLoopViewPagerLayout.setNormalBackground(R.drawable.normal_background);//默认指示器颜色
55+
mLoopViewPagerLayout.setSelectedBackground(R.drawable.selected_background);//选中指示器颜色
5456
L.e("LoopViewPager Empty 参数设置完毕");
5557

5658
//TODO 初始化
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<shape xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:shape="rectangle">
3+
4+
<corners android:radius="8dp" />
5+
<solid android:color="#FFF" />
6+
</shape>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<shape xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:shape="rectangle">
5+
6+
<corners android:radius="8dp" />
7+
<solid android:color="#FF6EB4" />
8+
</shape>

library/src/main/java/com/github/why168/LoopViewPagerLayout.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import android.content.Context;
44
import android.graphics.Rect;
55
import android.os.Handler;
6+
import android.os.Looper;
67
import android.os.Message;
8+
import android.support.annotation.DrawableRes;
79
import android.support.v4.view.ViewCompat;
810
import android.support.v4.view.ViewPager;
911
import android.util.AttributeSet;
@@ -40,7 +42,6 @@
4042
* @since JDK1.8
4143
*/
4244
public 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

Comments
 (0)