我们看一个安卓列表的实现,在RecycleView出来之前,安卓的列表一直是ListView来实现的,面试的时候也经常会问ListView的优化问题,这一篇我们先来看ListView的基本用法
layout文件是:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/white_a" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical"> <ListView android:id="@+id/news_home_listview" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@null" android:fadingEdge="none" android:listSelector="@color/trans_color" android:scrollbars="none" android:scrollingCache="false" /> </LinearLayout>
stackFromBottom属性,这只该属性之后你做好的列表就会显示你列表的最下面,值为true和false android:stackFromBottom="true"
transciptMode属性,需要用ListView或者其它显示大量Items的控件实时跟踪或者查看信息,并且希望最新的条目可以自动滚动到可视范围内。通过设置的控件transcriptMode属性可以将Android平台的控件(支持ScrollBar)自动滑动到最底部。
android:transcriptMode="alwaysScroll"cacheColorHint属性,很多人希望能够改变一下它的背景,使他能够符合整体的UI设计,改变背景背很简单只需要准备一张图片然后指定属性 android:background="@drawable/bg",不过不要高兴地太早,当你这么做以后,发现背景是变了,但是当你拖动,或者点击list空白位置的时候发现ListItem都变成黑色的了,破坏了整体效果。
如果你只是换背景的颜色的话,可以直接指定android:cacheColorHint为你所要的颜色,如果你是用图片做背景的话,那也只要将android:cacheColorHint指定为透明(#00000000)就可以了
divider属性,该属性作用是每一项之间需要设置一个图片做为间隔,或是去掉item之间的分割线
android:divider="@drawable/list_driver" 其中 @drawable/list_driver 是一个图片资源,如果不想显示分割线则只要设置为android:divider="@drawable/@null" 就可以了
fadingEdge属性,上边和下边有黑色的阴影 android:fadingEdge="none" 设置后没有阴影了
scrollbars属性,作用是隐藏listView的滚动条,
android:scrollbars="none"与setVerticalScrollBarEnabled(true);的效果是一样的,不活动的时候隐藏,活动的时候也隐藏
fadeScrollbars属性,android:fadeScrollbars="true" 配置ListView布局的时候,设置这个属性为true就可以实现滚动条的自动隐藏和显示。
fastScrollEnabled属性 ,
很多开发者不知道ListView列表控件的快速滚动滑块是如何启用的,这里Android开发网告诉大家,辅助滚动滑块只需要一行代码就可以搞定,如果你使用XML布局只需要在ListView节点中加入 android:fastScrollEnabled="true" 这个属性即可,而对于Java代码可以通过myListView.setFastScrollEnabled(true); 来控制启用,参数false为隐藏。 还有一点就是当你的滚动内容较小,不到当前ListView的3个屏幕高度时则不会出现这个快速滚动滑块,同时该方法仍然是AbsListView的基础方法,可以在ListView或GridView等子类中使用快速滚动辅助。
android:listSelector="@color/pink" listView item 选中时的颜色。默认为橙黄底色。
在Activity中使用ListView
public class TestListActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.feeds_main); ListView listView = (ListView) findViewById(R.id.news_home_listview); listView.setAdapter(new FeedsMainAdapter(this,getList())); } public ArrayList<String> getList() { ArrayList<String> list = new ArrayList<>(); list.add("性能优化"); list.add("图片优化"); list.add("存储优化"); return list; } }
adapter在ListView和数据源之间起到桥梁的作用,避免listview和数据源直接接触,而导致因为数据源的复杂性使listview显得臃肿。
每个ListView都必须有一个adapter来展示listview中的数据,这次我们使用BaseAdapter来实现,一般
BaseAdapter都是传入一个ArrayList来实现比较多
public class FeedsMainAdapter extends BaseAdapter{ private ArrayList<String> mList; private Context mContext; public FeedsMainAdapter(Context context,ArrayList<String> list) { this.mContext = context; this.mList = list; } @Override public int getCount() { return mList.size(); } @Override public String getItem(int position) { return mList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if(convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.feeds_main_item,null); holder = new ViewHolder(); holder.mTextView = (TextView)convertView.findViewById(R.id.main_text); holder.mPic = (SimpleDraweeView)convertView.findViewById(R.id.pic); convertView.setTag(holder); }else{ holder = (ViewHolder)convertView.getTag(); } holder.mTextView.setText(getItem(position)); if(position < Images.imageUrls.length) { holder.mPic.setImageURI(Images.imageUrls[position]); } return convertView; } class ViewHolder{ public TextView mTextView; public SimpleDraweeView mPic; } }
Adapter中的item的xml为:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:fresco="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/apk_all_white_selector" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:minHeight="50dp"> <com.facebook.drawee.view.SimpleDraweeView android:layout_width="40dp" android:layout_height="40dp" android:layout_marginLeft="20dp" android:id="@+id/pic" android:layout_gravity="center_vertical" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="20dp" android:gravity="center" android:layout_gravity="left|center_vertical" android:id="@+id/main_text" android:textSize="15sp" android:textColor="#000000"/> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/black" android:layout_marginTop="5dp"/> </LinearLayout>
代码我已经上传github:https://github.com/nickgao1986/StepSport