Android uses Recyclerview to implement a horizontal scrolling control _android

Source: Internet
Author: User
Tags getcolor

Objective

I believe we all know that there are many ways to implement the Android scrolling control, and it is easier to use Recyclerview. To do a simple age scrolling control, let's look at the use of Recyclerview, mainly the following points:

(1) Aligns the center position of the control.

(2) Calculate the rolling distance.

(3) Highlight Center view.

(4) Display the center data in real time.

(5) automatically align when stopping.

(6) When scrolling, set the button state switch.

Effect

1. The framework

The main concern Recyclerview part of the logic.

 /** * Initialization age slider/private void Initagelist () {Linearlayoutmanager Mlayoutmanager = new Linearlayoutmanager (g
  Etactivity (), linearlayoutmanager.horizontal, false);
  Mrvagelist.setlayoutmanager (Mlayoutmanager);
  Mageadapter = new Personageadapter (start_num, end_num);
  Mrvagelist.setadapter (Mageadapter); Mrvagelist.addonscrolllistener (New Recyclerview.onscrolllistener () {@Override public void onscrollstatechanged (

    Recyclerview recyclerview, int newstate) {super.onscrollstatechanged (Recyclerview, newstate);

    Mbdownstep.setenabled (FALSE); The effect is displayed when paused, otherwise it causes the redraw of the exception if (newstate = recyclerview.scroll_state_idle) {Mageadapter.highlightitem (getmiddleposit
     Ion ());
     Mrvagelist.scrolltoposition (GetScrollPosition ());
     Mlastvalue = Getmiddleposition ();

     Userinfomanager.setage (getmiddleposition () + start_num); Mbdownstep.setenabled (TRUE); Not available when sliding, can be stopped @Override public void onscrolled (recyclerview recyclerview, int dx, int DY) {//value is a real-time increment of Mtvagevalue.settext (string.valueof (getmiddleposition () + start_num));

  }
  });
 Mageadapter.highlightitem (Getmiddleposition ()); }

Set a horizontal layout

Linearlayoutmanager Mlayoutmanager =
    new Linearlayoutmanager (Getactivity (), Linearlayoutmanager.horizontal, FALSE);
  Mrvagelist.setlayoutmanager (Mlayoutmanager);

Add adapter to set the start and end positions.

  Mageadapter = new Personageadapter (start_num, end_num);
  Mrvagelist.setadapter (Mageadapter);

2. Adapter

Adapter to customize some functions, such as pointing to the central location, data linkage display, highlighting and Item so on.

/** * Age Adapter * <p> * Created by Wangchenlong on 15/11/12. */public class Personageadapter extends recyclerview.adapter<personageadapter.ageitemviewholder> {public static final int item_num = 7; The number of item owned per line must be odd private int mfrom; Start private int mTo; Terminate private int mhighlight =-1;
  Highlight public personageadapter (int from, int to) {mfrom = from;
 MTo = to; @Override public Ageitemviewholder Oncreateviewholder (viewgroup parent, int viewtype) {View item = LAYOUTINFLATER.F
    Rom (Parent.getcontext ()).

  Inflate (R.layout.view_age_item, parent, false);
  Set the width of the item viewgroup.layoutparams LP = Item.getlayoutparams ();

  Lp.width = Getitemstdwidth ();
 return new Ageitemviewholder (item); @Override public void Onbindviewholder (ageitemviewholder holder, int position) {Holder.gettextview (). SetText (String

  . valueof (Mfrom + position));
   Highlight if (isselected (position)) {Holder.gettextview (). Settextsize (30); Holder.gettextview (). SetTextColor (Chunyuapp.getappcontext (). Getresources (). GetColor (R.color.black));
   else {Holder.gettextview (). Settextsize (20);
  Holder.gettextview (). SetTextColor (Chunyuapp.getappcontext (). Getresources (). GetColor (R.color.gray_line));
  }//Highlight center, update before and after position public void Highlightitem (int position) {mhighlight = position;
  int offset = ITEM_NUM/2;
 for (int i = Position-offset I <= position + offset; ++i) notifyitemchanged (i);
 }//Determine if the highlight public boolean isselected (int position) {return mhighlight = = position;
 @Override public int GetItemCount () {return mto-mfrom + 1; //Get the standard width public static int getitemstdwidth () {Displaymetrics displaymetrics = Chunyuapp.getappcontext (). Getresour
  CES (). Getdisplaymetrics ();
 return displaymetrics.widthpixels/item_num;

  }//Viewholder public class Ageitemviewholder extends Recyclerview.viewholder {private TextView mtextview;

   Public Ageitemviewholder (View Itemview) {super (Itemview); MtextView = (TextView) Itemview.findviewbyid (R.id.item_age_value);
  Mtextview.settag (this);
  Public TextView Gettextview () {return mtextview; }
 }
}

Each cell width is one of the odd points of the screen width, and the screen is filled with the start point to the center.

 @Override public Ageitemviewholder Oncreateviewholder (viewgroup parent, int viewtype) {
  View item = Layoutinflater.from (Parent.getcontext ()).
    Inflate (R.layout.view_age_item, parent, false);

  Set the width of the item
  viewgroup.layoutparams LP = Item.getlayoutparams ();
  Lp.width = Getitemstdwidth ();

  return new Ageitemviewholder (item);
 }

Singular Item number, the center indicator must point to Item the center of the Centre.

Updates the style according to the selected state Item .

 @Override public void Onbindviewholder (ageitemviewholder holder, int position) {
  Holder.gettextview (). SetText ( String.valueof (Mfrom + position));

  Highlight
  if (isselected (position)) {
   Holder.gettextview (). Settextsize ();
   Holder.gettextview (). SetTextColor (Chunyuapp.getappcontext (). Getresources (). GetColor (R.color.black));
  else {
   Holder.gettextview (). Settextsize ();
   Holder.gettextview (). SetTextColor (Chunyuapp.getappcontext (). Getresources (). GetColor (R.color.gray_line));
  }
 

Set the highlight, highlight the center position, restore both sides, notify adapter redraw Viewholder.

 Highlight Center, update before and after position public
 void Highlightitem (int position) {
  mhighlight = position;
  int offset = ITEM_NUM/2;
  for (int i = Position-offset I <= position + offset; ++i)
   notifyitemchanged (i);
 }

 Determine if the highlight public
 boolean isselected (int position) {return
  mhighlight = = position;
 }
Notifyitemchanged () will redraw the selected page.

Gets the width, sets the number of display lines Item to singular, and the center points to one Item .

 Gets the standard width public
 static int getitemstdwidth () {
  Displaymetrics displaymetrics = Chunyuapp.getappcontext (). Getresources (). Getdisplaymetrics ();
  return displaymetrics.widthpixels/item_num;
 }

Note that Recyclerview cannot move half a unit, and each line is singular, it will point to the center.

3. Rolling logic

When scrolling, real-time updates the page display; Update highlighting and storing data at stop time; When scrolling ends, activate the button.

  Mrvagelist.addonscrolllistener (New Recyclerview.onscrolllistener () {@Override public void onscrollstatechanged (Recy
    Clerview recyclerview, int newstate) {super.onscrollstatechanged (Recyclerview, newstate);

    Mbdownstep.setenabled (FALSE); The effect is displayed when paused, otherwise it causes the redraw of the exception if (newstate = recyclerview.scroll_state_idle) {Mageadapter.highlightitem (getmiddleposit
     Ion ());
     Mrvagelist.scrolltoposition (GetScrollPosition ());
     Mlastvalue = Getmiddleposition ();

     Userinfomanager.setage (getmiddleposition () + start_num); Mbdownstep.setenabled (TRUE); Not available when sliding, stop can} @Override public void onscrolled (recyclerview recyclerview, int dx, int dy) {//value is real time
   Increase Mtvagevalue.settext (string.valueof (getmiddleposition () + start_num));

  }
  });
 Mageadapter.highlightitem (Getmiddleposition ()); /** * Get Middle position * * @return Current value * * Private int getmiddleposition () {return getscrollposition () + (Personagead Apter.
 ITEM_NUM/2); }/** * Get the sliding value, sliding offset/grid width * * @return Current value * * Private int getscrollposition () {return (int) (double) Mrvagelist.computehor
 Izontalscrolloffset ()/(double) personageadapter.getitemstdwidth ()); }

Determines the distance of the scroll unit, offset total distance/single Item width.

 private int getscrollposition () {return
  (int) (double) Mrvagelist.computehorizontalscrolloffset ()
    /double ) personageadapter.getitemstdwidth ());
 

computeHorizontalScrollOffset()Gets the offset total displacement of the recyclerview.

Middle position, plus half row Item number

 private int getmiddleposition () {return
  getscrollposition () + (PERSONAGEADAPTER.ITEM_NUM/2);
 }

Determines whether scrolling stops.

if (newstate = = Recyclerview.scroll_state_idle)

Automatically aligns when scrolling is stopped.

Mrvagelist.scrolltoposition (GetScrollPosition ());

The call needs to be highlighted Item .

Mageadapter.highlightitem (Getmiddleposition ());

Note that when scrolling stops, the update is highlighted better, otherwise the redraw speed is slower and the scrolling effect is affected.

Sliding effect

Summarize

Well, that's the whole story of this article, and we can customize the various scroll bars according to these! Hopefully this article will help.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.