ITeye博客

Android使用ListView构造复杂页面,并响应点击事件,通过intent跳转代码(转)

云端的Android · 2012-01-03 00:59 · Android点滴 · 移动开发 · Markdown

基础知识及注意事项:
1、在需要使用Intent跳转到其他layout的时候,需要在AndroidManifest.xml文件中增加新的layout.
比如本文需要

Java代码  收藏代码

  1. <activity android:name="SongActivity"></activity>

 

2、需要附加创建一个xml文件,作为listview的子项。

Xml代码  收藏代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. android:id="@+id/RelativeLayout01"
  4. android:layout_width="fill_parent"
  5. xmlns:android="http://schemas.android.com/apk/res/android"
  6. android:layout_height="wrap_content"
  7. android:paddingBottom="4dip"
  8. android:paddingLeft="12dip">
  9. <ImageView
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:id="@+id/ItemImage">
  13. </ImageView>
  14. <TextView
  15. android:layout_height="wrap_content"
  16. android:layout_width="fill_parent"
  17. android:id="@+id/ItemTitle"
  18. android:layout_toRightOf="@+id/ItemImage"
  19. android:textSize="30dip"
  20. ></TextView>
  21. <TextView
  22. android:layout_height="wrap_content"
  23. android:layout_width="fill_parent"
  24. android:layout_toRightOf="@+id/ItemImage"
  25. android:layout_below="@+id/ItemTitle"
  26. android:id="@+id/ItemText"
  27. ></TextView>
  28. </RelativeLayout>

 

Java代码  收藏代码

  1. package hzy.webkit;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import android.app.Activity;
  6. import android.app.ListActivity;
  7. import android.content.Intent;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.AdapterView;
  11. import android.widget.ListView;
  12. import android.widget.SimpleAdapter;
  13. import android.widget.Toast;
  14. public class WebkitActivity extends Activity  implements android.widget.AdapterView.OnItemClickListener   {
  15. ArrayList<HashMap<String, Object>> mylist = null;
  16. @Override
  17. public void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.main);
  20. mylist = new ArrayList<HashMap<String, Object>>();
  21. for(int i=0;i<15;i++)
  22. {
  23. HashMap<String, Object> map = new HashMap<String, Object>();
  24. map.put("icon", R.drawable.wlh);
  25. map.put("ItemTitle", "王力宏");
  26. map.put("ItemText", "春天的故事");
  27. mylist.add(map);
  28. }
  29. ListView lv = (ListView)this.findViewById(R.id.listView1);
  30. lv.setAdapter(initAdapter());
  31. lv.setOnItemClickListener(this);
  32. }
  33. @Override
  34. public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
  35. Intent intent = new Intent(getApplicationContext(), SongActivity.class);
  36. WebkitActivity.this.startActivity(intent);
  37. }
  38. public SimpleAdapter initAdapter(){
  39. SimpleAdapter mSchedule = new SimpleAdapter(this,mylist,//数据来源
  40. R.layout.single,
  41. new String[] {"icon","ItemTitle", "ItemText"},
  42. new int[] {R.id.ItemImage,R.id.ItemTitle,R.id.ItemText});
  43. return mSchedule;
  44. }
  45. }
  46. //Method 1
  47. //初始界面
  48. //获取web json或者 xml数据
  49. //代码生成html
  50. //webview加载该html数据显示
  51. //Method 2
  52. //初始界面
  53. //获取web json或者xml数据
  54. //原生listview 构造数据显示

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。