每日日报

ListView入门

1ListView核心类

            ①ListView
                    setAdapter 设置一个适配器
            ②BaseAdapter
                    getcount
                    getItem
                    getId
                    getView

2代码编写步骤

① 布局中声明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"
2.    xmlns:tools="http://schemas.android.com/tools"
3.    android:layout_width="match_parent"
4.    android:layout_height="match_parent"
5.    tools:context=".MainActivity" >
6.   <ImageView 
7.       android:id="@+id/iv_icon"
8.       android:layout_width="wrap_content"
9.       android:layout_height="wrap_content"
10.       android:src="@drawable/ic_launcher"
11.       android:layout_margin="5dp"/>
12.   <TextView
13.       android:id="@+id/tv_title"
14.       android:layout_toRightOf="@id/iv_icon"
15.       android:layout_height="wrap_content"
16.       android:layout_width="wrap_content"
17.       android:layout_margin="7dp"
18.       android:text="北京今年装修要多少钱"
19.       android:textSize="18sp"
20.       />
21.   <TextView
22.       android:id="@+id/tv_content"
23.       android:layout_toRightOf="@id/iv_icon"
24.       android:layout_below="@id/tv_title"
25.       android:layout_height="wrap_content"
26.       android:layout_width="wrap_content"
27.       android:text="获得报价"
28.       android:textColor="#999999"
29.       android:layout_marginLeft="8dp"
30.       android:textSize="16sp"
31.       />
32.</RelativeLayout>

③ 在Activity的oncreate方法中findviewbyid 找到 listView 控件

1。 protected void onCreate(Bundle savedInstanceState) {
2.        super.onCreate(savedInstanceState);
3.        setContentView(R.layout.activity_main3);
4.        //找到listview控件
5.        lv_list = (ListView) findViewById(R.id.lv_list);

 ④ 写一个类 继承BaseAdapter 重写里面四个方法

1. class MyAdapter extends BaseAdapter{
2.        //数据集合中有多少个条目 通过这个方法判断listview会显示出多少个条目 这里传6 最终就会显示出6个条目
3.        @Override
4.        public int getCount() {
5.            // TODO Auto-generated method stub
6.            return 6;
7.        }
8.        //根据listview 当前的position 从数据集合中取出对应的数据
9.        @Override
10.        public Object getItem(int position) {
11.            // TODO Auto-generated method stub
12.            return null;
13.        }
14.        //Get the row id associated with the specified position in the list.
15.        @Override
16.        public long getItemId(int position) {
17.            // TODO Auto-generated method stub
18.            return position;
19.        }
20.        //通过这个方法 返回条目的布局视图
21.        @Override
22.        public View getView(int position, View convertView, ViewGroup parent) {
23.            count++;
24.            TextView tv_text  = null;
25.            if(convertView == null){
26.                System.out.println("convertView == null创建了一个新的对象");
27.                tv_text = new TextView(MainActivity.this);
28.            }else{
29.                tv_text = (TextView) convertView;
30.                System.out.println("convertView不为空 复用旧的view");
31.            }
32.            
33.            //TextView tv_text = new TextView(MainActivity.this);
34.            tv_text.setText("我是第"+position+"个条目");
35.            return tv_text;
36.        }
37.    }

⑤创建adapter对象 调用setAdpater 给listview设置一个适配器

1.  //创建适配器对象
2.        MyAdapter myAdapter = new MyAdapter();
3.        //给listview设置适配器
4.        lv_list.setAdapter(myAdapter);

ListView优化

    思路 重用convertView 判断convertView 是否为空 如果为空则需创建新的view对象 如果不为空则直接使用convertView
 
原文地址:https://www.cnblogs.com/zhukaile/p/14836112.html