在Android中,可以使用RecyclerView或ListView来实现滚动图。以下是使用RecyclerView实现滚动图的步骤:
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<Drawable> images;
public MyAdapter(List<Drawable> images) {
this.images = images;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_image, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Drawable image = images.get(position);
holder.imageView.setImageDrawable(image);
}
@Override
public int getItemCount() {
return images.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
}
}
}
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
RecyclerView recyclerView = findViewById(R.id.recyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);
List<Drawable> images = new ArrayList<>();
images.add(getResources().getDrawable(R.drawable.image1));
images.add(getResources().getDrawable(R.drawable.image2));
// 添加更多图片...
MyAdapter adapter = new MyAdapter(images);
recyclerView.setAdapter(adapter);
通过以上步骤,就可以在Android中实现滚动图。