0
回答
Android: 带图标的ListView实现
华为云数据库免费试用   

ListView的SimpleAdapter有很好的扩展性,可以定义各种不同的布局,可以放上图片、Button、CheckBox等等,本文实现一个带有前后图标的列表视图:

 

列表中item的组成:

(R.id.img_pre)(        R.id.text             )(R.id.img)

 

首先定义用来显示列表中item的layout文件list_item2.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    
    >
<ImageView 
android:id="@+id/img_pre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
/>     
<TextView 
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:textStyle="bold|italic"
android:layout_weight="1"
/>   
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:layout_weight="2"
>
<ImageView 
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:layout_gravity="center_vertical"
/>  
</LinearLayout> 
</LinearLayout>
 

 

整个一行作为一个LinearLayout,

第一个图片作为一个IamgeView,文本为TextView,第二个图片为ImageView,

第二个ImageView再放到一个LinearLayout里面,并

通过android:gravity及android:layout_weight来调整显示位置

 

下面是实现代码:

public class HelloListView extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
        //setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, COUNTRIES));
        setListAdapter(new SimpleAdapter(this, getData(), R.layout.list_item2, 
        								new String[]{"img_pre", "text", "img"}, 
        								new int[]{R.id.img_pre, R.id.text, R.id.img}));
        ListView lv = getListView();
        //lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int pos,
					long id) {
				Map<String, Object> item = (Map<String, Object>)parent.getItemAtPosition(pos);
				Toast.makeText(getApplicationContext(), (String)item.get("text"),
						Toast.LENGTH_SHORT).show();
				
			}        	
        });
    }
    private List<Map<String, Object>> getData() {
    	List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();    	
    	
    	for(int i = 0; i < COUNTRIES.length; i++) {
    		Map<String, Object> map = new HashMap<String, Object>();
    		map.put("text", COUNTRIES[i]);
        	map.put("img", R.drawable.img);
        	map.put("img_pre", R.drawable.img_pre);        	
        	list.add(map);
    	}
    	
    	return list;
    }
    static final String[] COUNTRIES = new String[] {
        "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
        "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
        "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
        "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium",
        "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia",
        "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory",
        "British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi",
        "Cote d'Ivoire", "Cambodia", "Cameroon", "Canada", "Cape Verde",
        "Cayman Islands", "Central African Republic", "Chad", "Chile", "China",
        "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo",
        "Cook Islands", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czech Republic",
        "Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", "Dominican Republic",
        "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea",
        "Estonia", "Ethiopia", "Faeroe Islands", "Falkland Islands", "Fiji", "Finland",
        "Former Yugoslav Republic of Macedonia", "France", "French Guiana", "French Polynesia",
        "French Southern Territories", "Gabon", "Georgia", "Germany", "Ghana", "Gibraltar",
        "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau",
        "Guyana", "Haiti", "Heard Island and McDonald Islands", "Honduras", "Hong Kong", "Hungary",
        "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica",
        "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Laos",
        "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg",
        "Macau", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands",
        "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia", "Moldova",
        "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia",
        "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand",
        "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "North Korea", "Northern Marianas",
        "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru",
        "Philippines", "Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar",
        "Reunion", "Romania", "Russia", "Rwanda", "Sqo Tome and Principe", "Saint Helena",
        "Saint Kitts and Nevis", "Saint Lucia", "Saint Pierre and Miquelon",
        "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Saudi Arabia", "Senegal",
        "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands",
        "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "South Korea",
        "Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden",
        "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "The Bahamas",
        "The Gambia", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey",
        "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Virgin Islands", "Uganda",
        "Ukraine", "United Arab Emirates", "United Kingdom",
        "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan",
        "Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Wallis and Futuna", "Western Sahara",
        "Yemen", "Yugoslavia", "Zambia", "Zimbabwe"
      };
}
 

 

采用SimpleAdapter适配器,并添加了Click监听,Click的时候显示Toast提示信息

 

    	for(int i = 0; i < COUNTRIES.length; i++) {
    	 Map<String, Object> map = new HashMap<String, Object>();
    	 map.put("text", COUNTRIES[i]);
        	map.put("img", R.drawable.img);
        	map.put("img_pre", R.drawable.img_pre);        
        	list.add(map);
    	}
 

用来生成item数据

 

res/drawable 目录中放入两个img文件

 


原文链接:http://blog.csdn.net/zmyde2010/article/details/5929555
<无标签>
举报
长平狐
发帖于6年前 0回/2K+阅
顶部