package com.example.day0328_listview_2; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private ListView listView; private List<Map<String, Object>> data; private SimpleAdapter adapter; @Override //如果布局中的子控件可以获得焦点(Button,ImageButton,CheckBox) //整个父控件(LinearLayout)就失去了焦点 //解决方法可以在父控件中设置 android:descendantFocusability="blocksDescendants" //让子控件不要获得焦点 //beforeDescendants先于子控件获得焦点 //afterDescendants后语子控件获得焦点(默认值) //blocksDescendants阻止子控件获得焦点 // 同时在对应的子控件中设置 //android:docusable="false" protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取ListView对象 listView = (ListView) findViewById(R.id.lv); data =getData();//获取数据 adapter = new SimpleAdapter(MainActivity.this, data, R.layout.item, new String[]{"图片","姓名","年龄","按钮"}, new int[]{R.id.iv,R.id.tv1,R.id.tv2,R.id.bt}); listView.setAdapter(adapter); //设置Listview条目点击侦听 listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // 点击ListView每一项显示其内容 //view是item.xml最外层的布局 //判断view是否属于LinearLayout,是就进行强转,并获取其内容 if (view instanceof LinearLayout) { LinearLayout layout = (LinearLayout) view; View view1 =layout.getChildAt(1); //同样判断view1是否属于LinearLayout,是就进行强转,并获取其内容 if (view1 instanceof LinearLayout) { LinearLayout layout2 = (LinearLayout) view1; //获取第一个文本框的内容 TextView textView0 =(TextView) layout2.getChildAt(0); //获取第二个文本框的内容 TextView textView1 =(TextView) layout2.getChildAt(1); //Toast吐司显示内容 Toast.makeText(MainActivity.this,textView0.getText()+"\n"+ textView1.getText(), 0).show(); } } } }); } //生成数据 public List<Map<String, Object>> getData(){ List<Map<String, Object>> list = new ArrayList<Map<String,Object>>(); Map<String,Object> map = new HashMap<String, Object>(); //往map里面添加数据 map.put("图片", R.drawable.ic_launcher); map.put("姓名", "野比大雄"); map.put("年龄", 88); map.put("按钮", "更多..."); //把内容添加进list list.add(map); Map<String,Object> map2 = new HashMap<String, Object>(); //往map2里面添加数据 map2.put("图片", R.drawable.ic_launcher); map2.put("姓名", "野比先生"); map2.put("年龄", 888); map2.put("按钮", "更多..."); //把内容添加进list list.add(map2); Map<String,Object> map3 = new HashMap<String, Object>(); //往map3里面添加数据 map3.put("图片", R.drawable.ic_launcher); map3.put("姓名", "野比太太"); map3.put("年龄", 888); map3.put("按钮", "更多..."); //把内容添加进list list.add(map3); //返回数据list集合 return list; } } //布局 <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=".MainActivity" > <ListView android:id="@+id/lv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> //item布局 <?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="horizontal" android:descendantFocusability="blocksDescendants" > <!-- 如果布局中的子控件可以获得焦点(Button,ImageButton,CheckBox) 整个父控件(LinearLayout)就失去了焦点 解决方法可以在父控件中设置 android:descendantFocusability="blocksDescendants" 让子控件不要获得焦点 beforeDescendants先于子控件获得焦点 afterDescendants后语子控件获得焦点(默认值) blocksDescendants阻止子控件获得焦点 同时在对应的子控件中设置 android:docusable="false" --> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="match_parent" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv1" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv2" /> </LinearLayout> <Button android:focusable="false" android:id="@+id/bt" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>