我需要代码
时间: 2024-02-29 12:53:30 浏览: 15
以下是一个简单的实现轮播图的代码,仅供参考:
1. 布局文件:
```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="200dp" />
<LinearLayout
android:id="@+id/layout_indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/viewpager"
android:layout_marginBottom="10dp"
android:gravity="center_horizontal"
android:orientation="horizontal" />
</RelativeLayout>
```
2. Adapter 类:
```
public class MyPagerAdapter extends PagerAdapter {
private Context mContext;
private int[] mImageIds;
public MyPagerAdapter(Context context, int[] imageIds) {
mContext = context;
mImageIds = imageIds;
}
@Override
public int getCount() {
return mImageIds.length;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(mImageIds[position]);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((ImageView) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
```
3. Activity 类:
```
public class MainActivity extends AppCompatActivity {
private ViewPager mViewPager;
private LinearLayout mLayoutIndicator;
private int[] mImageIds = {R.drawable.image1, R.drawable.image2, R.drawable.image3};
private List<ImageView> mImageViews = new ArrayList<>();
private int mCurrentPosition = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = findViewById(R.id.viewpager);
mLayoutIndicator = findViewById(R.id.layout_indicator);
// 初始化轮播图
initBanner();
// 设置轮播图监听
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mCurrentPosition = position;
updateIndicator();
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
/**
* 初始化轮播图
*/
private void initBanner() {
for (int i = 0; i < mImageIds.length; i++) {
ImageView imageView = new ImageView(this);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(mImageIds[i]);
mImageViews.add(imageView);
// 添加指示器
ImageView indicator = new ImageView(this);
indicator.setImageResource(R.drawable.dot_selector);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 0, 10, 0);
mLayoutIndicator.addView(indicator, lp);
}
// 设置 Adapter
MyPagerAdapter adapter = new MyPagerAdapter(this, mImageIds);
mViewPager.setAdapter(adapter);
// 设置轮播图初始位置
int initPosition = Integer.MAX_VALUE / 2 - Integer.MAX_VALUE / 2 % mImageIds.length;
mViewPager.setCurrentItem(initPosition);
// 更新指示器
updateIndicator();
}
/**
* 更新指示器
*/
private void updateIndicator() {
for (int i = 0; i < mLayoutIndicator.getChildCount(); i++) {
ImageView indicator = (ImageView) mLayoutIndicator.getChildAt(i);
if (i == mCurrentPosition % mImageIds.length) {
indicator.setSelected(true);
} else {
indicator.setSelected(false);
}
}
}
}
```
需要注意的是,上面的代码仅是一个简单的实现,还有很多细节需要处理,比如图片的加载、缓存、异步处理等等。如果需要更加完善的轮播图功能,建议使用现成的第三方库,比如 AndroidImageSlider、LoopViewPager 等等。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)