C#寫日記類實例。本站提示廣大學習愛好者:(C#寫日記類實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#寫日記類實例正文
5.0供給了兩個新的Widget,它們應用了Material Design 的style和animation:
它兩位於 sdk/extras/android/support/v7/cardview 和 sdk/extras/android/support/v7/RecyclerView
創立列表
RecyclerView組件是一個更先輩和靈巧的版本的列表視圖。這個小部件是一個異常有用率的容器,經由過程無限的views,可以轉動顯示年夜型數據集。
RecyclerView組件數據聚集的元素,可在運轉時依據用戶操作或收集事宜停止轉變。
RecyclerView類簡化了顯示和處置年夜型數據集,它供給了:
你可以在RecyclerView中靈巧界說 結構治理器和動畫
要應用RecyclerView組件,您必需指定一個適配器和結構治理器。創立一個適配器,繼續RecyclerView.Adapter類。有關更多信息,請拜見上面的例子。
RecyclerView並肯定重用項目視圖時,結構治理器的應用item的辦法,不再是對用戶可見。重用(或收受接管)視圖,結構治理器能夠會問適配器,調換內容為分歧的數據集的元素。收受接管view時,以這類方法來改良機能:防止創立不用要的view或履行消費年夜的findViewById()查詢。
RecyclerView供給了以下治理器:
要創立一個自界說結構治理器,須要繼續RecyclerView.LayoutManager類
動畫:
添加和刪除item的動畫,在RecyclerView默許啟用。定制這些動畫,須要繼續RecyclerView.ItemAnimator類並應用RecyclerView.setItemAnimator()辦法。
例子:
layout
<!-- 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"/>
activity
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); //應用固定size 以優化機能 // 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); } ... } adapter 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 TextView v = (TextView)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; } // WordStr 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,以卡片式顯示分歧的外不雅。它可以有暗影和圓角
創立一個有暗影的卡片,應用card_view:cardElevation屬性。
應用這些屬性來定制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>
添加依附:
gradle依附
dependencies { ... compile 'com.android.support:cardview-v7:21.0.+' compile 'com.android.support:recyclerview-v7:21.0.+' }