Android自學曆程—RecyclerView的使用(2),androidrecycler

來源:互聯網
上載者:User

Android自學曆程—RecyclerView的使用(2),androidrecycler
Introduction to RecyclerView

RecyclerView在Android 5中被介紹,在 Support-V7的包中。她允許展示items在隨意任何之處(可聯想ListView),正如包名所說的,在API7以上均可使用(Android 22).

 

她的名字來自於其工作的方式,當一個Item被隱藏時,不是去destroyed她並且隨後為每一個新new出來的對象去建立一個新的item,隱藏的item被回收:她們被重用,並且會有新的資料繫結她們。

 

一個RccyclerView被分為6個主要的組件:

an Adpter,提供資料(類似Listview的)

an ItemAnimator, 負責items的修改,增加,刪除,移動的動畫效果

an ItemDecoration, Which can add drawings or change the layout of an item

an Layoutmanage,指定Items的布局

an ViewHolder, 每一個Items View的基類

the RecyclerView本身, 把所有的綁定

 

在support-V7包中,一些組件已經綁定了預設的實現方式。你有一個ItemAnimator和三個Layoutmanage可以玩玩。RecyclerView不需要修改,並且ItemDecoration是可選的,留給我們的還有Adpter和ViewHolder。

 

Display a RecyclerView1.Prepare your project  add to your dependenceies:

     RecyclerView: compile 'com.android.support:recyclerview-v7:22.2.1'

     CardView:    compile 'com.android.support:cardview-v7:22.2.1'

 

2.The bass item我們寫一個簡單的list,每一個items裡包含一個 title 和 一個 subtitle。
 1 public class Item{ 2     private String title; 3     private String subtitle; 4      5     public Item(String title,String subtitle){ 6             this.title = title; 7             this.subtitle = subtitle; 8     } 9     10     public String getTitle(){11             return title;12     }13     14     public String getSubtitle(){15             return subtitle;16     }17 }
3.Item layout我們的items用CardView來展示。一個 CardView就是修飾過的 FrameLayout,因此有兩個TextView的展示是非常簡單的。
 1 <?xml version="1.0" encoding="utf-8"?> 2 <android.support.v7.widget.CardView 3     xmlns:android="http://schemas.android.com/apk/res/android" 4     xmlns:app="http://schemas.android.com/apk/res-auto" 5     android:layout_width="match_parent" 6     android:layout_height="match_parent" 7     app:contentPadding="8dp" 8     app:cardUseCompatPadding="true"> 9 10     <LinearLayout11         android:layout_width="match_parent"12         android:layout_height="match_parent"13         android:orientation="vertical">14 15         <TextView16             android:id="@+id/title"17             android:layout_width="match_parent"18             android:layout_height="wrap_content"19             android:singleLine="true"20             style="@style/Base.TextAppearance.AppCompat.Headline"/>21 22         <TextView23             android:id="@+id/subtitle"24             android:layout_width="match_parent"25             android:layout_height="0dp"26             android:layout_weight="1"27             style="@style/Base.TextAppearance.AppCompat.Subhead"/>28 29     </LinearLayout>30 31 </android.support.v7.widget.CardView>

 

4.The adapter第一步是定義我們自己的 ViewHolder 類。她必須繼承 RecycleView.ViewHolder, 並且應該儲存,當綁定你的資料到holder上,你所需要的用到的View。(翻譯的太差了)(原句:and should store references to the Views you will need when binding your data on the holder .)這裡我們有2個textview。
 1 public class MyAdapter extents RecyclerView.Adaper<>{ 2     private static final String TAG = MyAdapter.class.getSImpleName(); 3      4     public static class MyViewHolder extents RecyclerView.Viewholder{ 5             TextView title; 6             TextView subtitle; 7          8             public MyViewHolder(View itemView){ 9                 super(itemView);10             11                 title = (TextView)itemView.findViewById(R.id.title);12                 subtitle = (TextView) itemView.findViewById(R.id.subtitle);13             }14     }15 }
現在,什麼是儲存物件的集合的最簡單的方法? 對,就是 Collect。 在這個例子裡,寫簡單的方法,我們儲存我們的items(對象的集合)在ArrayList,在MyAdaper.java類裡。
private List<Item> items;    private static final int ITEM_COUNT = 50;    public Myadapet() {        Random random = new Random();        items = new ArrayList<>();        for (int i = 0; i < ITEM_COUNT; i++) {            items.add(new Item("Item:" + i, "this is the item number " + i, random.nextBoolean()));        }    }

 

之後我們要實現真正的 RecyclerView.Adaper的方法:
  • onCreateViewHolder(ViewGroup viewGroup,int viewType)應該創造View,並且返回一個匹配的 ViewHolder,
  • OnBindViewHolder(ViewHolder holder,int position)應該利用 根據position擷取item裡的資料來填充ViewHolder,
  • getItemCount()應該給items的數量

 

在我們的栗子中,實現方式 還是比較簡單的
 1 @Override 2     public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 3  4         View v = LayoutInflater.from(viewGroup.getContext()).inflate(layout, viewGroup, false); 5         return new ViewHolder(v); 6     } 7  8 @Override 9     public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {10         final Item item = items.get(i);11         ViewHolder myViewHolder = (ViewHolder) viewHolder;12         myViewHolder.title.setText(item.getTitle());13         myViewHolder.subtitle.setText(item.getSubtitle())14     }15 16     @Override17     public int getItemCount() {18         return items.size();19     }

 

5.Bind everything together 我們已經定義了我們所需要的東西。見證奇蹟的時刻。第一步:添加一個 RecyclerView到 Activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">    <android.support.v7.widget.RecyclerView        android:id="@+id/recyclerView"        android:layout_width="match_parent"        android:layout_height="match_parent"/></RelativeLayout>

 

我們使用簡單的 LinearlayoutManger.我們也會使用簡單的 DefaultItemAnimator.

 1 package com.ryan.recycleviewdemo02; 2  3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.support.v7.widget.DefaultItemAnimator; 6 import android.support.v7.widget.GridLayoutManager; 7 import android.support.v7.widget.LinearLayoutManager; 8 import android.support.v7.widget.RecyclerView; 9 import android.view.Menu;10 import android.view.MenuItem;11 12 public class MainActivity extends AppCompatActivity {13 14     private static String TAG = MainActivity.class.getSimpleName();15 16     private RecyclerView recyclerView;17 //    private RecyclerView.LayoutManager layoutManager;18 //    private RecyclerView.Adapter adapter;19 20     @Override21     protected void onCreate(Bundle savedInstanceState) {22         super.onCreate(savedInstanceState);23         setContentView(R.layout.activity_main);24 25         recyclerView = (RecyclerView) findViewById(R.id.recyclerView);26         recyclerView.setAdapter(new Myadapet());27         recyclerView.setItemAnimator(new DefaultItemAnimator());28         recyclerView.setLayoutManager(new LinearlayoutManger(this));29 30     }31 32 }

 

上方顏色效果,後續再說。

 

到這裡 就是基本的 RecyclerView的展示。後續…………

 

翻譯加個人理解: www.enoent.fr/blog/2015/01/18/recyclerview-basics/,

謝謝。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.