android studio实现在页面上方有一个图片栏imageview,循环播放三张图片,也可以通过滑动切换
时间: 2024-02-22 11:57:16 浏览: 110
可以通过使用ViewPager和FragmentPagerAdapter来实现在页面上方有一个图片栏ImageView,并且可以循环播放三张图片,也可以通过滑动切换。下面是实现步骤:
1. 在布局文件中添加ViewPager和ImageView组件。
```
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="200dp" />
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="@drawable/ic_launcher_background" />
</LinearLayout>
```
2. 创建三个Fragment用来显示轮播图。
```
public class BannerFragment extends Fragment {
private int mPosition;
public static BannerFragment newInstance(int position) {
BannerFragment fragment = new BannerFragment();
Bundle args = new Bundle();
args.putInt("position", position);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPosition = getArguments().getInt("position");
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ImageView imageView = new ImageView(getActivity());
switch (mPosition) {
case 0:
imageView.setImageResource(R.drawable.banner_1);
break;
case 1:
imageView.setImageResource(R.drawable.banner_2);
break;
case 2:
imageView.setImageResource(R.drawable.banner_3);
break;
}
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
return imageView;
}
}
```
3. 创建一个FragmentPagerAdapter用来管理轮播图的Fragment。
```
public class BannerPagerAdapter extends FragmentPagerAdapter {
public BannerPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return BannerFragment.newInstance(position % 3);
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
}
```
4. 在Activity中设置ViewPager和ImageView的适配器,并设置ViewPager的滑动监听器和ImageView的图片资源。
```
public class MainActivity extends AppCompatActivity {
private ViewPager mViewPager;
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = findViewById(R.id.view_pager);
mImageView = findViewById(R.id.image_view);
BannerPagerAdapter adapter = new BannerPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(adapter);
mViewPager.setCurrentItem(Integer.MAX_VALUE / 2);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mImageView.setImageResource(getBannerResId(position % 3));
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
mImageView.setImageResource(getBannerResId(0));
}
private int getBannerResId(int position) {
switch (position) {
case 0:
return R.drawable.banner_1;
case 1:
return R.drawable.banner_2;
case 2:
return R.drawable.banner_3;
default:
return R.drawable.ic_launcher_background;
}
}
}
```
这样就可以实现在页面上方有一个图片栏ImageView,并且可以循环播放三张图片,也可以通过滑动切换。
阅读全文