strings.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version= "1.0"  encoding= "utf-8" ?>
<resources>
 
     <string name= "app_name" >ListView</string>
     <string name= "hello_world" >Hello world!</string>
     <string name= "action_settings" >Settings</string>
 
     
     
     <string-array  name= "names" >
           <item>萌萌</item>
           <item>小白</item>
           <item>张三</item>
           <item>李四</item>
           <item>王五</item>
           <item>萌萌</item>
           <item>小白</item>
           <item>张三</item>
           <item>李四</item>
           <item>王五</item>
           <item>萌萌</item>
           <item>小白</item>
           <item>张三</item>
           <item>李四</item>
           <item>王五</item>
     </string-array>
</resources>

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<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:paddingBottom= "@dimen/activity_vertical_margin"
     android:paddingLeft= "@dimen/activity_horizontal_margin"
     android:paddingRight= "@dimen/activity_horizontal_margin"
     android:paddingTop= "@dimen/activity_vertical_margin"
     tools:context= "com.example.listview.MainActivity"  >
 
    
     <ListView
         android:id= "@+id/ListView1_1" 
         android:layout_width= "match_parent"
         android:layout_height= "match_parent"
         android:entries= "@array/names"
        
         android:footerDividersEnabled= "true"
       
         android:scrollbars= "horizontal|vertical"
         android:fastScrollEnabled= "true"
         android:listSelector= "#f5ffcc"
         android:drawSelectorOnTop= "false"
         >
         
     </ListView>
</RelativeLayout>
<!--    1 .设置滚动条
         android:scrollbars= "horizontal|vertical"    水平和垂直方向的
         android:fastScrollEnabled= "true"            快速滚动条
          2 .item之间的间隔宽度
           android:dividerHeight= "2dp"
         3 .item之间的颜色
           android:divider= "#00ffff"
         4 .选中时的颜色
            android:listSelector= "#00ffff"
         5 .选中时,将字显示出来,尤其是写了选中时的颜色时,一定要加上这一条,否则字被遮盖
            android:drawSelectorOnTop= "false"
          -->
    

MainActivity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package  com.example.listview;
 
import  android.app.Activity;
import  android.os.Bundle;
import  android.view.Menu;
import  android.view.MenuItem;
import  android.view.View;
import  android.widget.AdapterView;
import  android.widget.TextView;
import  android.widget.AdapterView.OnItemClickListener;
import  android.widget.ListView;
import  android.widget.Toast;
 
public  class  MainActivity  extends  Activity  implements  OnItemClickListener{
     private  ListView listView;
     @Override
     protected  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         listView=(ListView) findViewById(R.id.ListView1_1);
         listView.setOnItemClickListener( this );
     }
     //listView列表项的单击事件
     @Override
     public  void  onItemClick(AdapterView<?> parent, View view,  int  position,
             long  id) {
         // TODO Auto-generated method stub
         TextView textView=(TextView)view;
         System.out.println(parent.getClass());
         System.out.println( "position:" +position);
         System.out.println( "view:" +view);
         System.out.println(textView.getText().toString());
         Toast.makeText(MainActivity. this , textView.getText().toString(), Toast.LENGTH_SHORT).show();
         
     }
}