import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.GridLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends Activity { public ViewPager pager = null; public ArrayList<View> viewContainter = new ArrayList<View>(); public String TAG = "tag"; public ArrayList<ImageView> imgList = new ArrayList<ImageView>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pager = (ViewPager) this.findViewById(R.id.viewpager); View nullView1 = new View(MainActivity.this); nullView1.setBackgroundColor(Color.TRANSPARENT); viewContainter.add(nullView1); int pageCount = 3; int count = 1; int row = 2; int col = 8; int btnWidth = 0; for(int pageNum = 0; pageNum<pageCount; pageNum++){ GridLayout gd = new GridLayout(this); btnWidth = (getScreenWidth() - gd.getPaddingLeft() * 2) / col; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if(count>pageCount*row*col-5){ break; } GridLayout.Spec rowSpec = GridLayout.spec(i); GridLayout.Spec columnSpec = GridLayout.spec(j); GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpec, columnSpec); params.width=btnWidth-24; params.height=btnWidth-24; params.leftMargin = 12; params.rightMargin = 12; params.topMargin = 0; params.bottomMargin = 12; ImageView imageView = new ImageView(this); imageView.setLayoutParams(new ViewGroup.LayoutParams(btnWidth, btnWidth)); imageView.setBackgroundColor(Color.WHITE); imageView.setImageResource(R.drawable.ic_launcher); final int finalCount = count; imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this, finalCount +"",Toast.LENGTH_SHORT).show(); } }); gd.addView(imageView, params); count++; } } viewContainter.add(gd); } View nullView2 = new View(MainActivity.this); nullView2.setBackgroundColor(Color.TRANSPARENT); viewContainter.add(nullView2); pager.getLayoutParams().height = (btnWidth-24)*row+12+12; pager.setAdapter(new PagerAdapter() { //viewpager中的组件数量 @Override public int getCount() { return viewContainter.size(); } //滑动切换的时候销毁当前的组件 @Override public void destroyItem(ViewGroup container, int position, Object object) { ((ViewPager) container).removeView(viewContainter.get(position)); } //每次滑动的时候生成的组件 @Override public Object instantiateItem(ViewGroup container, int position) { ((ViewPager) container).addView(viewContainter.get(position)); return viewContainter.get(position); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public int getItemPosition(Object object) { return super.getItemPosition(object); } }); LinearLayout point = (LinearLayout) findViewById(R.id.point); for(int i=0;i<pageCount;i++){ ImageView imageView = new ImageView(this); imageView.setLayoutParams(new ViewGroup.LayoutParams(20, 20)); imageView.setImageResource(R.drawable.ic_launcher); imageView.setPadding(5,5,5,5); point.addView(imageView); imgList.add(imageView); } setImageChoose(0); pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrollStateChanged(int arg0) { } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageSelected(int arg0) { Log.e(TAG, "------selected:" + arg0); if (arg0 == 0) { pager.setCurrentItem(arg0 + 1); setImageChoose(0); } else if (arg0 == viewContainter.size() - 1) { pager.setCurrentItem(arg0 - 1); setImageChoose(viewContainter.size() - 3); } else{ setImageChoose(arg0 - 1); } } }); pager.setCurrentItem(1); } private void setImageChoose(int index){ for(Object view : imgList){ ImageView img = (ImageView) view; img.setBackgroundColor(Color.WHITE); } ImageView view = (ImageView) imgList.get(index); view.setBackgroundColor(Color.RED); } private int getScreenWidth(){return 720;} }
<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=".MainActivity" > <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="100dp" > </android.support.v4.view.ViewPager> <LinearLayout android:gravity="center_horizontal" android:layout_height="30dp" android:layout_width="match_parent" android:id="@+id/point" android:layout_below="@+id/viewpager" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"> </LinearLayout> </RelativeLayout>