Creating Apps With Material Design —— Creating Lists and Cards

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


建立Lisst和Cards


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


建立RecyclerView 


該RecyclerView widget是ListView中的更先進,更靈活的版本號。這個小工具是一個容器。用於顯示。能頗有效地維護了意見數量有限,滾動大的數據集。

當你有收集數據。它的元素在執行時改變基於用戶行爲和網絡事件使用RecyclerView部件。 


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

    定位項目佈局管理器 
    默認的動畫爲公用項的操做。好比刪除或添加的項目 

android

您還可以在RecyclerView部件定義本身定義佈局管理器和動畫的靈活性。數組


要使用RecyclerView小部件。你必須指定一個適配器和一個佈局管理器。要建立一個適配器。擴展RecyclerView.Adapter類。實施的細節取決於你的數據集的詳細狀況和意見的類型。欲瞭解不少其它信息。請參見如下的樣例。 

RecyclerView內部的佈局管理器的位置的項目的意見,並肯定什麼時候重用項目的見解再也不對用戶可見。重用(或回收)的圖,佈局管理器可能會問適配器與數據集不一樣的元素替換視圖的內容。以這樣的方式回收的觀點提升經過避免產生沒必要要的視圖或執行昂貴findViewById()的查找性能。 

RecyclerView提供這些內置的佈局管理器: 

    LinearLayoutManager顯示在垂直或水平滾動列表項。網絡

 
    GridLayoutManager顯示在網格中的項目。 
    StaggeredGridLayoutManager顯示了交錯網格項目。eclipse

 

要建立本身定義佈局管理器。擴展RecyclerView.LayoutManager類。 ide


動畫 


動畫的加入和刪除項目中默認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>
相關文章
相關標籤/搜索