Android之ListView列表视图和界面跳转实现

来源:互联网 发布:小儿常见病推拿软件 编辑:程序博客网 时间:2021/04/12 15:29

本篇文章通过一个例子实现ListView列表视图并且点击不同的项进入不同的界面


第一步,我们需要在布局文件中设置ListView属性

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:orientation="vertical" >        <View        android:layout_width="fill_parent"        android:layout_height="1dip"        android:background="?android:attr/listDivider" />        <ListView        android:id="@+id/listView"        android:layout_width="fill_parent"        android:layout_height="wrap_content" /></LinearLayout>

那么在ListView里面需要什么内容呢


第二步,添加界面

1.在string里面设置文字,其实在xml文件或者activity界面里面我们也可以直接写文字,但是如果将文字都写在string里面,统一管理文字,这对以后我们修改起来会方便很多

<?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 name="demo1_name">测试1</string>    <string name="demo1_title">测试1名称</string>    <string name="demo1_desc">测试1描述:这是一个测试例子</string>        <string name="demo2_name">测试2</string>    <string name="demo2_title">测试2名称</string>    <string name="demo2_desc">测试2描述:这是一个测试例子</string>        <string name="demo3_name">测试3</string>    <string name="demo3_title">测试3名称</string>    <string name="demo3_desc">测试3描述:这是一个测试例子</string></resources>


2.添加几个界面对应的布局文件

demo1.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"    android:background="#f00" >    </LinearLayout>

demo2.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"    android:background="#0f0" >    </LinearLayout>

demo3.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"    android:background="#00f" >    </LinearLayout>

3.添加几个跳转界面

ActivityDemo1.java

public class ActivityDemo1 extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.demo1);}}

ActivityDemo2.java

public class ActivityDemo2 extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.demo2);}}

ActivityDemo3.java

public class ActivityDemo3 extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.demo3);}}

第三步,为了在界面中每一行显示两行文字,我们需要再增加一个自定义界面

demo_info_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="vertical" >        <TextView        android:id="@+id/title"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_marginTop="3dp"        android:text="demo busLineName"        android:textSize="18sp" />        <TextView        android:id="@+id/desc"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_marginBottom="3dp"        android:text="demo desc"        android:textSize="12sp" /></LinearLayout>

第四步,好了,准备工作做完了,可以在界面上操作了

MainActivity.java

public class MainActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ListView mListView = (ListView) findViewById(R.id.listView);// 添加ListItem,设置事件响应mListView.setAdapter(new DemoListAdapter());mListView.setOnItemClickListener(new OnItemClickListener() {public void onItemClick(AdapterView<?> arg0, View v, int index,long arg3) {onListItemClick(index);}});}void onListItemClick(int index) {Intent intent = null;intent = new Intent(MainActivity.this, demos[index].demoClass);this.startActivity(intent);}private static final DemoInfo[] demos = {new DemoInfo(R.string.demo1_title,R.string.demo1_desc, ActivityDemo1.class),new DemoInfo(R.string.demo2_title,R.string.demo2_desc, ActivityDemo2.class),new DemoInfo(R.string.demo3_title,R.string.demo3_desc, ActivityDemo3.class)};private class DemoListAdapter extends BaseAdapter {public DemoListAdapter() {super();}@Overridepublic View getView(int index, View convertView, ViewGroup parent) {convertView = View.inflate(MainActivity.this,R.layout.demo_info_item, null);TextView title = (TextView) convertView.findViewById(R.id.title);TextView desc = (TextView) convertView.findViewById(R.id.desc);title.setText(demos[index].title);desc.setText(demos[index].desc);if (index >= 14) {title.setTextColor(Color.YELLOW);}return convertView;}@Overridepublic int getCount() {return demos.length;}@Overridepublic Object getItem(int index) {return demos[index];}@Overridepublic long getItemId(int id) {return id;}}private static class DemoInfo {private final int title;private final int desc;private final Class<? extends android.app.Activity> demoClass;public DemoInfo(int title, int desc,Class<? extends android.app.Activity> demoClass) {this.title = title;this.desc = desc;this.demoClass = demoClass;}}}


第五步,查看效果

主界面

点击其中每一项,出现如下效果

  


源码下载



0 0
原创粉丝点击