码迷,mamicode.com
首页 > 其他好文 > 详细

Material Design(二)--UI控件之RecyclerView

时间:2016-05-15 11:07:02      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

RecyclerView是ListView的升级版,它提供了更好的性能而且更容易使用。

RecyclerView这个控件是一个可以装载大量的视图集合,并且可以非常效率的进行回收和滚动。当你list中的元素经常动态改变时可以使用RecyclerView控件。

RecyclerView非常容易使用,它提供了如下两个功能:

为每个条目位置提供了layout管理器(RecyclerView.setLayoutManager

为每个条目设置了操作动画(RecyclerView.setItemAnimator

下面的例子介绍了如何定义和使用一个RecyclerView:

1.在布局文件中添加一个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"/>  

2.初始化RecyclerView参数,设置layoutManager和adapter

 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);  
        }  
        ...  
    }  

3.创建一个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, 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;  
        }  
    }  

 

 原文地址:http://www.open-open.com/lib/view/open1416664070023.html

Material Design(二)--UI控件之RecyclerView

标签:

原文地址:http://www.cnblogs.com/goodpan/p/5494710.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!