首页 > 代码库 > 13、Android之ListView学习·
13、Android之ListView学习·
1、XML文件中添加ListView
(1)XML布局文件
<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/listView" android:layout_width="match_parent" android:layout_height="match_parent" android:entries="@array/names" android:divider="#aa0000" android:dividerHeight="20dp" android:drawSelectorOnTop="false" android:scrollbars="horizontal|vertical" android:fastScrollEnabled="true" android:listSelector="#00ffff" > </ListView> </RelativeLayout>
(2)string.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">ListViewDemo</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</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>
(3)MainActivity
package com.example.listviewdemo; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements OnItemClickListener{ private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.listView); lv.setOnItemClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub System.out.println("Parent = " + arg0.getClass()); System.out.println("View = " + arg1); TextView tv = (TextView)arg1; Toast.makeText(this,tv.getText(), Toast.LENGTH_SHORT).show(); System.out.println("Position = " + arg2); System.out.println("ID = " + arg3); } }
2、使用使用ListActivity和ArrayAdapter
由于ListActivity本身就是一个Activity,所以只有当页面仅存在一个空间,且空间的ID是一定的。
import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class MainActivity2 extends ListActivity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); String[] names = {"张三","李四","赵武","王六","周琦","养吧","文档",}; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,names); setListAdapter(adapter); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); TextView tv = (TextView)v; Toast.makeText(this,tv.getText(), Toast.LENGTH_SHORT).show(); } }
3、使用Adapter创建ListView,实现单选和复选
(1)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/listView1" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </LinearLayout>
(2)Activity
package com.example.listviewdemo; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainActivity3 extends Activity implements OnItemClickListener{ private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main3); lv = (ListView) findViewById(R.id.listView1); String[] citys = {"北京","上海","广州","深圳","东光","中国"}; //单选模式 // ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice,citys); // lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE); //多选模式 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice,citys); lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); lv.setOnItemClickListener(this);//注册事件 lv.setAdapter(adapter); } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub System.out.println("View = " + arg1); } }
13、Android之ListView学习·
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。