Android之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
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.demo1);
}
}


ActivityDemo2.java

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


ActivityDemo3.java

public class ActivityDemo3 extends Activity
{
@Override
protected 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
{

@Override
protected 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();
}
@Override
public 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;
}

@Override
public int getCount() {
return demos.length;
}

@Override
public Object getItem(int index) {
return demos[index];
}

@Override
public 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
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

若♡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值