ListView入门
ListView核心类
①ListView
setAdapter 设置一个适配器
②BaseAdapter
getcount
getItem
getId
getView
代码编写步骤
① 布局中声明listview节点(注意listview的高度不要使用包裹内容)
<RelativeLayout 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:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/lv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fastScrollEnabled="true"
android:text="@string/hello_world" />
</RelativeLayout>
② 条目布局创建出来
<RelativeLayout 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"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/iv_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_margin="5dp"/>
<TextView
android:id="@+id/tv_title"
android:layout_toRightOf="@id/iv_icon"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="7dp"
android:text="北京今年装修要多少钱"
android:textSize="18sp"
/>
<TextView
android:id="@+id/tv_content"
android:layout_toRightOf="@id/iv_icon"
android:layout_below="@id/tv_title"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="获得报价"
android:textColor="#999999"
android:layout_marginLeft="8dp"
android:textSize="16sp"
/>
</RelativeLayout>
③ 在Activity的oncreate方法中findviewbyid 找到 listView 控件
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
//找到listview控件
lv_list = (ListView) findViewById(R.id.lv_list);
④ 写一个类 继承BaseAdapter 重写里面四个方法
class MyAdapter extends BaseAdapter{
//数据集合中有多少个条目 通过这个方法判断listview会显示出多少个条目 这里传6 最终就会显示出6个条目
@Override
public int getCount() {
// TODO Auto-generated method stub
return 6;
}
//根据listview 当前的position 从数据集合中取出对应的数据
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
//Get the row id associated with the specified position in the list.
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
//通过这个方法 返回条目的布局视图
@Override
public View getView(int position, View convertView, ViewGroup parent) {
count++;
TextView tv_text = null;
if(convertView == null){
System.out.println("convertView == null创建了一个新的对象");
tv_text = new TextView(MainActivity.this);
}else{
tv_text = (TextView) convertView;
System.out.println("convertView不为空 复用旧的view");
}
//TextView tv_text = new TextView(MainActivity.this);
tv_text.setText("我是第"+position+"个条目");
return tv_text;
}
}
⑤创建adapter对象 调用setAdpater 给listview设置一个适配器
//创建适配器对象
MyAdapter myAdapter = new MyAdapter();
//给listview设置适配器
lv_list.setAdapter(myAdapter);
ListView优化
思路 重用convertView 判断convertView 是否为空 如果为空则需创建新的view对象 如果不为空则直接使用convertView