直接使用listView进行创建;
让Activity继承ListActivity(该Activity显示的组件为ListView);
————————
为listView设置现显示的列表项
如何让ListView显示出AdapterView的特性 通过setAdapter方法来进行AdapterView,并由Adapter提供列表项的每个组件,
AdapterView负责采用合适的方式来显示这些列表项
创建ArrayAdapter基本的参数
//本地Context对象
// 资源ID,作为列表项组件作为每个列表项的组件,
// 数组或List为多个列表项提供数据,包含多少的元素,就生成多少的列表项
MainActivity
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView list1 = (ListView) findViewById(R.id.list1); //定义一个数组 String[] arr1 = {"北京", "南京", "南通"}; //将数组包装成ArrayAdapter ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, R.layout.array_items, arr1); //为ListView设置Adapter list1.setAdapter(adapter1); ListView list2 = (ListView) findViewById(R.id.list2); String[] arr2 = {"唐僧", "孙悟空", "猪八戒", "牛魔王"}; ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, R.layout.checked_item, arr2); //本地Context对象 // 资源ID,作为列表项组件作为每个列表项的组件, // 数组或List为多个列表项提供数据,包含多少的元素,就生成多少的列表项 list2.setAdapter(adapter2); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical"> <!--直接使用数组资源给出列表--> <!----> <ListView android:id="@+id/list1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:divider="#f00" android:dividerHeight="10px" android:headerDividersEnabled="true"></ListView> <ListView android:id="@+id/list2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:divider="#f00" android:dividerHeight="2px" android:headerDividersEnabled="false"> </ListView> </LinearLayout>
array_item.xml
<?xml version="1.0" encoding="utf-8"?><!--设置--> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/TextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10px" android:shadowColor="#f0f" android:shadowDx="4" android:shadowDy="4" android:shadowRadius="2" android:textSize="24dp"> </TextView>
checked_item页面
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> </TextView>
基本的参数
//集合对象,对对象生成一个列表项 //一个界面布局的ID //是一个String[]类型的参数,提取对象生成列表项 //是一个int[]类型的参数,该参数决定填充哪些组件
public class MainActivity extends AppCompatActivity { private String[] names = new String[]{"酸奶店", "老板娘", "周太阳"}; private String[] descs = new String[]{"梦想", "梦想", "18岁"}; private int[] imagsIDs = new int[]{R.drawable.adele, R.drawable.adele2, R.drawable.adele3}; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //创建一个list集合,集合的元素为mao List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>(); for (int i = 0; i < names.length; i++) { Map<String, Object> listItem = new HashMap<String, Object>(); listItem.put("header", imagsIDs[i]); listItem.put("personName", names[i]); listItem.put("desc", descs[i]); listItems.add(listItem); } //创建一个SimpleAdapter SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.simple_item, new String[]{"personName","header","desc"}, new int[]{R.id.name, R.id.header, R.id.desc}); //集合对象,对对象生成一个列表项 //一个界面布局的ID //是一个String[]类型的参数,提取对象生成列表项 //是一个int[]类型的参数,该参数决定填充哪些组件 ListView list = (ListView) findViewById(R.id.mylist); //为ListView设置Adapter list.setAdapter(simpleAdapter); //为ListView的列表项的点击时间监听器 list.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { System.out.println(names[position]+"被点击了"); } }); //为ListView的列表项的选中事件绑定监听器 list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { System.out.println(names[position] + "被选中了"); } public void onNothingSelected(AdapterView<?> parent) { } }); } }
activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ListView android:id="@+id/mylist" android:layout_width="match_parent" android:layout_height="wrap_content"></ListView> </LinearLayout>
Simple_item.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="horizontal"> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:id="@+id/header" android:paddingLeft="10dp"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/name" android:textSize="20dp" android:textColor="#f0f" android:paddingLeft="10dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/desc" android:paddingLeft="10dp" android:textSize="14dp" /> </LinearLayout> </LinearLayout>
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView myList = (ListView) findViewById(R.id.myList); BaseAdapter adapter =new BaseAdapter() { public int getCount() { //该方法的返回值将会控制该Adapter将会包含多少个列表项 //指定一共包含40个选项 return 40; } //返回值决定第postition处的列表项的内容 public Object getItem(int position) { return null; } //重写该方法,该方法的返回值将作为列表项的iD public long getItemId(int position) { return position; } //重写该方法,该方法返回的view将作为列表项(第position处的列表项组件) public View getView(int position, View convertView, ViewGroup parent) { //创建一个LinearLayout,并向其中添加两个组件 LinearLayout line = new LinearLayout(MainActivity.this); line.setOrientation(0); ImageView image = new ImageView(MainActivity.this); image.setImageResource(R.drawable.smallfresh); TextView text = new TextView(MainActivity.this); text.setText("第"+(position + 1)+"列表项"); text.setTextSize(20); text.setTextColor(Color.RED); line.addView(image); line.addView(text); //返回LineLayout实例 return line; } }; myList.setAdapter(adapter); } }