ListView在我们Android项目中的地位是有目共睹的,相信几乎每一个App中都有它的身影。
ListView主要是用列表形式来加载数据,在特定情况下需要实现一些特殊功能:如刷新数据,加载数据,实现动画效果等。
作为我们常用的控件,有哪些需要注意的呢?
**为ListView的每一Item设置分隔线
第一种方法:也是最简单地方法,在布局文件中设置ListView的
divider属性
如:android:divider=”@color/black”
第二种方法:设置android:divider=”@null”表示不要分隔线,然后,自己在Item布局中添加分隔线。
<ListView> android:id="@+id/test_lv" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="#d1d1d1"<!--设置分隔线颜色--> android:dividerHeight="1px"<!--设置分隔线高度--> > </ListView>
** 默认情况下,ListView的item有点击效果,怎么样改变这种效果呢?
设置ListViewr的android:listSelector属性,比如可以设置透明取消这种效果android:listSelector=”@android:color/transparent”
也可以设置自己想要的效果,添加对应的color资源或drawble资源 就可以了。
** 当数据比较多的时候,ListView在滚动时候会显示一个默认的滚动条,要取消这个滚动条的话,可以设置android:scrollbars属性
如:android:scrollbars=”none”
** ListView性能优化,必用ViewHolder来充分发挥ListView的recycle机制
** ListView中数据动态变化,添加或删除数据操作,效果显而易见。
//实现ListView数据添加删除public class MainActivity extends Activity implements View.OnClickListener { private ListView test_lv; private List<TestBean> dataList; private TestAdapter adapter; private Button add_btn, del_btn; private ImageView emptyIv; private LinearLayout operator_ll; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initDatas(); initViews(); } private void initDatas() { dataList = new ArrayList<>(); for (int i = 0; i < 5; i++) { TestBean bean = new TestBean(); bean.setTitle("标题_" + i); bean.setContent("这是内容_" + i); bean.setType(i % 2 == 0 ? 1 : 2); dataList.add(bean); } } private void initViews() { this.test_lv = (ListView) findViewById(R.id.test_lv); this.add_btn = (Button) findViewById(R.id.add_btn); this.del_btn = (Button) findViewById(R.id.del_btn); emptyIv = (ImageView) findViewById(R.id.empty_iv); operator_ll=(LinearLayout)findViewById(R.id.operator_ll); this.add_btn.setOnClickListener(this); this.del_btn.setOnClickListener(this); if (dataList.size() == 0) { emptyIv.setVisibility(View.VISIBLE); operator_ll.setVisibility(View.GONE); test_lv.setEmptyView(emptyIv); } else { emptyIv.setVisibility(View.GONE); operator_ll.setVisibility(View.VISIBLE); adapter = new TestAdapter(this, dataList); test_lv.setAdapter(adapter); } test_lv.setSelection(15); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.add_btn: //默认增加在第一个位置 TestBean bean = new TestBean(); bean.setTitle("增加项"); bean.setContent("这是增加内容"); bean.setType(1); dataList.add(0, bean); break; case R.id.del_btn: //默认删除第一条数据 dataList.remove(0); break; } adapter.notifyDataSetChanged();//刷新ListView数据 }}//ListView对应Adapterpublic class TestAdapter extends BaseAdapter { private Context mContext; private List<TestBean> listDatas; public TestAdapter(Context mContext, List<TestBean> listDatas) { this.mContext = mContext; this.listDatas = listDatas; } @Override public int getCount() { return listDatas.size(); } @Override public Object getItem(int position) { return listDatas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = LayoutInflater.from(mContext).inflate(R.layout.test_item, null); holder.title = (TextView) convertView.findViewById(R.id.item_title); holder.content = (TextView) convertView.findViewById(R.id.item_content); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.content.setText(listDatas.get(position).getContent()); holder.title.setText(listDatas.get(position).getTitle()); return convertView; } final class ViewHolder { TextView title; TextView content; }}
** 类似聊天界面,ListView中有多种item样式效果
//实现ListView中多种item样式public class TypeActivity extends Activity { private ListView type_lv; private List<TestBean> dataList; private TypeAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_type); initDatas(); initViews(); } private void initDatas() { dataList = new ArrayList<>(); for (int i = 0; i < 9; i++) { TestBean bean = new TestBean(); bean.setTitle("标题_" + i); bean.setContent("这是内容_" + i); bean.setType(i % 2 == 0 ? 1 : 2); dataList.add(bean); } } private void initViews() { this.type_lv = (ListView) findViewById(R.id.type_lv); adapter = new TypeAdapter(this, dataList); type_lv.setAdapter(adapter); }}//对应Adapterpublic class TypeAdapter extends BaseAdapter { private Context mContext; private List<TestBean> listDatas; public TypeAdapter(Context mContext, List<TestBean> listDatas) { this.mContext = mContext; this.listDatas = listDatas; } @Override public int getCount() { return listDatas.size(); } @Override public Object getItem(int position) { return listDatas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; //根据样式设置不同的布局及数据展示 int type = getItemViewType(position); if (convertView == null) { holder = new ViewHolder(); if (type == 1) { convertView = LayoutInflater.from(mContext).inflate(R.layout.test_item, null); holder.title = (TextView) convertView.findViewById(R.id.item_title); holder.content = (TextView) convertView.findViewById(R.id.item_content); } else { convertView = LayoutInflater.from(mContext).inflate(R.layout.type_item, null); holder.title = (TextView) convertView.findViewById(R.id.type_title); holder.content = (TextView) convertView.findViewById(R.id.type_title); } convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.content.setText(listDatas.get(position).getContent()); holder.title.setText(listDatas.get(position).getTitle()); return convertView; }//关键方法之getViewTypeCount:获取有多少种样式 @Override public int getViewTypeCount() { return 2; }//关键方法之getItemViewType:获取item类型 @Override public int getItemViewType(int position) { return listDatas.get(position).getType(); } final class ViewHolder { TextView title; TextView content; }}
相关布局
activity_main.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" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:id="@+id/operator_ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/add_btn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="添加" /> <Button android:id="@+id/del_btn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="删除" /> </LinearLayout> <ListView android:id="@+id/test_lv" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="#d1d1d1"<!--分隔线颜色--> android:dividerHeight="1px"<!--分隔线高度--> android:listSelector="@android:color/transparent"<!--取消默认点击效果--> android:scrollbars="none"><!--隐藏滚动条--> </ListView> <ImageView android:id="@+id/empty_iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="100dp" android:src="@mipmap/empty" android:visibility="gone" /></LinearLayout>
activity_type.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" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/type_lv" android:layout_width="match_parent" android:layout_height="wrap_content" ></ListView></LinearLayout>
test_item.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingLeft="15dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"> <TextView android:id="@+id/item_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <TextView android:id="@+id/item_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="Hello World!" /></LinearLayout>
以上就是搜集的Android ListView常用小技巧全部内容,希望对大家的学习有所帮助。
新闻热点
疑难解答