官网:https://developer.android.com/training/material/lists-cards.html
上一节:使用Material主题
下一节:定义阴影和裁剪View
在你的应用程序,创建复杂的列表和卡片与材料设计风格,您可以使用RecyclerView和CardView部件。
RecyclerView类简化了显示和处理大型数据集,它提供了:
· 布局管理器
· 常见的默认动画item操作,如删除、添加项目
你可以在RecyclerView中灵活定义 布局管理器和动画
要使用RecyclerView组件,您必须指定一个适配器和布局管理器。创建一个适配器,继承RecyclerView.Adapter类。有关更多信息,请参见下面的例子。
RecyclerView并确定重用项目视图时,布局管理器的利用item的方法,不再是对用户可见。重用(或回收)视图,布局管理器可能会问适配器,替换内容为不同的数据集的元素。回收view时,以这种方式来改进性能:避免创建不必要的view或执行消耗大的findViewById()查询。
RecyclerView提供了如下管理器:
· LinearLayoutManager 横向或纵向的滚动列表
· GridLayoutManager 网格列表
· StaggeredGridLayoutManager 交错的网格列表
要创建一个自定义布局管理器,需要继承RecyclerView.LayoutManager类
<!-- 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); //使用固定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); } ... }
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; } }
<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>
dependencies { ... compile 'com.android.support:cardview-v7:21.0.+' compile 'com.android.support:recyclerview-v7:21.0.+' }
Android(Lollipop/5.0) Material Design(四) 创建列表和卡片
原文地址:http://blog.csdn.net/jjwwmlp456/article/details/46775489