public class MainActivity extends Activity {
//准备数据
ArrayList<InfoBean> list=null;
InfoBean info=null;
ViewPager viewPager;
LinearLayout ll_dots;
private ImageView[] dots;
/** ViewPager页数 */
private int viewPager_size;
//当前页
private int currentIndex;
//gridView 页面item的数量
int pageItemCount;
//页面的宽高
int width;
int height;
//保存每个GridView的视图
ArrayList<View> viewlist=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WindowManager wm=(WindowManager)this.getSystemService(this.WINDOW_SERVICE);
width=wm.getDefaultDisplay().getWidth();
height=wm.getDefaultDisplay().getHeight();
Log.e("width", width+"");
Log.e("height", height+"");
list=getData();
Log.e("size", list.size()+"");
//初始化视图
initView();
}
private void initView() {
viewPager=(ViewPager) findViewById(R.id.vPager);
ll_dots=(LinearLayout) findViewById(R.id.ll_dots);
initDots();
setAdapter();
}
private void setAdapter() {
viewlist=new ArrayList<View>();
for(int i=0;i<viewPager_size;i++){
viewlist.add(getGridViewItem(i)); //36
}
viewPager.setAdapter(new ViewPager_GridView_Adapter(viewlist));
}
//每个GridView页面中的布局
private View getGridViewItem(int index) {
LayoutInflater inflater=(LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
View layout=inflater.inflate(R.layout.channel_viewpage_gridview, null);
GridView gridView=(GridView) layout.findViewById(R.id.vp_gv);
int col=(width/160) >2 ?(width/160):3;
Log.e("col", col+"");//3 4
gridView.setNumColumns(col);
//每个页面的adapter
ViewPager_GV_ItemAdapter adapter=new ViewPager_GV_ItemAdapter(this, list, index, pageItemCount);
gridView.setAdapter(adapter);
//设置gridView中点击Item事件
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, position + currentIndex * pageItemCount+"", Toast.LENGTH_SHORT).show();
if(null !=list.get(position + currentIndex * pageItemCount).getOnClickListener()){
list.get(position+currentIndex*pageItemCount).getOnClickListener().ongvItemClickListener(view);
}
}
});
return gridView;
}
private void initDots() {
int col = (width/160) >2 ? (width/160) :3; //3 4
int row = (height/400) >4?(height/400):2; //2 2
pageItemCount = col * row; //每一页可装item //6
//gridView 的页数
viewPager_size = list.size() / pageItemCount +1; //5
if (0 < viewPager_size) {
ll_dots.removeAllViews();
if (1 == viewPager_size) {
ll_dots.setVisibility(View.GONE);
} else if (1 < viewPager_size) {
ll_dots.setVisibility(View.VISIBLE);
for (int j = 0; j < viewPager_size; j++) {
ImageView image = new ImageView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(10, 10); //dot的宽高
params.setMargins(3, 0, 3, 0);
image.setBackgroundResource(R.drawable.dot_unselected);
ll_dots.addView(image, params);
}
}
}
if (viewPager_size != 1) {
dots = new ImageView[viewPager_size];
for (int i = 0; i < viewPager_size; i++) {
//从布局中填充dots数组
dots[i] = (ImageView) ll_dots.getChildAt(i);
//dots[i].setEnabled(true);
//dots[i].setTag(i);
}
currentIndex = 0; //当前页
//dots[currentIndex].setEnabled(false);
dots[currentIndex].setBackgroundResource(R.drawable.dot_selected);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
setCurDot(arg0);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
}
private ArrayList<InfoBean> getData() {
list=new ArrayList<InfoBean>();
for(int i=0;i<46;i++){
info=new InfoBean("苹果"+i, R.drawable.apple, i);
list.add(info);
}
return list;
}
private void setCurDot(int position) {
if (position < 0 || position > viewPager_size - 1 || currentIndex == position) {
return;
}
for(int i=0;i<dots.length;i++){
dots[i].setBackgroundResource(R.drawable.dot_unselected);
}
//dots[positon].setEnabled(false);
// dots[currentIndex].setEnabled(true);
dots[position].setBackgroundResource(R.drawable.dot_selected);
currentIndex = position;
}
} |
|