// 无限轮播
布局文件
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent">代码实现<android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="200dp" /> <LinearLayout android:id="@+id/line" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal" android:layout_alignBottom="@+id/vp" /></RelativeLayout>
public class MyBase extends PagerAdapter{ private ShouyeFragment context; private ArrayList<ImageView> arrayList; public MyBase(ShouyeFragment context, ArrayList<ImageView> arrayList) { this.context = context; this.arrayList = arrayList; } public int getCount() { return Integer.MAX_VALUE; } public boolean isViewFromObject(View arg0, Object arg1) { return arg0==arg1; } public Object instantiateItem(ViewGroup container, int position) { ImageView imageView=arrayList.get(position%arrayList.size()); container.addView(imageView); return imageView; } public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } }Activity
public class ShouyeFragment extends Fragment { private ArrayList<ImageView> arrayList2; private ArrayList<ImageView> arrayList; private DisplayImageOptions displayImageOptions; private String[] image = new String[] { "http://l2.51fanli.net//tuan//images//1//5806eac956808.jpg", "http://l2.51fanli.net//tuan//images//b//580991bb30560.jpg", "http://l0.51fanli.net//tuan//images//b//58115f2593dc3.jpg", "http://l2.51fanli.net//tuan//images//0//57923840b054d.jpg", "http://l2.51fanli.net//tuan//images//e//58101e11ab164.jpg" }; private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; private String mParam1; private String mParam2; private ViewPager vp; private LinearLayout layout; private Handler handler=new Handler(){ public void handleMessage(Message msg) { int currentItem = vp.getCurrentItem(); currentItem++; vp.setCurrentItem(currentItem); } }; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_shouye, container, false); vp = (ViewPager) view.findViewById(R.id.vp); layout = (LinearLayout) view.findViewById(R.id.line); Setdata(); SetdataYuan(); vp.setAdapter(new MyBase(this,arrayList)); handler.sendEmptyMessageDelayed(1,2000); //viewpage设置监听 vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { public void onPageSelected(int arg0) { for (int i = 0; i < image.length; i++) { if (arg0%arrayList2.size()==i) { arrayList2.get(i).setImageResource(R.drawable.a2); }else { arrayList2.get(i).setImageResource(R.drawable.a1); } } } public void onPageScrolled(int arg0, float arg1, int arg2) { } public void onPageScrollStateChanged(int arg0) { } }); return view; } //给view赋值的方法 private void Setdata() { displayImageOptions = new DisplayImageOptions.Builder().build(); arrayList = new ArrayList<ImageView>(); for (int i = 0; i <image.length; i++) { ImageView imageView=new ImageView(getActivity()); ImageLoader.getInstance().displayImage(image[i],imageView,displayImageOptions); arrayList.add(imageView); } } //小圆点赋值 @SuppressLint("NewApi") private void SetdataYuan() { arrayList2 = new ArrayList<>(); for (int i = 0; i < image.length; i++) { ImageView imageView=new ImageView(getActivity()); if (i==0) { imageView.setImageResource(R.drawable.a2); }else { imageView.setImageResource(R.drawable.a1); } LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(20,20); layoutParams.setMargins(5, 0, 5, 0); layout.addView(imageView, layoutParams); arrayList2.add(imageView); } } }
<?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="#cccccc"/> </shape>