New Android control RecyclerView

Source: Internet
Author: User

New Android control RecyclerView

RecyclerView is a new Widgets in the android-support-v7-21 version. We can use RecyclerView to easily implement horizontal and vertical ListView. GridView and waterfall stream effects. Let's take a look at the project implementation results first.

Is it amazing? RecyclerView can be easily implemented. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Authorization + authorization + vJvrP9tcS2r7ut0Ke5 + zxiciAvPg0KNC7L/authorization/t9a0 + sLro7o8L3A + DQo8cHJlIGNsYXNzPQ = "brush: java;"> MRecyclerView = (RecyclerView) findViewById (R. id. rv); // set its Adapter myRecyclerViewListAdapater = new MyRecyclerViewListAdapater (this, sourceDatas); mRecyclerView. setAdapter (myRecyclerViewListAdapater); // set its LayoutManage, and set its display mode through LayoutManager LinearLayoutManager linearLayoutManager = new LinearLayoutManager (this, LinearLayoutManager. VERTICAL, false); mRecyclerView. setLayoutManager (linearLayoutManager); // sets the split line // dividerItemDecoration = new DividerItemDecoration (this, LinearLayoutManager. VERTICAL); // mRecyclerView. addItemDecoration (dividerItemDecoration); // you can add the mRecyclerView animation to delete an Item. setItemAnimator (new DefaultItemAnimator (); myRecyclerViewListAdapater. setOnItemClickListener (new MyRecyclerViewListAdapater. onItemClickListener () {@ Override public void onItemClickListener (View view, int position) {Toast. makeText (MainActivity. this, click: + position, Toast. LENGTH_SHORT ). show () ;}@ Override public boolean onLongItemClickListener (View view, int position) {myRecyclerViewListAdapater. deleteItem (position); return false ;}});

Adapter code

Public class MyRecyclerViewListAdapater extends RecyclerView. Adapter
  
   
{Private Context context; protected List
   
    
SourceDatas; private LayoutInflater layoutInflater; public OnItemClickListener mOnItemClickListener; public MyRecyclerViewListAdapater (Context context, List
    
     
SourceDatas) {this. context = context; this. sourceDatas = sourceDatas; layoutInflater = LayoutInflater. from (context) ;}@ Override public MyRecyclerViewListAdapater. myListHolder onCreateViewHolder (ViewGroup viewGroup, int I) {View = layoutInflater. inflate (R. layout. item, viewGroup, false); MyListHolder myListHolder = new MyListHolder (view); return myListHolder ;}@ Override public void onBindViewHo Lder (final MyListHolder viewHolder, final int I) {viewHolder. textView. setText (sourceDatas. get (I); itemClick (viewHolder);}/*** ItemView Click Event */protected void itemClick (final MyListHolder viewHolder) {viewHolder. itemView. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {if (null! = MOnItemClickListener) {int postion = viewHolder. getLayoutPosition (); mOnItemClickListener. onItemClickListener (viewHolder. itemView, postion) ;}}); viewHolder. itemView. setOnLongClickListener (new View. onLongClickListener () {@ Override public boolean onLongClick (View v) {if (null! = MOnItemClickListener) {int position = viewHolder. getLayoutPosition (); return mOnItemClickListener. onLongItemClickListener (viewHolder. itemView, position) ;}return false ;}}) ;}/ *** number of items returned by the method **/@ Override public int getItemCount () {return sourceDatas. size ();}/*** add item */public void addItem (int position) {sourceDatas. add (position, Add Item); notifyItemInserted (position);}/*** delete Item */public void deleteItem (int positon) {sourceDatas. remove (positon); notifyItemRemoved (positon);}/*** set the item Click Interface */public void setOnItemClickListener (OnItemClickListener onItemClickListener) {this. mOnItemClickListener = onItemClickListener;}/*** the Recycler itself does not provide ItemClick events to us, here we need to implement this interface by ourselves * Click interface **/public interface OnItemClickListener {// click the event public void onItemClickListener (View view, int position ); // long press event public boolean onLongItemClickListener (View view, int position);} class MyListHolder extends RecyclerView. viewHolder {TextView textView; // inherits RecyclerView. viewHolder must implement its constructor // you can see that a View parameter is received in its constructor. This View is the ItemView public MyListHolder (View itemView) we created) {super (itemView); textView = (TextView) itemView. findViewById (R. id. TV _list_item );}}}
    
   
  

Layout:

 

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.