今年6月的Google I/O大会上。Android L的初次见面我相信让会让非常多android粉丝有些小激动和小期待。当然作为开发人员的我来说,激动不言而喻,毕竟这是自08年以来改变最大的一个版本号。
新的设计语言(Material Design),5000多个新增api。废话不多说,今天要说的基本都是在Android5.0中非经常见。也算是对自己学习的一种记录。
1.CardView
顾名思义,CardView 卡片视图。继承自framelayout。能够通过设置圆角以及阴影来展示带有像卡片一样的效果
使用方法:在布局声明CardView(注意:须要引入android-support-v7-cardview.jar)
<span style="font-size:14px;"><android.support.v7.widget.CardView
android:id="@+id/cardview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:elevation="100dp"
card_view:cardBackgroundColor="@color/cardview_initial_background"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="@dimen/margin_large"
android:layout_marginRight="@dimen/margin_large">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_medium"
android:text="@string/cardview_contents"/>
</android.support.v7.widget.CardView></span>
在Activity中通过调用mCardView.setRadius(progress)与mCardView.setElevation(progress)方法来分别设置CardView的圆角和阴影。
2.RecyclerView
RecyclerView的出现能够替代ListView和GridView。它标准化了ViewHolder。之前我们在写ListView的Adapter时需要自己定义ViewHolder来提升ListView速度。
使用RecyclerView的时候必需要设置LayoutManager,它会通知系统以什么样的布局来展示RecyclerView,眼下系统提供了2种LayoutManager,LinearLayoutManager和GridLayoutManager相应着线性和格子,当然了也能够自己定义LayoutManager来满足各种需求
使用方法:布局文件里声明RecyclerView(注意:须要引入android-support-v7-recyclerview.jar)
<span style="font-size:14px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:checkedButton="@+id/linear_layout_rb">
<RadioButton android:id="@+id/linear_layout_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/linear_layout_manager"/>
<RadioButton android:id="@+id/grid_layout_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/grid_layout_manager"/>
</RadioGroup> <android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout></span>
在Activity中调用例如以下代码
<span style="font-size:14px;">mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
//相似listview
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
//相似grdiview
mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
mAdapter = new CustomAdapter(mDataset);
// Set CustomAdapter as the adapter for RecyclerView.
mRecyclerView.setAdapter(mAdapter);</span>
CustomAdapter代码例如以下
<span style="font-size:14px;">public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private static final String TAG = "CustomAdapter"; private String[] mDataSet; /**
* Provide a reference to the type of views that you are using (custom ViewHolder)
*/
public static class ViewHolder extends RecyclerView.ViewHolder {
private final TextView textView; public ViewHolder(View v) {
super(v);
// Define click listener for the ViewHolder's View.
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
textView = (TextView) v.findViewById(R.id.textView);
} public TextView getTextView() {
return textView;
}
} /**
* Initialize the dataset of the Adapter.
*
* @param dataSet String[] containing the data to populate views to be used by RecyclerView.
*/
public CustomAdapter(String[] dataSet) {
mDataSet = dataSet;
} // Create new views (invoked by the layout manager)
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
// Create a new view.
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.text_row_item, viewGroup, false); return new ViewHolder(v);
} // Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
// Get element from your dataset at this position and replace the contents of the view
// with that element
viewHolder.getTextView().setText(mDataSet[position]);
viewHolder.getTextView().setPadding(0, position, 0, 0);
} // Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataSet.length;
}
}</span>
今天先介绍到这里。兴许还会介绍FloatActionBar,触摸反馈,Activity过场动画等。
转载请标注出处,多谢~