Creating Apps With Material Design —— Creating Lists and Cards

來源:互聯網
上載者:User

標籤:l   android開發   material design   

轉載請註明 http://blog.csdn.net/eclipsexys 翻譯自Developer Android,時間倉促,有翻譯問題請留言指出,謝謝


建立Lisst和Cards

在你的應用程式建立複雜的清單,並與材料設計風格卡,您可以使用RecyclerView和CardView組件。 


建立RecyclerView 
該RecyclerView widget是ListView中的更先進,更靈活的版本。這個小工具是一個容器,用於顯示,能非常有效地維護了意見數量有限,滾動大的資料集。當你有收集資料,它的元素在運行時改變基於使用者行為和網路事件使用RecyclerView組件。 


該RecyclerView類簡化,提供顯示和處理大資料集: 

    定位項目布局管理器 
    預設的動畫為公用項的操作,例如刪除或增加的項目 

您還可以在RecyclerView組件定義自訂布局管理器和動畫的靈活性。


要使用RecyclerView小組件,你必須指定一個適配器和一個布局管理器。要建立一個適配器,擴充RecyclerView.Adapter類。實施的細節取決於你的資料集的具體情況和意見的類型。欲瞭解更多資訊,請參見下面的例子。 

RecyclerView內部的布局管理器的位置的項目的意見,並確定何時重用項目的看法不再對使用者可見。重用(或回收)的圖,布局管理器可能會問適配器與資料集不同的元素替換視圖的內容。以這種方式回收的觀點提高通過避免產生不必要的視圖或執行昂貴findViewById()的尋找效能。 

RecyclerView提供這些內建的布局管理器: 

    LinearLayoutManager顯示在垂直或水平滾動清單項目。 
    GridLayoutManager顯示在網格中的項目。 
    StaggeredGridLayoutManager顯示了交錯網格項目。 

要建立自訂布局管理器,擴充RecyclerView.LayoutManager類。 


動畫 
動畫的添加和刪除項目中預設RecyclerView啟用。要自訂這些動畫,繼承RecyclerView.ItemAnimator類,並使用RecyclerView.setItemAnimator()方法。




Examples


下面的程式碼範例示範如何將RecyclerView添加到布局:

<!-- A RecyclerView with some commonly used attributes --><android.support.v7.widget.RecyclerView    android:id="@+id/my_recycler_view"    android:scrollbars="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"/>

一旦你添加了一個RecyclerView小組件布局,擷取控制代碼的對象,將其串連到一個布局管理器,並附上要顯示的資料配接器:

public class MyActivity extends Activity {    private RecyclerView mRecyclerView;    private RecyclerView.Adapter mAdapter;    private RecyclerView.LayoutManager mLayoutManager;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.my_activity);        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);        // use this setting to improve performance if you know that changes        // in content do not change the layout size of the RecyclerView        mRecyclerView.setHasFixedSize(true);        // use a linear layout manager        mLayoutManager = new LinearLayoutManager(this);        mRecyclerView.setLayoutManager(mLayoutManager);        // specify an adapter (see also next example)        mAdapter = new MyAdapter(myDataset);        mRecyclerView.setAdapter(mAdapter);    }    ...}

該適配器提供訪問資訊在您的資料集,建立視圖的項目,取代了一些新的資料項目的視圖的內容時,原來的產品不再可見。下面的程式碼範例顯示了一個簡單的實現,它由一個字串數組的使用TextView的小組件顯示的一組資料:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {    private String[] mDataset;    // Provide a reference to the views for each data item    // Complex data items may need more than one view per item, and    // you provide access to all the views for a data item in a view holder    public static class ViewHolder extends RecyclerView.ViewHolder {        // each data item is just a string in this case        public TextView mTextView;        public ViewHolder(TextView v) {            super(v);            mTextView = v;        }    }    // Provide a suitable constructor (depends on the kind of dataset)    public MyAdapter(String[] myDataset) {        mDataset = myDataset;    }    // Create new views (invoked by the layout manager)    @Override    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,                                                   int viewType) {        // create a new view        View v = LayoutInflater.from(parent.getContext())                               .inflate(R.layout.my_text_view, parent, false);        // set the view‘s size, margins, paddings and layout parameters        ...        ViewHolder vh = new ViewHolder(v);        return vh;    }    // Replace the contents of a view (invoked by the layout manager)    @Override    public void onBindViewHolder(ViewHolder holder, int position) {        // - get element from your dataset at this position        // - replace the contents of the view with that element        holder.mTextView.setText(mDataset[position]);    }    // Return the size of your dataset (invoked by the layout manager)    @Override    public int getItemCount() {        return mDataset.length;    }}

建立CardView

CardView擴充的FrameLayout類,並允許你顯示裡面有跨平台一致的外觀卡的資訊。 CardView組件可以有陰影和圓角。 

要建立具有陰影卡,使用card_view:cardElevation屬性。 CardView使用真實高程和動態陰影在Android5.0(API等級21)以上,並回落到較早版本的綱領性陰影實施。欲瞭解更多資訊,請參見維護相容性。 

使用這些屬性來定製CardView小組件的外觀: 

    要設定圓角半徑在你的布局,使用card_view:cardCornerRadius屬性。 
    要設定圓角半徑在你的代碼中,使用CardView.setRadius方法。 
    設定卡的背景顏色,使用card_view:cardBackgroundColor屬性。 

下面的程式碼範例顯示了如何在您的布局CardView組件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:card_view="http://schemas.android.com/apk/res-auto"    ... >    <!-- A CardView that contains a TextView -->    <android.support.v7.widget.CardView        xmlns:card_view="http://schemas.android.com/apk/res-auto"        android:id="@+id/card_view"        android:layout_gravity="center"        android:layout_width="200dp"        android:layout_height="200dp"        card_view:cardCornerRadius="4dp">        <TextView            android:id="@+id/info_text"            android:layout_width="match_parent"            android:layout_height="match_parent" />    </android.support.v7.widget.CardView></LinearLayout>


Creating Apps With Material Design —— Creating Lists and Cards

聯繫我們

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