帮我写一个android studio实现图片轮播的代码
时间: 2023-07-20 15:17:28 浏览: 97
安卓实现图片的轮播
好的,以下是一个简单的 Android Studio 实现图片轮播的代码。
首先,在布局文件中添加 ViewPager 和指示器(Indicator):
```xml
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="200dp" />
<LinearLayout
android:id="@+id/indicatorLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal" />
```
接着,在 MainActivity 中设置 ViewPager 的 Adapter 和指示器:
```java
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private LinearLayout indicatorLayout;
private int[] imageIds = {R.drawable.image1, R.drawable.image2, R.drawable.image3};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.viewPager);
indicatorLayout = findViewById(R.id.indicatorLayout);
// 设置 Adapter
ImagePagerAdapter adapter = new ImagePagerAdapter(getSupportFragmentManager(), imageIds);
viewPager.setAdapter(adapter);
// 设置指示器
for (int i = 0; i < imageIds.length; 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);
indicatorLayout.addView(imageView, params);
}
indicatorLayout.getChildAt(0).setEnabled(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 < indicatorLayout.getChildCount(); i++) {
indicatorLayout.getChildAt(i).setEnabled(false);
}
indicatorLayout.getChildAt(position).setEnabled(true);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
```
最后,创建一个 PagerAdapter,用于显示图片:
```java
public class ImagePagerAdapter extends FragmentPagerAdapter {
private int[] imageIds;
public ImagePagerAdapter(@NonNull FragmentManager fm, int[] imageIds) {
super(fm);
this.imageIds = imageIds;
}
@NonNull
@Override
public Fragment getItem(int position) {
return ImageFragment.newInstance(imageIds[position]);
}
@Override
public int getCount() {
return imageIds.length;
}
}
```
其中,ImageFragment 是一个自定义 Fragment,用于显示图片:
```java
public class ImageFragment extends Fragment {
private static final String ARG_IMAGE_ID = "imageId";
private int imageId;
public ImageFragment() {
}
public static ImageFragment newInstance(int imageId) {
ImageFragment fragment = new ImageFragment();
Bundle args = new Bundle();
args.putInt(ARG_IMAGE_ID, imageId);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
imageId = getArguments().getInt(ARG_IMAGE_ID);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_image, container, false);
ImageView imageView = rootView.findViewById(R.id.imageView);
imageView.setImageResource(imageId);
return rootView;
}
}
```
这样就实现了一个简单的图片轮播。
阅读全文