在android开发中ListView控件应该是使用最频繁的控件之一,ListView可以设置其各种属性从而达到特定的UI效果,下面我们就来熟悉ListView的各种属性以及使用方法。首先写一下ListView的属性,
下面代码是我做一个项目时所设置ListView的属性:
<ListView android:id="@+id/memberList" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@color/color_dark_grey" android:dividerHeight="1dip" android:scrollingCache="true" android:cacheColorHint="#00000000" android:fastScrollEnabled="true" android:focusable="true"> </ListView>
一:divider
该属性是用于设置每个Item之间的间隔,这个间隔可以是张图片也可以是用某种颜色填充的特定高度的间隔,我上面的代码就是用颜色填充的高度为1dip的间
隔如果想去除这个间隔,只需要设置为:android:divider="@drawable/@null"就行。
二:srollingCache
该属性为滚动条缓存,当设置为ture时,假如你的ListView总共要加载20项Item,但是只有5项是可见的,android就会自动帮你缓存你后面即将显示的3个或多
个Item,当你向想滚动的同时android又自动会帮你缓存几个显示过的Item。下面是StackOverflow 上对该属性的解释:
三:cacheColorHint
该属性为设置ListView的背景颜色,正常我们点击ListView时其默认显示的颜色为黄色,但是有时候为了整体的界面风格需要设置其背景颜色,比如微信的
整个背景颜色就是绿色的,阿里旺旺就是深蓝色的。你只需设置 android:cacheColorHint="#ff4e31"即可,我上面的代码其背景颜色是用图片作为背景的,只
需将其设置成透明的即可:android:cacheColorHint="#00000000",然后再在Item的布局文件中将其背景设置成特定的图片就行。注意的是我们设置ListView
的背景图片,但是如果不设置此属性的话,滚动的时候还是会为原来的颜色的。
四:fadeScrollbars
该属性为设置ListView滚动条的动隐藏,android:fadeScrollbars="true" 时在ListView滚动时可以进行自动隐藏
五:stackFormBottom
该属性为设置ListView的显示位置,如果设置为true时,ListView就会显示最下面的几个Item
六:trainScriptMode
该属性为设置ListView的自动滚动效果,如果你想显示ListView的最新Item时,android:transcriptMode="alwaysScroll" 这样设置即可,然后设置ListView要
显示的位置,ListView就会自动滚动到要显示的位置
七:fadingedge
当ListView上下有黑影时,只需设置:android:fadingEdge="none"就行
ListView的属性就介绍到这里,下面介绍ListView的使用方法:
我一般习惯使用BaseAdapter作为ListView的适配器,下面就介绍ListView中BaseAdapter的使用方法,看代码:
public class ContactListAdapter extends BaseAdapter{ Context context; ImportFriendsTask friend; List<Friend> list; List<View> viewList = new ArrayList<View>(); public ContactListAdapter(Context context){ this.context = context; friend=new ImportFriendsTask(context); list =friend.loadContacts(); } public List<Friend> getImportFriendsTask(){ return list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } public List<View> receiveView(){ return viewList; } @Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView==null){ LinearLayout ll = (LinearLayout)LayoutInflater.from(context).inflate(R.layout.mylist, null); TextView name = (TextView)ll.findViewById(R.id.name); TextView telephone =(TextView)ll.findViewById(R.id.telephone); name.setText(list.get(position).getName().toString()); telephone.setText(list.get(position).getPhone().toString()); convertView=ll; Viewhold hold = new Viewhold(); hold.name=name; hold.telephone=telephone; convertView.setTag(hold); viewList.add(convertView); }else{ Viewhold hold = (Viewhold)convertView.getTag(); hold.name.setText(list.get(position).getName().toString()); hold.telephone.setText(list.get(position).getPhone().toString()); viewList.add(convertView); } return convertView; } } class Viewhold{ TextView name; TextView telephone; public TextView getName() { return name; } public void setName(TextView name) { this.name = name; } public TextView getTelephone() { return telephone; } public void setTelephone(TextView telephone) { this.telephone = telephone; } }
然后显示监听
list = (ListView)findViewById(R.id.listView1); adapter = new ContactListAdapter(this); list.setAdapter(adapter); list.setOnItemClickListener(this);
其实还有另外一种监听方法:自定义监听接口,然后回调。
public void setOnItemClickListener(OnItemClickListener listener) { mOnItemClickListener = listener; } /** * 重新定义菜单选项单击接口 */ public interface OnItemClickListener { public void onItemClick(View view, int position); } private void init() { onClickListener = new OnClickListener() { @Override public void onClick(View view) { selectedPos = (Integer)view.getTag(); Log.i("GroupAdapter:", selectedPos+"GroupAdapter:onClick开始监听"); setSelectedPosition(selectedPos); if (mOnItemClickListener != null){ mOnItemClickListener.onItemClick(view, selectedPos); Log.i("GroupAdapter:", "GroupAdapter:mOnItemClickListener.onItemClick"); } Log.i("GroupAdapter:", "GroupAdapter:onClick结束监听"); } }; } /** * 设置选中的position,并通知列表刷新 */ public void setSelectedPosition(int pos){ if (groupList != null && pos< groupList.size()){ selectedPos = pos; selectedText = groupList.get(pos); notifyDataSetChanged(); } } }
private void init(){ LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.view_distance, this, true); setBackgroundDrawable(getResources().getDrawable(R.drawable.choosearea_bg_mid)); listView = (ListView) findViewById(R.id.listView); adapter = new GroupAdapter(groupList, context); listView.setAdapter(adapter); adapter.setOnItemClickListener(itemClickListener()); } private GroupAdapter.OnItemClickListener itemClickListener(){ GroupAdapter.OnItemClickListener go = new OnItemClickListener() { @Override public void onItemClick(View view, int position) { if (mOnSelectListener != null){ showText = groupList.get(position); mOnSelectListener.getValue(String.valueOf(position+1), groupList.get(position)); Log.i("ViewGroup:", "ViewGroup:"+showText); } } }; return go; }
private void init(){ LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.view_distance, this, true); setBackgroundDrawable(getResources().getDrawable(R.drawable.choosearea_bg_mid)); listView = (ListView) findViewById(R.id.listView); adapter = new GroupAdapter(groupList, context); listView.setAdapter(adapter); adapter.setOnItemClickListener(itemClickListener()); } private GroupAdapter.OnItemClickListener itemClickListener(){ GroupAdapter.OnItemClickListener go = new OnItemClickListener() { @Override public void onItemClick(View view, int position) { if (mOnSelectListener != null){ showText = groupList.get(position); mOnSelectListener.getValue(String.valueOf(position+1), groupList.get(position)); Log.i("ViewGroup:", "ViewGroup:"+showText); } } }; return go; }
public void setOnItemClickListener(OnItemClickListener listener) { mOnItemClickListener = listener; } /** * 重新定义菜单选项单击接口 */ public interface OnItemClickListener { public void onItemClick(View view, int position); } private void init() { onClickListener = new OnClickListener() { @Override public void onClick(View view) { selectedPos = (Integer)view.getTag(); Log.i("GroupAdapter:", selectedPos+"GroupAdapter:onClick开始监听"); setSelectedPosition(selectedPos); if (mOnItemClickListener != null){ mOnItemClickListener.onItemClick(view, selectedPos); Log.i("GroupAdapter:", "GroupAdapter:mOnItemClickListener.onItemClick"); } Log.i("GroupAdapter:", "GroupAdapter:onClick结束监听"); } }; } /** * 设置选中的position,并通知列表刷新 */ public void setSelectedPosition(int pos){ if (groupList != null && pos< groupList.size()){ selectedPos = pos; selectedText = groupList.get(pos); notifyDataSetChanged(); } } }