Android L中的RecyclerView 、CardView 、Palette的使用

t889in39 9年前

Android L版本中新增了RecyclerView、CardView 、Palette。RecyclerView、CardView为用于显示复杂视图的新增Widget。Palette作为调色板类,可以让你从图像中提取突出的颜色。

 

RecyclerView

   RecyclerView作为替代ListView使用,RecyclerView标准化了ViewHolder,ListView中convertView是复用的,在RecyclerView中,是把ViewHolder作为缓存的单位了,然后convertView作为ViewHolder的成员变量保持在ViewHolder中,也就是说,假设没有屏幕显示10个条目,则会创建10个ViewHolder缓存起来,每次复用的是ViewHolder,所以他把getView这个方法变为了onCreateViewHolder。 ViewHolder更适合多种子布局的列表,尤其IM的对话列表。RecyclerView不提供setOnItemClickListener方法,你可以在ViewHolder中添加事件。


RecyclerView可以实现横向、纵向滑动视图:

使用RecyclerView,必须使用指定一个adapter、定义一个布局管理器。创建adapter必须继承自RecyclerView.Adapter。实施的细节需要看数据类型和需要的视图。

  RecyclerView widget



RecyclerView 提供了 LayoutManager,RecylerView 不负责子 View 的布局,我们可以自定义 LayoutManager 来实现不同的布局效果,目前只提供了LinearLayoutManager。 LinearLayoutManager 可以指定方向,默认是垂直, 可以设置成水平。

 

RecyclerView Demo:

1、布局文件

<!-- 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"/>

2、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);          // 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;      }  }

3、Recycler 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;      }  }

设置横向:

@Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_recycler_view_horizontal);         // specify an adapter (see also next example)         List<MyAdapter.Item> itemList = new ArrayList<MyAdapter.Item>();         for (int i = 0; i < 100; i++)             itemList.add(new MyAdapter.Item("Item " + i, "world"));         mAdapter = new MyAdapter(itemList);         mRecyclerViewHorizontal = (RecyclerView) findViewById(R.id.my_recycler_view_horizontal);         mRecyclerViewHorizontal.setHasFixedSize(true);         // use a linear layout manager         LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);         mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);         mRecyclerViewHorizontal.setLayoutManager(mLayoutManager);         mRecyclerViewHorizontal.setAdapter(mAdapter);     }

CardView

 

CardView继承自FrameLayout类,可以在一个卡片布局中一致性的显示内容,卡片可以包含圆角和阴影。

可以使用android:elevation属性,创建一个阴影的卡片。

 

怎样指定CardView的属性:

1、使用android:cardCornerRadius属性指定圆角半径

2、使用CardView.setRadius 设置圆角半径。

3、使用 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>

 

Palette

Palette从图像中提取突出的颜色,这样可以把色值赋给ActionBar、或者其他,可以让界面整个色调统一,效果见上图(Palette)。

Palette这个类中提取以下突出的颜色:

Vibrant  (有活力)

Vibrant dark(有活力 暗色)

Vibrant light(有活力 亮色)

Muted  (柔和)

Muted dark(柔和 暗色)

Muted light(柔和 亮色)

提取色值代码如下:

Bitmap bm = BitmapFactory.decodeResource(getResources(), item.image);      Palette palette = Palette.generate(bm);      if (palette.getLightVibrantColor() != null) {            name.setBackgroundColor(palette.getLightVibrantColor().getRgb());            getSupportActionBar().setBackgroundDrawable(new ColorDrawable(palette.getLightVibrantColor().getRgb()));      }  }