diff --git a/app/src/main/java/com/github/wanglu1209/banner/demo/MainActivity.java b/app/src/main/java/com/github/wanglu1209/banner/demo/MainActivity.java index c6036bb..66195b0 100644 --- a/app/src/main/java/com/github/wanglu1209/banner/demo/MainActivity.java +++ b/app/src/main/java/com/github/wanglu1209/banner/demo/MainActivity.java @@ -36,16 +36,16 @@ protected void onCreate(Bundle savedInstanceState) { * 最后需要调用开始轮播 * 个人建议在onPause()/onDestroy()方法中来停止 -- stopAutoPlay() */ - banner.setDot(R.drawable.no_selected_dot, R.drawable.selected_dot). - setDotGravity(Banner.CENTER). - setAdapter(ad). - setOnItemClickListener(new BannerPagerAdapter.onItemClickListener() { - @Override - public void onClick(int position) { - Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show(); - } - }). - startAutoPlay(); + banner.setDotGravity(Banner.CENTER). + setDot(R.drawable.no_selected_dot, R.drawable.selected_dot). + setAdapter(ad). + setOnItemClickListener(new BannerPagerAdapter.onItemClickListener() { + @Override + public void onClick(int position) { + Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show(); + } + }). + startAutoPlay(); } } diff --git a/bannerlibrary/src/main/java/com/github/wanglu1209/bannerlibrary/Banner.java b/bannerlibrary/src/main/java/com/github/wanglu1209/bannerlibrary/Banner.java index 98292f3..fc71ea3 100644 --- a/bannerlibrary/src/main/java/com/github/wanglu1209/bannerlibrary/Banner.java +++ b/bannerlibrary/src/main/java/com/github/wanglu1209/bannerlibrary/Banner.java @@ -132,6 +132,45 @@ public Banner setAdapter(BannerPagerAdapter adapter) { mPager.setAdapter(mAdapter); mPager.setCurrentItem(mAdapter.size); + + LinearLayout.LayoutParams dotParams = + new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, + ViewGroup.LayoutParams.WRAP_CONTENT); + /** + * 未选中小圆点的间距 + */ + dotParams.rightMargin = 12; + + /** + * 创建未选中的小圆点 + */ + for (int i = 0; i < mAdapter.size; i++) { + ImageView iv = new ImageView(mContext); + iv.setImageDrawable(mContext.getResources().getDrawable(mDot[0])); + iv.setLayoutParams(dotParams); + mDotGroup.addView(iv); + } + + + /** + * 添加到任务栈,当前所有任务完事之后添加已经选中的那个小圆点 + */ + post(new Runnable() { + @Override + public void run() { + + ImageView iv = new ImageView(mContext); + iv.setImageDrawable(mContext.getResources().getDrawable(mDot[1])); + FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); + /** + * 设置选中小圆点的左边距 + */ + params.leftMargin = (int) mDotGroup.getChildAt(0).getX(); + params.gravity = Gravity.BOTTOM; + mFrameLayout.addView(iv, params); + mSelectedDot = mFrameLayout.getChildAt(1); + } + }); return this; } @@ -157,42 +196,7 @@ public Banner setTime(int time) { public Banner setDot(int... dots) { mDot[0] = dots[0]; mDot[1] = dots[1]; - /** - * 添加到任务栈,当前所有任务完事之后添加已经选中的那个小圆点 - */ - post(new Runnable() { - @Override - public void run() { - - LinearLayout.LayoutParams dotParams = - new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, - ViewGroup.LayoutParams.WRAP_CONTENT); - /** - * 未选中小圆点的间距 - */ - dotParams.rightMargin = 12; - /** - * 创建未选中的小圆点 - */ - for (int i = 0; i < mAdapter.size; i++) { - ImageView iv = new ImageView(mContext); - iv.setImageDrawable(mContext.getResources().getDrawable(mDot[0])); - iv.setLayoutParams(dotParams); - mDotGroup.addView(iv); - } - ImageView iv = new ImageView(mContext); - iv.setImageDrawable(mContext.getResources().getDrawable(mDot[1])); - FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); - /** - * 设置选中小圆点的左边距 - */ - params.leftMargin = (int) mDotGroup.getChildAt(0).getX(); - params.gravity = Gravity.BOTTOM; - mFrameLayout.addView(iv, params); - mSelectedDot = mFrameLayout.getChildAt(1); - } - }); return this; }