在Fragment中使用Android轮播控件的步骤如下:
<com.youth.banner.Banner
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="200dp"/>
public class MyFragment extends Fragment {
private Banner mBanner;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my, container, false);
mBanner = view.findViewById(R.id.banner);
List<Integer> images = new ArrayList<>();
images.add(R.drawable.image1);
images.add(R.drawable.image2);
images.add(R.drawable.image3);
mBanner.setImages(images)
.setImageLoader(new GlideImageLoader())
.start();
return view;
}
}
public class GlideImageLoader extends ImageLoader {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
Glide.with(context)
.load(path)
.into(imageView);
}
}
这样就可以在Fragment中使用Android轮播控件了。