Android Recyclerview to create automatic cycling effect _android

Source: Internet
Author: User
Tags static class

Look at the effect chart first

Main areas of treatment:
1, Recyclerview in the adapter of the item individuals can be unlimited polling.
2, Recyclerview automatic sliding
3, the finger press when the slide stop, the finger lifted and continue to automatically slide

public class Autopollrecyclerview extends Recyclerview {private static final long time_auto_poll = 16;
 Autopolltask Autopolltask; private Boolean running; Indicates whether the private Boolean canrun;//indicator is automatically polled and can be automatically polled if it is not needed to set false public Autopollrecyclerview
  Nullable AttributeSet attrs) {Super (context, attrs);
 Autopolltask = new Autopolltask (this);
  Static Class Autopolltask implements Runnable {private final weakreference<autopollrecyclerview> mreference; Holding external class references with weak references-> prevent memory leaks public autopolltask (Autopollrecyclerview reference) {this.mreference = new weakreference& Lt
  Autopollrecyclerview> (reference);
   @Override public void Run () {Autopollrecyclerview Recyclerview = Mreference.get (); 
    if (Recyclerview!= null && recyclerview.running &&recyclerview.canrun) {Recyclerview.scrollby (2, 2);
   Recyclerview.postdelayed (Recyclerview.autopolltask,recyclerview.time_auto_poll); }}//Open: If running, stop-> before opening PUBlic void Start () {if (running) stop ();
  Canrun = true;
  running = true;
 Postdelayed (Autopolltask,time_auto_poll);
  public void Stop () {running = false;
 Removecallbacks (Autopolltask);
    @Override public boolean ontouchevent (motionevent e) {switch (e.getaction ()) {case Motionevent.action_down:
    if (running) stop ();
   Break Case MotionEvent.ACTION_UP:case MotionEvent.ACTION_CANCEL:case MotionEvent.ACTION_OUTSIDE:if (canrun) St
    Art ();
  Break
 Return Super.ontouchevent (e);
 }
}

Adapter Processing: A Onbindviewholder () method that mainly deals with GetItemCount () and data padding

public class Autopolladapter extends recyclerview.adapter<baseviewholder> {
 Private final context mcontext;< C2/>private final list<string> Mdata;
 Public Autopolladapter (context context, list<string> List) {
  This.mcontext = context;
  This.mdata = list;
 }
 @Override public
 Baseviewholder Oncreateviewholder (viewgroup parent, int viewtype) {
  View view = Layoutinflater.from (Mcontext). Inflate (R.layout.item_auto_poll, parent, false);
  Baseviewholder holder = new Baseviewholder (view);
  return holder;
 }
 @Override public
 void Onbindviewholder (baseviewholder holder, int position) {
  String data = Mdata.get ( Position%mdata.size ());
  Holder.settext (R.id.tv_content,data);
 }
 @Override public
 int GetItemCount () {return
  integer.max_value}
 }
}

Finally attach the code for the activity call

public class Autopollrecycleractivity extends Baseactivity {
 @Override
 protected void OnCreate (Bundle Savedinstancestate) {
  super.oncreate (savedinstancestate);
  Setcontentview (r.layout.activity_auto_poll);
  Inittoolbar ();
  Initview ();
 }
 private void Initview () {
  Autopollrecyclerview Mrecyclerview = (autopollrecyclerview) Findviewbyid (r.id.rv_ Recycleview);
  list<string> list = new arraylist<> ();
  for (int i = 0; i < 5;) {
   List.add ("Item:" + ++i);
  }
  Autopolladapter adapter = new Autopolladapter (this, list);
  Mrecyclerview.setlayoutmanager (this, linearlayoutmanager.horizontal, false) (new Linearlayoutmanager);
  Mrecyclerview.additemdecoration (This, divideritemdecoration.horizontal_list) (new divideritemdecoration);
  Mrecyclerview.setadapter (adapter);
  if (true)//Ensure that the total number of itemcount exceeds the screen width-> handle
   mrecyclerview.start ();
 }

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.