自定义布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
activity_main.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="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/lv1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
package com.example.administrator.app_adapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化ListView
ListView lv1 = findViewById(R.id.lv1);
//创建简单适配器;参数1:上下文;参数2:数据源(特定泛型的集合数据源);参数3:自定义的列表项布局文件;
//参数4:记录Map(数据源)中的键名;参数5:绑定(自定义布局)视图中的ID
final SimpleAdapter simpleAdapter = new SimpleAdapter(this,getdata(),
R.layout.item_layout,new String[]{"image", "text"}, new int[]{R.id.image, R.id.text});
//为视图绑定适配器
lv1.setAdapter(simpleAdapter);
//为每项添加点击事件
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//点击事件
/*parent是指当前的listview;
*view是当前listview中的item的view的布局,就是可用这个view获取里面控件id后操作控件
* position是当前item在listview中适配器的位置
* id是当前item在listview里第几行的位置
*/
//获得选中项中的HashMap对象
HashMap<String,String> map=(HashMap<String,String>)parent.getItemAtPosition(position);
String Text=map.get("text");
Toast.makeText(MainActivity.this,Text,Toast.LENGTH_SHORT).show();
}
});
}
//创建数据源
private List<Map<String, Object>> getdata() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<>();
map.put("image", R.mipmap.ic_launcher);
map.put("text", "数据1");
list.add(map);
map = new HashMap<>();
map.put("image", R.mipmap.ic_launcher);
map.put("text", "数据2");
list.add(map);
map = new HashMap<>();
map.put("image", R.mipmap.ic_launcher);
map.put("text", "数据3");
list.add(map);
map = new HashMap<>();
map.put("image", R.mipmap.ic_launcher);
map.put("text", "数据4");
list.add(map);
map = new HashMap<>();
map.put("image", R.mipmap.ic_launcher);
map.put("text", "数据5");
list.add(map);
map = new HashMap<>();
map.put("image", R.mipmap.ic_launcher);
map.put("text", "数据6");
list.add(map);
map = new HashMap<>();
map.put("image", R.mipmap.ic_launcher);
map.put("text", "数据7");
list.add(map);
map = new HashMap<>();
map.put("image", R.mipmap.ic_launcher);
map.put("text", "数据8");
list.add(map);
map = new HashMap<>();
map.put("image", R.mipmap.ic_launcher);
map.put("text", "数据9");
list.add(map);
map = new HashMap<>();
map.put("image", R.mipmap.ic_launcher);
map.put("text", "数据10");
list.add(map);
return list;
}
}