ViewPager
画小圆点
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
本地图片
public class MainActivity extends Activity { private ViewPager vp; private GridView gv; private List<View> list = new ArrayList<View>(); private View v1; private View v4; private View v2; private View v3; private RadioGroup rg; private List<Bean> list1; private LinearLayout lin; private ArrayList<ImageView> listima; private Handler hand = new Handler() { public void handleMessage(android.os.Message msg) { int item = vp.getCurrentItem(); // 条目索引+1 item++; vp.setCurrentItem(item); hand.sendEmptyMessageDelayed(0, 2000); }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Init(); } public void Init() { lin = (LinearLayout) findViewById(R.id.la); rg = (RadioGroup) findViewById(R.id.radioGroup1); vp = (ViewPager) findViewById(R.id.vp); gv = (GridView) findViewById(R.id.gridView1); v1 = View.inflate(this, R.layout.v1, null); v2 = View.inflate(this, R.layout.v2, null); v3 = View.inflate(this, R.layout.v3, null); v4 = View.inflate(this, R.layout.v4, null); list.add(v1); list.add(v2); list.add(v3); list.add(v4); vp.setAdapter(new PagerAdapter() { @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(list.get(position % list.size())); } @Override public Object instantiateItem(ViewGroup container, int position) { View v = list.get(position % list.size()); v.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { // 按下 case MotionEvent.ACTION_DOWN: // 移除所有的回调和消息 hand.removeCallbacksAndMessages(null); break; // 抬起 case MotionEvent.ACTION_UP: hand.sendEmptyMessageDelayed(0, 2000); break; // 取消的时候,继续 case MotionEvent.ACTION_CANCEL: hand.sendEmptyMessageDelayed(0, 3000); break; default: break; }// TODO Auto-generated method stub return true; } }); container.addView(v); return v; } }); vp.setCurrentItem(list.size() * 100000); // 设置延时切换 发送延时消息 hand.sendEmptyMessageDelayed(0, 2000); vp.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int arg0) { for (int j = 0; j < listima.size(); j++) { if (j == arg0 % listima.size()) { listima.get(j).setImageResource(R.drawable.ite2); } else { listima.get(j).setImageResource(R.drawable.ite); } } for (int i = 0; i < list.size(); i++) { if (arg0 == i) { RadioButton rb = (RadioButton) rg.getChildAt(i); rb.setChecked(true); } } } @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 } }); /* rg.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.radio0: vp.setCurrentItem(0); break; case R.id.radio1: vp.setCurrentItem(1); break; case R.id.radio2: vp.setCurrentItem(2); break; case R.id.radio3: vp.setCurrentItem(3); break; default: break; } } }); */ // 加载小圆点 loadima(); // get(); } public void loadima() { listima = new ArrayList<ImageView>(); for (int i = 0; i < list.size(); i++) { ImageView image = new ImageView(this); if (i == 0) { image.setImageResource(R.drawable.ite2); } else { image.setImageResource(R.drawable.ite); } LayoutParams params = new LayoutParams(15, 15); params.setMargins(5, 0, 5, 0); lin.addView(image, params); listima.add(image); } }
<corners android:radius="8dp"/>
<solid android:color="#fff"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="8dp"/>
<solid android:color="#88000000"/>
</shape>
网络图片
View v = View.inflate(context, R.layout.vpitem, null);
ImageView ima = (ImageView) v.findViewById(R.id.vpitemima);
用imageloader加载并 拼接路径
ImageLoader image = ImageLoader.getInstance();
image.displayImage(
"http://172.17.29.120/localuser/loupengfei/kaoshi/gv/"
+ list.get(position%list.size()), ima);
如果是从网上获取图片
public void getvp() {
String st2 = "http://172.17.29.120/localuser/loupengfei/kaoshi/gv/ad.json";
Util uti = new Util() {
@Override
protected void onPostExecute(String result) {
Gson gson = new Gson();
List<Beanvp> list = gson.fromJson(result,
new TypeToken<List<Beanvp>>() {
}.getType());
Log.d(TAG, list.size() + "size");
listima = new ArrayList<String>();
for (Beanvp beanvp : list) {
listima.add(beanvp.src);
}
Log.d(TAG, listima.size() + "ASsize");
Pageradapter pag = new Pageradapter(listima, MainActivity.this);
vp.setAdapter(pag);
inityuandian();
vp.setCurrentItem(listima.size()*100000);
handler.sendEmptyMessageDelayed(0, 2000);
super.onPostExecute(result);
}
};
uti.execute(st2);
}
推荐阅读
-
Android利用ViewPager实现带小圆球的图片滑动
-
使用ViewPager实现android软件使用向导功能实现步骤
-
实现轮转广告带底部指示的自定义ViewPager控件
-
TabLayout关联ViewPager后不显示文字的解决方法
-
Android 使用ViewPager实现轮播图效果
-
Android中TabLayout+ViewPager实现tab和页面联动效果
-
Android编程开发ScrollView中ViewPager无法正常滑动问题解决方法
-
Android Viewpager实现无限循环轮播图
-
Android使用ViewPager实现滚动广告
-
Android解决viewpager嵌套滑动冲突并保留侧滑菜单功能