ViewPager添加小圆点很简单,但是如果是网络图片可能就不太好做了,所以我这里给出一种方法,当然你也可以用其他的
1.主界面xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.cqytjr.www.cheji.Activity.BigImageActivity"> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/viewpager"> </android.support.v4.view.ViewPager> <LinearLayout android:layout_width="wrap_content" android:layout_height="40dp" android:id="@+id/ll_pager_num" android:orientation="horizontal" android:layout_alignParentBottom="true" android:layout_marginBottom="45dp" android:layout_centerHorizontal="true" > </LinearLayout> </RelativeLayout>
activity代码:
public class BigImageActivity extends BaseActivity { private ViewPager pager; private List <ImageView>list; private LinearLayout mNumLayout; private List<Button>pagerList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_big_image); initView(); } @Override protected void initView() { super.initView(); initWithData(); mNumLayout = (LinearLayout) findViewById(R.id.ll_pager_num); pagerList = new ArrayList<Button>(); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_page_indicator); for (int i = 0; i < list.size(); i++) { Button bt = new Button(this); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); //为小圆点左右添加间距 params.leftMargin = 10; params.rightMargin = 10; //手动给小圆点一个大小 params.height = bitmap.getWidth(); params.width = bitmap.getHeight(); // bt.setLayoutParams(new ViewGroup.LayoutParams(bitmap.getWidth(),bitmap.getHeight())); // bt.setBackgroundResource(R.drawable.ic_page_indicator); mNumLayout.addView(bt,params); pagerList.add(bt); } pager = (ViewPager)findViewById(R.id.viewpager); PagerAdapter adapter = new PagerAdapter() { @Override public int getCount() { return list.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(list.get(position)); return list.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(list.get(position)); } }; pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { choseItem(position); } @Override public void onPageScrollStateChanged(int state) { } }); pager.setAdapter(adapter); pager.setCurrentItem(0); choseItem(0); } public void initWithData(){ int[]strings = {R.drawable.ic_test_0,R.drawable.ic_test_1,R.drawable.ic_test_2, R.drawable.ic_test_3,R.drawable.ic_test_4,R.drawable.ic_test_5,R.drawable.ic_test_6}; list = new ArrayList<ImageView>(); for (int i=0;i<strings.length;i++){ ImageView imageView = new ImageView(getApplicationContext()); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setImageResource(strings[i]); list.add(imageView); } } private void choseItem(int i){ Log.d("BigImageActivity",i+"==========="); for (Button button:pagerList){ button.setBackgroundResource(R.drawable.ic_page_indicator); } Button button = pagerList.get(i); button.setBackgroundResource(R.drawable.ic_page_indicator_focused); } }
时间: 2024-11-03 12:50:45