Android L中的RecyclerView 、CardView 、Palette的使用

來源:互聯網
上載者:User

標籤:des   android   blog   http   io   ar   color   使用   sp   

《Material Design》提到,Android L版本中新增了RecyclerView、CardView 、Palette。RecyclerView、CardView為用於顯示複雜視圖的新增Widget。Palette作為調色盤類,可以讓你從映像中提取突出的顏色。

 

 

RecyclerView

 

    RecyclerView作為替代ListView使用,RecyclerView標準化了ViewHolder,ListView中convertView是複用的,在RecyclerView中,是把ViewHolder作為緩衝的單位了,然後convertView作為ViewHolder的成員變數保持在ViewHolder中,也就是說,假設沒有螢幕顯示10個條目,則會建立10個ViewHolder緩衝起來,每次複用的是ViewHolder,所以他把getView這個方法變為了onCreateViewHolder。 ViewHolder更適合多種子布局的列表,尤其IM的對話列表。RecyclerView不提供setOnItemClickListener方法,你可以在ViewHolder中添加事件。RecyclerView的使用可以參考《Material Design UI Widgets》。


RecyclerView可以實現橫向、縱向滑動視圖:

 

                 

RecyclerView 1                                    RecyclerView 2

 

設定橫向:

 

[java] view plaincopyprint? 
  1. @Override  
  2.    protected void onCreate(Bundle savedInstanceState) {  
  3.        super.onCreate(savedInstanceState);  
  4.        setContentView(R.layout.activity_recycler_view_horizontal);  
  5.   
  6.        // specify an adapter (see also next example)  
  7.        List<MyAdapter.Item> itemList = new ArrayList<MyAdapter.Item>();  
  8.        for (int i = 0; i < 100; i++)  
  9.            itemList.add(new MyAdapter.Item("Item " + i, "world"));  
  10.        mAdapter = new MyAdapter(itemList);  
  11.   
  12.   
  13.        mRecyclerViewHorizontal = (RecyclerView) findViewById(R.id.my_recycler_view_horizontal);  
  14.        mRecyclerViewHorizontal.setHasFixedSize(true);  
  15.   
  16.        // use a linear layout manager  
  17.        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);  
  18.        mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);  
  19.        mRecyclerViewHorizontal.setLayoutManager(mLayoutManager);  
  20.        mRecyclerViewHorizontal.setAdapter(mAdapter);  
  21.   
  22.   
  23.    }  



 

CardView 

    CardView繼承自FrameLayout類,可以在一個卡片布局中一致性的顯示內容,卡片可以包含圓角和陰影。CardView是一個Layout,可以布局其他View。CardView 的使用可以參考《Material Design UI Widgets》。文章最後會給出這篇文章範例程式碼。

 

                    

CardView                                         Palette

 

Palette

 

    Palette從映像中提取突出的顏色,這樣可以把色值賦給ActionBar、或者其他,可以讓介面整個色調統一,效果見(Palette)。

 

Palette這個類中提取以下突出的顏色:

Vibrant  (有活力)

Vibrant dark(有活力 暗色)

Vibrant light(有活力 亮色)

Muted  (柔和)

Muted dark(柔和 暗色)

Muted light(柔和 亮色)

 

提取色值代碼如下:

 

 

[java] view plaincopyprint? 
    1. Bitmap bm = BitmapFactory.decodeResource(getResources(), item.image);  
    2.           Palette palette = Palette.generate(bm);  
    3.           if (palette.getLightVibrantColor() != null) {  
    4.               name.setBackgroundColor(palette.getLightVibrantColor().getRgb());  
    5.               getSupportActionBar().setBackgroundDrawable(new ColorDrawable(palette.getLightVibrantColor().getRgb()));  
    6.               // getSupportActionBar().  
    7.   
    8.           }  

Android L中的RecyclerView 、CardView 、Palette的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.