简介:
本篇博客主要介绍的是如何使用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"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical"> <ImageVIEw androID:layout_wIDth="300dp" androID:layout_height="350dp" androID:layout_gravity="center_horizontal" androID:ID="@+ID/img" androID:src="@drawable/p5" androID:scaleType="fitXY" /> <androID.support.v7.Widget.RecyclerVIEw androID:layout_wIDth="match_parent" androID:layout_height="150dp" androID:layout_margintop="10dp" androID:ID="@+ID/recyclervIEw" ></androID.support.v7.Widget.RecyclerVIEw></linearLayout>
2.适配器布局item_horizon.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" androID:orIEntation="vertical" > <ImageVIEw androID:layout_wIDth="100dp" androID:layout_height="match_parent" androID:ID="@+ID/img" androID:layout_marginleft="5dp" androID:layout_marginRight="5dp" androID:scaleType="fitXY" /></linearLayout>
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 List<Integer> datas; 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 <mimgIDs.length ; i++) { datas.add(mimgIDs[i]); } } @OverrIDe protected voID onResume() { super.onResume(); mHandler.postDelayed(scrollRunnable,10); } @OverrIDe protected voID onStop() { super.onStop(); mHandler.removeCallbacks(scrollRunnable); } @OverrIDe public voID onItemClick(VIEw vIEw,int tag) { Toast.makeText(this,"第"+tag+"张图片被点击了",Toast.LENGTH_SHORT).show(); }}
适配器RecyAdapter
public class RecyAdapter extends RecyclerVIEw.Adapter<RecyAdapter.VIEwHolder> implements VIEw.OnClickListener { private Context context; private List<Integer> datas; private OnItemClickListener onItemClickListener; public RecyAdapter(Context context,List<Integer> 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实现图片水平自动循环滚动效果全部内容,希望文章能够帮你解决Android使用Recyclerview实现图片水平自动循环滚动效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)