How to use Recyclerview and how to set up tap listening

Source: Internet
Author: User

Recyclerview is derived from the ListView, which is more efficient than the ListView and is more convenient to use.

This article explains how to do this with Recyclerview: vertical list effects, landscape list effects, grid list effects , waterfall flows (Landscape and portrait), and Item's Click to listen (because Recyclerview does not have the item's click to listen function, here is my self-created, the feeling is still useful). That's a powerful effect, isn't it? Haha, see the source code below

(1) Main layout

<?XML version="1.0"encoding="Utf-8"?>
<relativelayoutxmlns:Android="Http://schemas.android.com/apk/res/android"
Android: Layout_width="Match_parent"
Android: layout_height="Match_parent">

<android.support.v7.widget.recyclerview
Android: id="@+id/recyclerview"
Android: Layout_width="Match_parent"
Android: layout_height="Match_parent"/>

</RelativeLayout>

(2) Item

<?XML version="1.0"encoding="Utf-8"?>
<linearlayoutxmlns:Android="Http://schemas.android.com/apk/res/android"
Android: Layout_width="Match_parent"
Android: layout_height="Match_parent"
Android: orientation="Vertical">

<textview
Android: id="@+id/tv"
Android: textsize="60DP"
Android:p adding="20DP"
Android: Layout_width="Wrap_content"
Android: layout_height="Wrap_content"/>

</LinearLayout>

(3) Mainactivity code

Mainactivity appcompatactivity {Recyclerview string[] = {} (Bundle savedinstancestate) {. OnCreate (Savedinstanc eState) Setcontentview (r.layout.) = (Recyclerview) Findviewbyid (r.id.). Sethasfixedsize () Staggeredgridlayoutmanager Staggeredgridlayoutmanager =staggeredgridlayoutmanager ( Staggeredgridlayoutmanager.). Setlayoutmanager (Staggeredgridlayoutmanager). Setadapter (Myadapter ())

(4) Adapter Adapter

myadapter recyclerview.adapter<myadapter.viewholder>{    string[]  Myadapter (String[] mdatas) {        =mdatas}     ViewHolder  (Viewgroup parentviewtype)  {         View view=layoutinflater. (Parent.getcontext ()). Inflate (r.layout.parent) Viewholder viewholder=viewholder (Viewparent.getContext ()) viewholder}     (viewholder holderposition)  {         holder. SetText ([position])}     ()  {        .}     ViewHolder RecyclerView.ViewHolder View.OnClickListener {         textview context viewholder (view itemviewcontext  Context)  {             (Itemview) =&NBSP; (TextView)  itemview.findviewbyid (r.id.). =contextitemview.setonclicklistener ()}         (View v)  {             toast. (. GetText (). toString () Toast.). Show ()}    }}

Operating effect:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/76/5E/wKioL1ZRpXTCMveKAACk36iRDlM932.jpg "title=" Qq20151122192043.jpg "alt=" Wkiol1zrpxtcmvekaack36irdlm932.jpg "/>

This article is from the "Mobile Platform Development" blog, make sure to keep this source http://liuxudong1001.blog.51cto.com/10877072/1715723

How to use Recyclerview and how to set up tap listening

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.