轉載請註明 http://blog.csdn.net/eclipsexys 翻譯自Developer Android。時間倉促,有翻譯問題請留言指出,謝謝
html
在你的應用程序建立複雜的清單,並與材料設計風格卡。您可以使用RecyclerView和CardView部件。 java
當你有收集數據。它的元素在執行時改變基於用戶行爲和網絡事件使用RecyclerView部件。
該RecyclerView類簡化,提供顯示和處理大數據集:
定位項目佈局管理器
默認的動畫爲公用項的操做。好比刪除或添加的項目
android
您還可以在RecyclerView部件定義本身定義佈局管理器和動畫的靈活性。數組
要使用RecyclerView小部件。你必須指定一個適配器和一個佈局管理器。要建立一個適配器。擴展RecyclerView.Adapter類。實施的細節取決於你的數據集的詳細狀況和意見的類型。欲瞭解不少其它信息。請參見如下的樣例。
RecyclerView內部的佈局管理器的位置的項目的意見,並肯定什麼時候重用項目的見解再也不對用戶可見。重用(或回收)的圖,佈局管理器可能會問適配器與數據集不一樣的元素替換視圖的內容。以這樣的方式回收的觀點提升經過避免產生沒必要要的視圖或執行昂貴findViewById()的查找性能。
RecyclerView提供這些內置的佈局管理器:
LinearLayoutManager顯示在垂直或水平滾動列表項。網絡
GridLayoutManager顯示在網格中的項目。
StaggeredGridLayoutManager顯示了交錯網格項目。eclipse
要建立本身定義佈局管理器。擴展RecyclerView.LayoutManager類。 ide
如下的代碼演示樣例演示怎樣將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"/>
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); } ... }
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擴展的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>