viewpager无限轮播加小圆点
2017-10-25 18:59
211 查看
导包:
res下drawable
dot_focus.xml:
dot_nomal.xml
layout下:
activity_main.xml
记得加权限:
借鉴于http://www.cnblogs.com/ldou/p/5383169.html小姐姐的,如果有看不懂的可以去查看源码
compile 'com.jiechic.library:xUtils:2.6.14'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
compile 'com.android.support:support-annotations:26.0.0-alpha1'
res下drawable
dot_focus.xml:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#ff0000" /> <corners android:radius="8dip" /> </shape>
dot_nomal.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#ffffff" /> <corners android:radius="8dip" /> </shape>
layout下:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas 4000 .android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="code.xp.wuxianlunbo.MainActivity"> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:orientation="horizontal" android:id="@+id/dotcontaint" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:gravity="center" /> </RelativeLayout> dot_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <View android:id="@+id/dotView" android:layout_width="8dp" android:layout_height="8dp" android:layout_margin="10dp" android:background="@drawable/dot_nomal" /> </LinearLayout> mainActivity
import android.os.Bundle; import android.os.Handler; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import com.lidroid.xutils.BitmapUtils; import java.util.ArrayList; import java.util.Timer; import java.util.TimerTask; public class MainActivity extends AppCompatActivity { private ViewPager vp; private LinearLayout dotcontaint; ArrayList<ImageView> imgs; ArrayList<View> dots; int currentIndex = 0; int olderIndex = 0; //图片数据 private String[] image_url = new String[]{ "http://pic8.nipic.com/20100701/5290458_114840036316_2.jpg", "http://img3.3lian.com/2013/s1/20/d/57.jpg", "http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg", "http://a0.att.hudong.com/15/08/300218769736132194086202411_950.jpg"}; Handler h = new Handler() { public void handleMessage(android.os.Message msg) { vp.setCurrentItem(currentIndex);//设置此次要显示的pager //切换选中的圆点 dots.get(olderIndex).setBackgroundResource(R.drawable.dot_nomal);//设置上次选中的圆点为不选中 dots.get(currentIndex % image_url.length).setBackgroundResource(R.drawable.dot_focus);//设置当前选中的圆点为选中 olderIndex = currentIndex % image_url.length; } ; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } public void init() { vp = (ViewPager) findViewById(R.id.vp); dotcontaint = (LinearLayout) findViewById(R.id.dotcontaint); //获得网络图片,配置给Veiwpager getImageViewList(); //获得圆点 getDotList(); //设置第一个圆点为选中状态 dots.get(0).setBackgroundResource(R.drawable.dot_focus); vp.setAdapter(new MyVpAdapger());//配置pager页 int currentItem = Integer.MAX_VALUE / 2 - Integer.MAX_VALUE / 2 % image_url.length; vp.setCurrentItem(currentItem); //通过定时器,制作自动划动效果 Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { currentIndex = vp.getCurrentItem() + 1;//下一个页 h.sendEmptyMessage(0x123);//在此线程中,不能操作ui主线程 } }, 3000, 2000); } private void getDotList() { // TODO Auto-generated method stub dots = new ArrayList<View>(); //循环图片数组,创建对应数量的dot for (int i = 0; i < image_url.length; i++) { View view = LayoutInflater.from(this).inflate(R.layout.dot_layout, null);//加载布局 View dot = view.findViewById(R.id.dotView);//得到布局中的dot点组件 //收集dot dots.add(dot); //把布局添加到线性布局 dotcontaint.addView(view); } } class MyVpAdapger extends PagerAdapter { @Override public int getCount() { // TODO Auto-generated method stub return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; } @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub // super.destroyItem(container, position, object); // ImageView img = imgs.get(position); container.removeView((View) object); } @Override public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub int item = position % image_url.length; container.addView(imgs.get(item)); return imgs.get(item); } } /** * 到网络获得图片信息,并赋值到ImageView中 */ private void getImageViewList() { // TODO Auto-generated method stub imgs = new ArrayList<ImageView>(); BitmapUtils btUtil = new BitmapUtils(this); //加载图片 for (int i = 0; i < image_url.length; i++) { ImageView img = new ImageView(this); img.setScaleType(ImageView.ScaleType.FIT_XY); btUtil.display(img, image_url[i]); imgs.add(img); } } }
记得加权限:
<uses-permission android:name="android.permission.INTERNET" />
借鉴于http://www.cnblogs.com/ldou/p/5383169.html小姐姐的,如果有看不懂的可以去查看源码
相关文章推荐
- ViewPager+小圆点的无限轮播
- 使用自制绘图实现viewpager无限轮播的小圆点
- viewpager +小圆点 +无限轮播图
- Viewpager无限轮播加小圆点
- ViewPager自动无限轮播加小圆点
- ViewPager加小圆点无限轮播
- ViewPager+小圆点进行无限轮播
- Android 开发中的应用:ViewPager无限轮播(网络图片,小圆点)
- ViewPager无限轮播图+小圆点+HttpURLConnection +handler+Thread
- ViewPager的无限轮播,和shape的小圆点使用,及webview显示进度条
- ViewPager小圆点无限轮播
- ViewPager实现无限轮播+小圆点
- 集成实现ViewPager的无限轮播和圆点变动
- viewpager无限轮播,画小圆点,小圆点的布局
- viewpager无限轮播,画小圆点,小圆点的布局
- ViewPager小圆点无限轮播
- Viewpager实现小圆点无限轮播
- viewpager无限轮播、加小圆点
- viewpager无限轮播(从网络上获取图片小圆点)
- TabLayout+ViewPager无限轮播加小圆点,XRecyclerView下拉刷新,加载更多