Android之ListView优化
关于ListView几个方面的优化:
- ListView的大小设定固定值;
- 复用convertView, 使用ViewHolder提高在容器中查找组件的效率;
- 使用分页加载;
- 快速滚动时, item不显示耗时加载的图片(或其他资源)
Android中有一个反复循环构件(Recycler),它的工作原理如下:(参考:http://www.cnblogs.com/xiaowenji/archive/2010/12/08/1900579.html)
- 如果有很多的item时,那么可见的条目存在内存中,其他的在Recycler中。
- ListView先请求一个type1视图(getView)然后请求其他可见的项目。convertView在getView中是空(null)的。
- 当item1滚出屏幕,并且一个新的项目从屏幕底端上来时,ListView再请求一个type1视图。convertView此时不是空值了,它的值是item1。你只需设定新的数据然后返回convertView,不必重新创建一个视图。
代码示例:
- public class ListViewActivity extends Activity {
- private ArrayList<String> list = new ArrayList<>();
- private ListView listView;
- private ItemAdapter itemAdapter;
- private int index = 0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_list_view);
- listView = (ListView) findViewById(R.id.listView);
- initData();
- itemAdapter = new ItemAdapter();
- listView.setAdapter(itemAdapter);
- }
- public void initData(){
- for(int i = 0; i < 50; i++){
- list.add("item--" + index++);
- }
- }
- class ItemAdapter extends BaseAdapter {
- @Override
- public int getCount() {
- return list.size();
- }
- @Override
- public Object getItem(int position) {
- if(position >= 0 && position < list.size()) return list.get(position);
- return null;
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- Log.e("getView:--" + position, convertView + "");
- ViewHolder vh;
- if(convertView == null){
- convertView = getLayoutInflater().inflate(R.layout.list_item, null);
- vh = new ViewHolder();
- vh.textView = (TextView) convertView.findViewById(R.id.textView);
- convertView.setTag(vh);
- }else{
- vh = (ViewHolder) convertView.getTag();
- }
- vh.textView.setText("item--" + position);
- return convertView;
- }
- class ViewHolder{
- TextView textView;
- }
- }
- }
一、ListView的大小设定固定值
首先需要了解Adapter的使用,里面有一个getCount()是返回ListView要显示的item个数,而手机界面可以显示的个数是通过: ListView的高度/item的高度 。getView()方法是需要返回一个视图,如果ListView的高度不确定,而是使用wrap_content属性,那么ListView在匹配的时候,就会在一直在尝试可以显示多少个item在ListView上,那么会影响到性能问题。
- <ListView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/listView"
- android:layout_alignParentTop="true"
- android:layout_alignParentStart="true" />
1. 如果在layout里面没有设置ListView的高度为定值时,观察如下现象:
10-20 17:01:59.857 21761-21761/com.example.lenovo.listviewtest E/getView:--0﹕ null
10-20 17:01:59.870 21761-21761/com.example.lenovo.listviewtest E/getView:--1﹕ android.widget.LinearLayout{42994980 V.E..... ......I. 0,0-0,0}
10-20 17:01:59.870 21761-21761/com.example.lenovo.listviewtest E/getView:--2﹕ android.widget.LinearLayout{42994980 V.E..... ......I. 0,0-0,0}
10-20 17:01:59.870 21761-21761/com.example.lenovo.listviewtest E/getView:--3﹕ android.widget.LinearLayout{42994980 V.E..... ......I. 0,0-0,0}
10-20 17:01:59.870 21761-21761/com.example.lenovo.listviewtest E/getView:--4﹕ android.widget.LinearLayout{42994980 V.E..... ......I. 0,0-0,0}
10-20 17:01:59.874 21761-21761/com.example.lenovo.listviewtest E/getView:--5﹕ android.widget.LinearLayout{42994980 V.E..... ......I. 0,0-0,0}
10-20 17:01:59.874 21761-21761/com.example.lenovo.listviewtest E/getView:--6﹕ android.widget.LinearLayout{42994980 V.E..... ......I. 0,0-0,0}
10-20 17:01:59.874 21761-21761/com.example.lenovo.listviewtest E/getView:--7﹕ android.widget.LinearLayout{42994980 V.E..... ......I. 0,0-0,0}
10-20 17:01:59.934 21761-21761/com.example.lenovo.listviewtest E/getView:--0﹕ android.widget.LinearLayout{42994980 V.E..... ......I. 0,0-0,0}
10-20 17:01:59.937 21761-21761/com.example.lenovo.listviewtest E/getView:--1﹕ null
10-20 17:01:59.940 21761-21761/com.example.lenovo.listviewtest E/getView:--2﹕ null
10-20 17:01:59.944 21761-21761/com.example.lenovo.listviewtest E/getView:--3﹕ null
10-20 17:01:59.947 21761-21761/com.example.lenovo.listviewtest E/getView:--4﹕ null
10-20 17:01:59.947 21761-21761/com.example.lenovo.listviewtest E/getView:--5﹕ null
10-20 17:01:59.950 21761-21761/com.example.lenovo.listviewtest E/getView:--6﹕ null
10-20 17:01:59.950 21761-21761/com.example.lenovo.listviewtest E/getView:--7﹕ null
10-20 17:01:59.957 21761-21761/com.example.lenovo.listviewtest E/getView:--0﹕ null
10-20 17:01:59.957 21761-21761/com.example.lenovo.listviewtest E/getView:--1﹕ android.widget.LinearLayout{429a2fa0 V.E..... ......I. 0,0-0,0}
10-20 17:01:59.957 21761-21761/com.example.lenovo.listviewtest E/getView:--2﹕ android.widget.LinearLayout{429a2fa0 V.E..... ......I. 0,0-0,0}
10-20 17:01:59.957 21761-21761/com.example.lenovo.listviewtest E/getView:--3﹕ android.widget.LinearLayout{429a2fa0 V.E..... ......I. 0,0-0,0}
10-20 17:01:59.957 21761-21761/com.example.lenovo.listviewtest E/getView:--4﹕ android.widget.LinearLayout{429a2fa0 V.E..... ......I. 0,0-0,0}
10-20 17:01:59.960 21761-21761/com.example.lenovo.listviewtest E/getView:--5﹕ android.widget.LinearLayout{429a2fa0 V.E..... ......I. 0,0-0,0}
10-20 17:01:59.960 21761-21761/com.example.lenovo.listviewtest E/getView:--6﹕ android.widget.LinearLayout{429a2fa0 V.E..... ......I. 0,0-0,0}
10-20 17:01:59.960 21761-21761/com.example.lenovo.listviewtest E/getView:--7﹕ android.widget.LinearLayout{429a2fa0 V.E..... ......I. 0,0-0,0}
.....
2. 如果设置ListView的高度为定值时,观察如下现象:
10-20 17:13:31.624 29386-29386/com.example.lenovo.listviewtest E/getView:--0﹕ null
10-20 17:13:31.631 29386-29386/com.example.lenovo.listviewtest E/getView:--1﹕ null
10-20 17:13:31.634 29386-29386/com.example.lenovo.listviewtest E/getView:--2﹕ null
10-20 17:13:31.638 29386-29386/com.example.lenovo.listviewtest E/getView:--3﹕ null
10-20 17:13:31.641 29386-29386/com.example.lenovo.listviewtest E/getView:--4﹕ null
10-20 17:13:31.644 29386-29386/com.example.lenovo.listviewtest E/getView:--5﹕ null
10-20 17:13:31.644 29386-29386/com.example.lenovo.listviewtest E/getView:--6﹕ null
10-20 17:13:31.651 29386-29386/com.example.lenovo.listviewtest E/getView:--7﹕ null
10-20 17:20:42.501 29386-29386/com.example.lenovo.listviewtest E/getView:--8﹕ null
10-20 17:20:42.614 29386-29386/com.example.lenovo.listviewtest E/getView:--9﹕ android.widget.LinearLayout{4299bd90 V.E..... ........ 0,-133-656,7}
10-20 17:20:42.698 29386-29386/com.example.lenovo.listviewtest E/getView:--10﹕ android.widget.LinearLayout{4299e9b8 V.E..... ........ 0,-128-656,12}
10-20 17:20:42.751 29386-29386/com.example.lenovo.listviewtest E/getView:--11﹕ android.widget.LinearLayout{4299fda0 V.E..... ........ 0,-138-656,2}
10-20 17:20:42.851 29386-29386/com.example.lenovo.listviewtest E/getView:--12﹕ android.widget.LinearLayout{429a1188 V.E..... ........ 0,-129-656,11}
10-20 17:20:42.998 29386-29386/com.example.lenovo.listviewtest E/getView:--13﹕ android.widget.LinearLayout{429a2570 V.E..... ........ 0,-137-656,3}
10-20 17:20:50.464 29386-29386/com.example.lenovo.listviewtest E/getView:--14﹕ android.widget.LinearLayout{429a3958 V.E..... ........ 0,-138-656,2}
10-20 17:20:58.315 29386-29386/com.example.lenovo.listviewtest E/getView:--15﹕ android.widget.LinearLayout{429a4d40 V.E..... ........ 0,-140-656,0}
结论:
由以上比较可以知道,设定ListView的属性时,为了提高匹配的效率,可以把ListView的高度设为确定值。
二、复用convertView, 使用ViewHolder提高在容器中查找组件的效率
1. 上面的代码已经重复使用了convertView了,如果每次都是重新对item中的布局实例化成View对象,view = getLayoutInflater().inflate(R.layout.list_item, null); 这是比较耗时的。
- view = getLayoutInflater().inflate(R.layout.list_item, null);
- Log.e("getView:--" + position, view + "");
10-20 19:35:03.723 18046-18046/com.example.lenovo.listviewtest E/getView:--0﹕ android.widget.LinearLayout{42998e50 V.E..... ......I. 0,0-0,0}
10-20 19:35:03.730 18046-18046/com.example.lenovo.listviewtest E/getView:--1﹕ android.widget.LinearLayout{4299bec0 V.E..... ......I. 0,0-0,0}
10-20 19:35:03.733 18046-18046/com.example.lenovo.listviewtest E/getView:--2﹕ android.widget.LinearLayout{4299d570 V.E..... ......I. 0,0-0,0}
10-20 19:35:03.737 18046-18046/com.example.lenovo.listviewtest E/getView:--3﹕ android.widget.LinearLayout{4299ec20 V.E..... ......I. 0,0-0,0}
10-20 19:35:03.740 18046-18046/com.example.lenovo.listviewtest E/getView:--4﹕ android.widget.LinearLayout{429a02d0 V.E..... ......I. 0,0-0,0}
10-20 19:35:03.743 18046-18046/com.example.lenovo.listviewtest E/getView:--5﹕ android.widget.LinearLayout{429a1980 V.E..... ......I. 0,0-0,0}
10-20 19:35:03.747 18046-18046/com.example.lenovo.listviewtest E/getView:--6﹕ android.widget.LinearLayout{429a3030 V.E..... ......I. 0,0-0,0}
10-20 19:35:03.750 18046-18046/com.example.lenovo.listviewtest E/getView:--7﹕ android.widget.LinearLayout{429a46e0 V.E..... ......I. 0,0-0,0}
10-20 19:35:11.330 18046-18046/com.example.lenovo.listviewtest E/getView:--8﹕ android.widget.LinearLayout{429a9638 V.E..... ......I. 0,0-0,0}
10-20 19:35:13.503 18046-18046/com.example.lenovo.listviewtest E/getView:--0﹕ android.widget.LinearLayout{429ab200 V.E..... ......I. 0,0-0,0}
10-20 19:35:15.697 18046-18046/com.example.lenovo.listviewtest E/getView:--8﹕ android.widget.LinearLayout{429ad050 V.E..... ......I. 0,0-0,0}
10-20 19:35:18.773 18046-18046/com.example.lenovo.listviewtest E/getView:--8﹕ android.widget.LinearLayout{429aed90 V.E..... ......I. 0,0-0,0}
10-20 19:35:20.477 18046-18046/com.example.lenovo.listviewtest E/getView:--9﹕ android.widget.LinearLayout{429b0888 V.E..... ......I. 0,0-0,0}
10-20 19:35:20.634 18046-18046/com.example.lenovo.listviewtest E/getView:--10﹕ android.widget.LinearLayout{429b21c0 V.E..... ......I. 0,0-0,0}
10-20 19:35:21.484 18046-18046/com.example.lenovo.listviewtest E/getView:--2﹕ android.widget.LinearLayout{429b3ba8 V.E..... ......I. 0,0-0,0}
10-20 19:35:21.604 18046-18046/com.example.lenovo.listviewtest E/getView:--1﹕ android.widget.LinearLayout{429b54e0 V.E..... ......I. 0,0-0,0}
10-20 19:35:21.680 18046-18046/com.example.lenovo.listviewtest E/getView:--0﹕ android.widget.LinearLayout{429b6d90 V.E..... ......I. 0,0-0,0}
2. 上面的例子是一个view就一个TextView组件,但是如果当item的结构比较复杂时,通过convertView.findById()来查找每个组件id就会很费时,如果建立一个ViewHold的话,就避免了在view容器中查找组件的耗时工作了。
10-20 20:08:49.140 3604-3604/com.example.lenovo.listviewtest E/getView:--0﹕ null
10-20 20:08:49.146 3604-3604/com.example.lenovo.listviewtest E/getView:--1﹕ null
10-20 20:08:49.150 3604-3604/com.example.lenovo.listviewtest E/getView:--2﹕ null
10-20 20:08:49.153 3604-3604/com.example.lenovo.listviewtest E/getView:--3﹕ null
10-20 20:08:49.156 3604-3604/com.example.lenovo.listviewtest E/getView:--4﹕ null
10-20 20:08:49.160 3604-3604/com.example.lenovo.listviewtest E/getView:--5﹕ null
10-20 20:08:49.160 3604-3604/com.example.lenovo.listviewtest E/getView:--6﹕ null
10-20 20:08:49.163 3604-3604/com.example.lenovo.listviewtest E/getView:--7﹕ null
10-20 20:08:53.760 3604-3604/com.example.lenovo.listviewtest E/getView:--8﹕ null
10-20 20:08:55.023 3604-3604/com.example.lenovo.listviewtest E/getView:--9﹕ android.widget.LinearLayout{ V.E..... ........ 0,-137-656,3} //item--0 convertView = Recycler中的type1
10-20 20:08:56.040 3604-3604/com.example.lenovo.listviewtest E/getView:--9﹕ android.widget.LinearLayout{ V.E..... ........ 0,1072-656,1212}
10-20 20:08:56.190 3604-3604/com.example.lenovo.listviewtest E/getView:--10﹕ android.widget.LinearLayout{4299c098 V.E..... ........ 0,-132-656,8} //item--1 convertView = Recycler中的type2
10-20 20:08:57.153 3604-3604/com.example.lenovo.listviewtest E/getView:--11﹕ android.widget.LinearLayout{4299d498 V.E..... ........ 0,-139-656,1}
10-20 20:08:58.500 3604-3604/com.example.lenovo.listviewtest E/getView:--12﹕ android.widget.LinearLayout{4299e898 V.E..... ........ 0,-132-656,8}
10-20 20:09:00.160 3604-3604/com.example.lenovo.listviewtest E/getView:--13﹕ android.widget.LinearLayout{4299fc98 V.E..... ........ 0,-134-656,6}
10-20 20:09:00.343 3604-3604/com.example.lenovo.listviewtest E/getView:--14﹕ android.widget.LinearLayout{429a1098 V.E..... ........ 0,-130-656,10}
10-20 20:09:02.023 3604-3604/com.example.lenovo.listviewtest E/getView:--15﹕ android.widget.LinearLayout{429a2498 V.E..... ........ 0,-139-656,1}
10-20 20:09:03.957 3604-3604/com.example.lenovo.listviewtest E/getView:--16﹕ android.widget.LinearLayout{429a3898 V.E..... ........ 0,-135-656,5}
10-20 20:09:06.000 3604-3604/com.example.lenovo.listviewtest E/getView:--7﹕ android.widget.LinearLayout{429a3898 V.E..... ........ 0,1053-656,1193}
10-20 20:09:06.137 3604-3604/com.example.lenovo.listviewtest E/getView:--6﹕ android.widget.LinearLayout{429a2498 V.E..... ........ 0,1043-656,1183} //item--6 从convertView中取得
10-20 20:09:06.270 3604-3604/com.example.lenovo.listviewtest E/getView:--5﹕ android.widget.LinearLayout{429a1098 V.E..... ........ 0,1063-656,1203}
在滑动ListView的时候,当最下面显示item--9的时候,item--0就存在Recycler的type1中,当convertView就不会为null了,同时通过观察以上的getView--15可以知道,此时item--6的视图在Recycler中,当向上滑动ListView显示到getView--6的时候,是从convertView中获取的,而不是新实例化layout得到的。
总结: 通过对比,显然复用convertView可以提高ListView的显示效略,起到优化作用。同时如果item内部的组件多的时候,建议使用ViewHold, 可以提高在view容器中查找组件的效率。
三、使用分页加载数据
当客户端从服务端获取数据量过大,一般为用户考虑流量,我们都需要对数据进行分页加载(特别是图片的加载),就上面的例子,模拟ListView的分页显示和下拉刷新功能。需要用到ListView的滚动监听。
- public class ListViewActivity extends Activity implements AbsListView.OnScrollListener{
- private Vector<String> list = new Vector<>(); //Vector保证线程安全
- private ListView listView;
- private ItemAdapter itemAdapter;
- private int index = 0;
- private int count = 0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_list_view);
- listView = (ListView) findViewById(R.id.listView);
- listView.addFooterView(getLayoutInflater().inflate(R.layout.footer_view, null));
- initData();
- itemAdapter = new ItemAdapter();
- listView.setAdapter(itemAdapter);
- listView.setOnScrollListener(this);
- }
- public void initData(){
- for(int i = 0; i < 20; i++){
- list.add("item--" + index++);
- }
- }
- private Handler handler = new Handler(){
- @Override
- public void handleMessage(Message msg) {
- if(msg.what == 100){
- itemAdapter.notifyDataSetChanged(); //adapter更新数据,listView变化
- }
- }
- };
- Runnable r = new Runnable() {
- @Override
- public void run() {
- initData();
- try {
- Thread.sleep(2000); //休眠2秒
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- handler.sendEmptyMessage(100);
- }
- };
- @Override
- public void onScrollStateChanged(AbsListView view, int scrollState) {
- if(itemAdapter.getCount() == count && scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE){
- new Thread(r).start(); //子线程运行
- }
- }
- @Override
- public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
- count = firstVisibleItem + visibleItemCount - 1; //减1是减去listView的footerView
- }
- class ItemAdapter extends BaseAdapter {
- @Override
- public int getCount() {
- return list.size();
- }
- @Override
- public Object getItem(int position) {
- if(position >= 0 && position < list.size()) return list.get(position);
- return null;
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- Log.e("getView:--" + position, convertView + ""); //观察convertView值
- ViewHolder vh;
- if(convertView == null){
- convertView = getLayoutInflater().inflate(R.layout.list_item, null);
- vh = new ViewHolder();
- vh.textView = (TextView) convertView.findViewById(R.id.textView);
- convertView.setTag(vh);
- }else{
- vh = (ViewHolder) convertView.getTag();
- }
- vh.textView.setText("item--" + position);
- return convertView;
- }
- class ViewHolder{
- TextView textView;
- }
- }
- }
listView页脚:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal" android:layout_width="match_parent"
- android:layout_height="wrap_content" android:gravity="center_horizontal">
- <ProgressBar
- style="?android:attr/progressBarStyleSmall"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/progressBar" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textAppearance="?android:attr/textAppearanceSmall"
- android:text="正在努力加载..."
- android:id="@+id/textView" />
- </LinearLayout>
四、快速滚动时, item中可以不显示耗时加载的图片或其他资源
一般情况下需要从网络获取图片显示需要做缓存处理,那么此时可以实现ListView的滚动监听OnScrollListener接口中的方法:
- private int startPosition;
- private int endPosition;
- @Override
- public void onScrollStateChanged(AbsListView view, int scrollState) {
- switch (scrollState){
- case SCROLL_STATE_IDLE:{
- if(itemAdapter.getCount() == count){
- new Thread(r).start(); //子线程运行
- }
- for(int i = startPosition; i < endPosition; i++){
- //可以设置图片的加载显示
- }
- break;
- }
- case SCROLL_STATE_TOUCH_SCROLL:
- //可以设置图片的加载显示
- //...
- break;
- case SCROLL_STATE_FLING: //快速滚动
- break;
- }
- }
- @Override
- public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
- count = firstVisibleItem + visibleItemCount - 1; //减1是减去listView的footerView
- startPosition = firstVisibleItem; //可视起始的item位置
- endPosition = count; //可视最后的item位置
- }
Android之ListView优化的更多相关文章
- Android之——ListView优化
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47209253 作为client.其最基本的任务就是最直观的和用户交互.从serve ...
- 【Android】ListView 优化
重用 ListView Item ListView创建时其会创建屏幕可容纳数量的 Item.ListView 滚动时,刚消失的 item 会被保存到回收池中.新出现的 item 从回收池中获取避免反复 ...
- 【Android】listview优化
http://www.cnblogs.com/over140/archive/2011/03/23/1991100.html http://blog.sina.com.cn/s/blog_5fc933 ...
- Android之ListView性能优化——一行代码绑定数据——万能适配器
如下图,加入现在有一个这样的需求图,你会怎么做?作为一个初学者,之前我都是直接用SimpleAdapter结合一个Item的布局来实现的,感觉这样实现起来很方便(基本上一行代码就可以实现),而且也没有 ...
- Android中ListView的几种常见的优化方法
Android中的ListView应该算是布局中几种最常用的组件之一了,使用也十分方便,下面将介绍ListView几种比较常见的优化方法: 首先我们给出一个没有任何优化的Listview的Adapte ...
- (转)Android之ListView原理学习与优化总结
转自: http://jishu.zol.com.cn/12893.html 在整理前几篇文章的时候有朋友提出写一下ListView的性能优化方面的东西,这个问题也是小马在面试过程中被别人问到的….. ...
- ym——Android之ListView性能优化
转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持! Android之ListView性能优化 假设有看过我写过的15k面试题的朋友们一定知 ...
- wemall app商城源码Android之ListView异步加载网络图片(优化缓存机制)
wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享wemall app商城源码Android之L ...
- android listview优化:滑动时颜色错乱问题
最近android的listview写多了,也学习了各种listview的优化,列如viewHolder的使用.今天做item颜色设置时遇到一个新的问题.我这里设置“未完成”是灰色的,“已完成”是 ...
随机推荐
- python语言精粹《一》
第一章 静态语言:需要声明类型.变量不能改变类型! 动态语言:(也称脚本语言)主要的应用场景都是很短的应用程序(脚本),比如给静态语言编写的程序进行数据预处理.这样的程序通常也统称胶水代码. pyth ...
- 在附件管理模块中增加对FTP 上传和预览的支持
在之前介绍的附件管理模块里面<Winform开发框架之通用附件管理模块>以及<Winform开发框架之附件管理应用>,介绍了附件的管理功能,通过对数据库记录的处理和文件的管理, ...
- JavaScript严格模式有什么不同
看ES6,瞄到“严格模式”,问了下自己什么是“严格模式”?答案好像不是很明朗,遂总结如下: 严格模式声明:“use strict”; 1.禁止变量未声明就赋值 2.限制动态绑定(属性和方法归属哪个对象 ...
- 黄油刀ButterKnife的使用
1.ButterKnife是一个由JakeWharton写的开源框架,它使用注解处理将属性和方法和View绑定,以生成模板代码. 2.作用: @1通过使用@BindView 注释属性取消了findVi ...
- 使用 bufferedreader 的好处
简单的说,一次IO操作,读取一个字节也是读取,读取8k个字节也是读取,两者花费时间相差不多.而一次IO的来回操作却要耗费大量时间.好比是一辆大型汽车(设装100人),要去车站接人到公司,接一个人也是接 ...
- [转] (CQRS)命令和查询责任分离架构模式(二) 之 Command的实现
概述 继续引用上篇文章中的图片(来源于Udi Dahan博客),UI中的写入操作都将被封装为一个命令中,发送给Domain Model来处理. 我们遵循Domain Driven Design的设计思 ...
- Swift自增和自增运算
自增和自增运算 和 C 语言一样,Swift 也提供了方便对变量本身加1或减1的自增(++)和自减(--)的运算符.其操作对象可以是整形和浮点型. var i = ++i // 现在 i = 1 ...
- TPYBoard自制微信远程智能温湿度计
智能时代一夜间什么都能远程了.创业者想着如何做智能产品,如何做远程控制.DIY爱好者也想着如何自制各种奇妙的工具.这里和大家一起学习制作一款廉价的智能温湿度计.说它廉价是因为共计花费不过40元,说它智 ...
- 很好的复习资料: SQL语句到底怎么写 ?
本文用到的数据库如下: CREATE DATABASE exam; /*创建部门表*/ CREATE TABLE dept( deptno INT PRIMARY KEY, dname VARCHAR ...
- Generic(泛型)
什么是泛型:"通过参数化类型来实现在同一份代码上操作多种数据类型.利用"参数化类型"将类型抽象化,从而实现灵活的复用". 简单来说泛型就是为了使一些代码能够重复 ...