Android studio 里实现轮播图具体代码
时间: 2023-12-20 20:07:45 浏览: 118
Android轮播图的实现
实现轮播图的具体代码可以参考以下步骤:
1. 在布局文件中添加ViewPager和指示器Indicator:
```
<RelativeLayout
android:id="@+id/rl_banner"
android:layout_width="match_parent"
android:layout_height="200dp">
<android.support.v4.view.ViewPager
android:id="@+id/vp_banner"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:id="@+id/ll_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:orientation="horizontal"/>
</RelativeLayout>
```
2. 创建一个适配器BannerPagerAdapter继承自PagerAdapter,在其中实现轮播图的数据绑定:
```
public class BannerPagerAdapter extends PagerAdapter {
private List<String> mDataList;
private Context mContext;
private OnBannerItemClickListener mListener;
public BannerPagerAdapter(Context context, List<String> dataList, OnBannerItemClickListener listener) {
mContext = context;
mDataList = dataList;
mListener = listener;
}
@Override
public int getCount() {
return mDataList.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, final int position) {
ImageView imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
Glide.with(mContext).load(mDataList.get(position)).into(imageView);
container.addView(imageView);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.onBannerItemClick(position);
}
}
});
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
public interface OnBannerItemClickListener {
void onBannerItemClick(int position);
}
}
```
3. 在Activity或Fragment中初始化ViewPager和Indicator,并设置适配器:
```
private ViewPager mViewPager;
private LinearLayout mIndicatorLayout;
private List<String> mDataList = new ArrayList<>();
private BannerPagerAdapter mBannerPagerAdapter;
private int mCurrentPosition = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = findViewById(R.id.vp_banner);
mIndicatorLayout = findViewById(R.id.ll_indicator);
initDataList();
initIndicator();
initViewPager();
}
private void initDataList() {
mDataList.add("http://xxx/1.jpg");
mDataList.add("http://xxx/2.jpg");
mDataList.add("http://xxx/3.jpg");
mDataList.add("http://xxx/4.jpg");
mDataList.add("http://xxx/5.jpg");
}
private void initIndicator() {
for (int i = 0; i < mDataList.size(); i++) {
ImageView imageView = new ImageView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.leftMargin = 10;
params.rightMargin = 10;
imageView.setLayoutParams(params);
imageView.setImageResource(R.drawable.ic_indicator_normal);
mIndicatorLayout.addView(imageView);
}
}
private void initViewPager() {
mBannerPagerAdapter = new BannerPagerAdapter(this, mDataList, new BannerPagerAdapter.OnBannerItemClickListener() {
@Override
public void onBannerItemClick(int position) {
// 轮播图点击事件处理
}
});
mViewPager.setAdapter(mBannerPagerAdapter);
mViewPager.setCurrentItem(mDataList.size() * 1000);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
int realPosition = position % mDataList.size();
mIndicatorLayout.getChildAt(mCurrentPosition).setImageResource(R.drawable.ic_indicator_normal);
mIndicatorLayout.getChildAt(realPosition).setImageResource(R.drawable.ic_indicator_selected);
mCurrentPosition = realPosition;
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
mHandler.sendEmptyMessageDelayed(0, 3000);
}
private Handler mHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
mHandler.sendEmptyMessageDelayed(0, 3000);
return true;
}
});
```
通过以上代码,即可在Android Studio里实现轮播图。
阅读全文