android简单轮播器代码,简单实现android轮播图
时间: 2023-10-30 17:04:45 浏览: 97
以下是一个简单的Android轮播器的实现代码:
1. 在布局文件中添加ViewPager和指示器(Indicator):
```xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:id="@+id/indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal"/>
</RelativeLayout>
```
2. 创建Adapter和ViewHolder:
```java
public class MyPagerAdapter extends PagerAdapter {
private Context context;
private List<Integer> imageList;
public MyPagerAdapter(Context context, List<Integer> imageList) {
this.context = context;
this.imageList = imageList;
}
@Override
public int getCount() {
return imageList.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = LayoutInflater.from(context).inflate(R.layout.item_pager, null);
ImageView imageView = view.findViewById(R.id.imageView);
imageView.setImageResource(imageList.get(position));
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public MyViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
}
}
```
3. 在Activity中初始化ViewPager和指示器:
```java
private ViewPager viewPager;
private LinearLayout indicator;
private List<Integer> imageList;
private List<ImageView> indicatorList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.viewPager);
indicator = findViewById(R.id.indicator);
imageList = new ArrayList<>();
imageList.add(R.drawable.image1);
imageList.add(R.drawable.image2);
imageList.add(R.drawable.image3);
indicatorList = new ArrayList<>();
for (int i = 0; i < imageList.size(); i++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.indicator_selector);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(10, 0, 10, 0);
indicator.addView(imageView, params);
indicatorList.add(imageView);
}
MyPagerAdapter adapter = new MyPagerAdapter(this, imageList);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);
indicatorList.get(0).setSelected(true);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
for (int i = 0; i < indicatorList.size(); i++) {
indicatorList.get(i).setSelected(false);
}
indicatorList.get(position).setSelected(true);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
```
这是一个简单的轮播器实现,但是它还有很多可以改进的地方,比如使用ViewPager2、使用更加灵活的Indicator等。
阅读全文