更新时间:2021-12-04 02:02:14
转载请注明 http://blog.csdn.net/eclipsexys 翻译自Developer Android,时间仓促,有翻译问题请留言指出,谢谢
在你的应用程序创建复杂的清单,并与材料设计风格卡,您可以使用RecyclerView和CardView部件。
您还可以在RecyclerView部件定义自定义布局管理器和动画的灵活性。
要使用RecyclerView小部件,你必须指定一个适配器和一个布局管理器。要创建一个适配器,扩展RecyclerView.Adapter类。实施的细节取决于你的数据集的具体情况和意见的类型。欲了解更多信息,请参见下面的例子。
RecyclerView内部的布局管理器的位置的项目的意见,并确定何时重用项目的看法不再对用户可见。重用(或回收)的图,布局管理器可能会问适配器与数据集不同的元素替换视图的内容。以这种方式回收的观点提高通过避免产生不必要的视图或执行昂贵findViewById()的查找性能。
RecyclerView提供这些内置的布局管理器:
LinearLayoutManager显示在垂直或水平滚动列表项。
GridLayoutManager显示在网格中的项目。
StaggeredGridLayoutManager显示了交错网格项目。
要创建自定义布局管理器,扩展RecyclerView.LayoutManager类。
下面的代码示例演示如何将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>
Creating Apps With Material Design —— Using the Material Theme
Creating Apps With Material Design —— Working with Drawables
Creating Apps With Material Design —— Maintaining Compatibility
Creating Apps With Material Design —— Creating Lists and Cards
Creating Apps With Material Design —— Defining Shadows and Clipping Views
Creating Apps With Material Design —— Defining Custom Animations