ListView Split Line, Recycleview split Line

Source: Internet
Author: User

The Recycleview split line needs to be defined by itself and is not by default. The code is as follows

Package Com.ipd.east.eastapplication.adapter;import Android.content.context;import Android.content.res.TypedArray ; Import Android.graphics.canvas;import Android.graphics.paint;import Android.graphics.rect;import Android.graphics.drawable.drawable;import Android.support.v4.content.contextcompat;import Android.support.v7.widget.linearlayoutmanager;import Android.support.v7.widget.recyclerview;import    Android.view.view;public class Recycleviewdivider extends Recyclerview.itemdecoration {private Paint mpaint;    Private drawable Mdivider; private int mdividerheight = 2;//Split line height, default is 1px private int morientation;// The direction of the list: Linearlayoutmanager.vertical or linearlayoutmanager.horizontal private static final int[] Attrs = new Int[]{android .    R.attr.listdivider}; /** * Default Split line: Height is 2px, color is gray * * @param context * @param orientation List Direction */Public Recycleviewdivider ( context context, int orientation) {if (Orientation! = linearlayoutmanager.vertical && OrientatIon! = Linearlayoutmanager.horizontal) {throw new IllegalArgumentException ("Please enter the correct parameters!        ");        } morientation = orientation;        Final TypedArray a = Context.obtainstyledattributes (attrs);        Mdivider = a.getdrawable (0);    A.recycle (); }/** * Custom Split Line * * @param context * @param orientation list direction * @param Drawableid Split Line picture */PU        Blic Recycleviewdivider (context context, int orientation, int drawableid) {This (context, orientation);        Mdivider = contextcompat.getdrawable (context, Drawableid);    Mdividerheight = Mdivider.getintrinsicheight (); }/** * Custom Split Line * * @param context * @param orientation list direction * @param dividerheight Split Line Height * @p Aram Dividercolor Split Line Color */public Recycleviewdivider (context context, int orientation, int dividerheight, int divi        Dercolor) {This (context, orientation);        Mdividerheight = Dividerheight; Mpaint = new Paint (paint.anti_alias_flag);        Mpaint.setcolor (Dividercolor);    Mpaint.setstyle (Paint.Style.FILL);  }//Get Split line size @Override public void getitemoffsets (Rect outrect, view view, Recyclerview parent, Recyclerview.state        State) {Super.getitemoffsets (Outrect, view, parent, state);    Outrect.set (0, 0, 0, mdividerheight); }//Draw split line @Override public void OnDraw (Canvas C, Recyclerview parent, recyclerview.state state) {Super.on        Draw (c, parent, state);        if (morientation = = linearlayoutmanager.vertical) {drawvertical (c, parent);        } else {Drawhorizontal (c, parent); }}//Draw landscape item split line private void drawhorizontal (canvas canvas, Recyclerview parent) {Final int left = par        Ent.getpaddingleft ();        Final int right = Parent.getmeasuredwidth ()-parent.getpaddingright ();        Final int childsize = Parent.getchildcount (); for (int i = 0; i < childsize; i++) {final View child = Parent.getchilDAt (i);            Recyclerview.layoutparams layoutparams = (recyclerview.layoutparams) child.getlayoutparams ();            Final int top = Child.getbottom () + Layoutparams.bottommargin;            final int bottom = top + mdividerheight;                if (Mdivider! = null) {Mdivider.setbounds (left, top, right, bottom);            Mdivider.draw (canvas);            } if (Mpaint! = null) {Canvas.drawrect (left, top, right, bottom, mpaint);  }}}//Draw portrait item split line private void drawvertical (canvas canvas, Recyclerview parent) {Final int top        = Parent.getpaddingtop ();        final int bottom = Parent.getmeasuredheight ()-Parent.getpaddingbottom ();        Final int childsize = Parent.getchildcount ();            for (int i = 0; i < childsize; i++) {final View child = Parent.getchildat (i);            Recyclerview.layoutparams layoutparams = (recyclerview.layoutparams) child.getlayoutparams (); Final int left = Child.getright () + Layoutparams.rightmargin;            Final int right = left + mdividerheight;                if (Mdivider! = null) {Mdivider.setbounds (left, top, right, bottom);            Mdivider.draw (canvas);            } if (Mpaint! = null) {Canvas.drawrect (left, top, right, bottom, mpaint); }        }    }}

If you use it in activity, you should use it

   Rvxunjiadanchakanbaojia.additemdecoration (New Recycleviewdivider (Getbasecontext (),                Linearlayoutmanager.vertical,20,getresources (). GetColor (R.color.gray));


The ListView split line looks like this

There is a split line between each item item in the ListView, and setting android:footerdividersenabled indicates whether the split line is displayed, which defaults to true.

1. Do not display the split line as long as you add android:footerdividersenabled= "false" to the ListView control.

[HTML]View PlainCopy
  1. < listview &NBSP;&NBSP;
  2.     android:id = " @+id/local_groups_list " &NBSP;&NBSP;
  3.     android:layout_width = " match_parent " &NBSP;&NBSP;
  4.     android:layout_height = " wrap_content " &NBSP;&NBSP;
  5. android:footerdividersenabled="false" />

2. Change the split line color and width of the ListView, you need to define the Android:divider and android:dividerheight properties in the layout.

[HTML]View PlainCopy
  1. < listview &NBSP;&NBSP;
  2.     android:id = " @+id/local_groups_list " &NBSP;&NBSP;
  3.     android:layout_width = " match_parent " &NBSP;&NBSP;
  4.     android:layout_height = " wrap_content " &NBSP;&NBSP;
  5.     android:divider = " @color/divider_color " &NBSP;&NBSP;
  6. android:dividerheight="1px" />

ListView Split Line, Recycleview split Line

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.