首先在glide中导入banner包,然后fragment中建立一个banner
implementation ("io.github.youth5201314:banner:2.2.3")
implementation ("com.github.bumptech.glide:glide:4.15.1")
<com.example.Banner
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
再然后再fragment中写入下列代码
public class YourFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.your_fragment_layout, container, false);
List<String> list = new ArrayList<>();
list.add("https://img-blog.csdnimg.cn/direct/f3a246c70ee942fa8463e124b676817c.png");
list.add("https://img-blog.csdnimg.cn/direct/3a26643d33454f17bf307fa0416649b1.png");
Banner banner = view.findViewById(R.id.banner);
banner.setAdapter(new BannerImageAdapter<String>(list) {
@Override
public void onBindView(BannerImageHolder holder, String data, int position, int size) {
Glide.with(holder.itemView)
.load(data)
.into(holder.imageView);
}
});
return view;
}
}