这篇文章将为大家详细讲解有关Android使用Recyclerview实现图片轮播效果的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
企业建站必须是能够以充分展现企业形象为主要目的,是企业文化与产品对外扩展宣传的重要窗口,一个合格的网站不仅仅能为公司带来巨大的互联网上的收集和信息发布平台,创新互联公司面向各种领域:柴油发电机等成都网站设计、全网整合营销推广解决方案、网站设计等建站排名服务。
思路:
1.准备m张图片
1.使用Recyclerview实现,返回无数个(实际Interge.MAXVALUE)item,第n个item显示第n%m张图片
3.使用recyclerview.scrollBy 每个一段时间水平滚动一段距离
4.通过layoutManager.findFirstVisibleItemPosition()获取当前显示的第一个View是第几个item,上面的ImageView显示对应de图片
实现代码:
XML文件
1.activity布局文件activity_recy.xml
<?xml version="1.0" encoding="utf-8"?>
2.适配器布局item_horizon.xml
<?xml version="1.0" encoding="utf-8"?>
Activity
public class HorizontalActivity extends AppCompatActivity implements RecyAdapter.OnItemClickListener { private String TAG="HorizontalActivity"; @BindView(R.id.img) ImageView img; @BindView(R.id.recyclerview) RecyclerView recyclerview; private Integer[] mImgIds = {R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p5, R.drawable.pic1, R.drawable.pic5, R.drawable.pic6}; private Listdatas; private RecyAdapter recyAdapter; private Handler mHandler=new Handler(); private LinearLayoutManager layoutManager; private int oldItem=0; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recy); ButterKnife.bind(this); initData(); initRecy(); img.setImageResource(datas.get(0)); recyAdapter.setOnItemClickListener(this); } Runnable scrollRunnable=new Runnable() { @Override public void run() { recyclerview.scrollBy(3,0); // int firstItem = layoutManager.findFirstVisibleItemPosition(); int firstItem=layoutManager.findFirstVisibleItemPosition(); if(firstItem!=oldItem&&firstItem>0){ oldItem=firstItem; img.setImageResource(datas.get(oldItem%datas.size())); } Log.e(TAG, "run: firstItem:"+firstItem ); mHandler.postDelayed(scrollRunnable,10); } }; private void initRecy() { recyAdapter=new RecyAdapter(this,datas); layoutManager = new LinearLayoutManager(this); layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); recyclerview.setLayoutManager(layoutManager); recyclerview.setAdapter(recyAdapter); } private void initData() { datas=new ArrayList<>(); for (int i = 0; i
适配器RecyAdapter
public class RecyAdapter extends RecyclerView.Adapterimplements View.OnClickListener { private Context context; private List datas; private OnItemClickListener onItemClickListener; public RecyAdapter(Context context, List datas) { this.context = context; this.datas = datas; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.item_horizontal, parent, false); ViewHolder vh=new ViewHolder(view); view.setOnClickListener(this); return vh; } @Override public void onBindViewHolder(ViewHolder holder, int position) { int newPos=position%datas.size(); holder.img.setImageResource(datas.get(newPos)); holder.itemView.setTag(position); } @Override public int getItemCount() { return Integer.MAX_VALUE; } @Override public void onClick(View view) { if(onItemClickListener!=null){ onItemClickListener.onItemClick(view, (Integer) view.getTag()); } } class ViewHolder extends RecyclerView.ViewHolder { ImageView img; public ViewHolder(View itemView) { super(itemView); img=itemView.findViewById(R.id.img); } } public void setOnItemClickListener(OnItemClickListener listener){ this.onItemClickListener=listener; } interface OnItemClickListener{ void onItemClick(View view,int tag); } }
关于Android使用Recyclerview实现图片轮播效果的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。