Android L 開發者預覽支持庫提供兩個新的Widgets,RecyclerView和CardView。使用這兩個Widgets能夠顯示覆雜的Listview和卡片佈局,這兩個Widgets默認使用Material design。
html
RecyclerView是一個更高級柔性版本的Listview,RecyclerView是一個能包含不少視圖的容器,它能完美的處理循環和滾動。在item動態變化的Listview使用RecyclerView。java
RecyclerView使用很簡單,由於它提供了:
android
一、定位item的佈局管理器ide
二、常見的item操做默認動畫佈局
你可以靈活的爲RecyclerView自定義佈局管理器和動畫。動畫
使用RecyclerView,必須使用指定一個adapter、定義一個佈局管理器。建立adapter必須繼承自RecyclerView.Adapter。實施的細節須要看數據類型和須要的視圖。ui
RecyclerView widgetthis
RecyclerView 提供了 LayoutManager,RecylerView 不負責子 View 的佈局,咱們能夠自定義 LayoutManager 來實現不一樣的佈局效果,目前只提供了LinearLayoutManager。 LinearLayoutManager 能夠指定方向,默認是垂直, 能夠指定水平, 這樣就輕鬆實現了水平的 ListView。
spa
RecyclerView Demo:
.net
一、佈局文件
<!-- 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); // improve performance if you know that changes in content // do not change the 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); } ... } To create a simple adapter: public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private String[] mDataset; // Provide a reference to the type of views that you are using // (custom viewholder) public static class ViewHolder extends RecyclerView.ViewHolder { 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, null); // 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; } }
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private String[] mDataset; // Provide a reference to the type of views that you are using // (custom viewholder) public static class ViewHolder extends RecyclerView.ViewHolder { 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, null); // 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類,能夠在一個卡片佈局中一致性的顯示內容,卡片能夠包含圓角和陰影。
能夠使用android:elevation屬性,建立一個陰影的卡片。
怎樣指定CardView的屬性:
一、使用android:cardCornerRadius屬性指定圓角半徑
二、使用CardView.setRadius 設置圓角半徑。
三、使用 android:cardBackgroundColor屬性設置卡片顏色
在建立佈局文件中建立CardView:
<!-- 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>
參考:
http://developer.android.com/preview/material/ui-widgets.html