揚州旅遊app(五)

轉載請寫明出處,謝謝


這兩天動搖軍心,不想搞android了,弄了javaweb,後來想想算了,還是老實弄安卓吧。廢話不說,開始今天內容。

今天我們來完成揚州美景這個模塊的內容。

其實步驟和揚州美食模塊基本一樣。咱們可以複用揚州美食模塊的代碼。

先上圖


主要界面有一個ListView來完成。

貼代碼

<?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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="揚州美景界面"
        android:textColor="#ff0000"
        android:textSize="22sp" />

    <ListView
        android:id="@+id/lv_meijing"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</LinearLayout>


然後去實現java部分的邏輯代碼

首先去找到listview

private ListView lv_meijing;
lv_meijing = (ListView) findViewById(R.id.lv_meijing);


然後設置它的適配器

lv_meijing.setAdapter(adapter);

我覺得這裏可以用simpleAdapter,但是想了想,還是自定義adapter比較方便。

	private class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return names.length;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			View view = View.inflate(YangzhoumeijingActivity.this,
					R.layout.list_item_meijing, null);
			TextView tv_item = (TextView) view.findViewById(R.id.tv_item);
			ImageView iv_item = (ImageView) view.findViewById(R.id.iv_item);
			tv_item.setText(names[arg0]);
			iv_item.setImageResource(ids[arg0]);
			return view;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

	}

getView()方法裏,我們把xml文件轉化爲一個view,對應的每一個條目的佈局文件代碼爲

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >


    <ImageView
        android:id="@+id/iv_item"
        android:layout_width="60dip"
        android:layout_height="60dip"
        android:src="@drawable/app" />


    <TextView
        android:id="@+id/tv_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="揚州美景"
        android:textColor="#000000"
        android:textSize="25sp" />

</LinearLayout>


數據數組names和ids分別爲

	private static String[] names = { "揚州八怪紀念館", "大明寺", "東關古渡", "鳳凰島", "個園",
			"何園", "京杭大運河", "盧氏鹽商住宅", "史可法紀念館", "瘦西湖", "雙博館", "文昌閣", "茱萸灣",
			"朱自清故居" };
	private static int[] ids = { R.drawable.meijing_baguaijinianguan,
			R.drawable.meijing_damingsi, R.drawable.meijing_dongguangudu,
			R.drawable.meijing_fenghuangdao, R.drawable.meijing_geyuan,
			R.drawable.meijing_heyuan, R.drawable.meijing_jinghangdayunhe,
			R.drawable.meijing_lushiyanshangzhuzhai,
			R.drawable.meijing_shikefa, R.drawable.meijing_shouxihu,
			R.drawable.meijing_shuangboguan, R.drawable.meijing_wenchangge,
			R.drawable.meijing_zhuyuwan, R.drawable.meijing_zhuziqing };


然後去實例化這個適配器adapter

private MyAdapter adapter;
adapter = new MyAdapter();


到這次,我們相當於複習了一邊listview的用法,揚州美景模塊的頁面也完成了。

接下來,我們設置每一個條目的點擊事件。

因爲每一個條目完成方法雷同,所以,我們只選取其中一個作爲演示。

		lv_meijing.setOnItemClickListener(new OnItemClickListener() {
			Intent intent;

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				switch (arg2) {
				// 揚州八怪紀念館
				case 0:
					intent = new Intent(YangzhoumeijingActivity.this,
							YangZhouBaGuaiJiNianGuan.class);
					startActivity(intent);
					break;

				default:
					break;
				}

			}
		});


通過意圖,我們來到揚州八怪紀念館頁面

其中YangZhouBaGuaiJiNianGuan.class代碼如下

public class YangZhouBaGuaiJiNianGuan extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_meijing_yangzhoubaguai);
	}
}

他的佈局爲

<?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:gravity="center_horizontal"
    android:orientation="vertical" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:src="@drawable/meijing_baguaijinianguan" />

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <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:text="簡介"
                android:textColor="#ff9933"
                android:textSize="22sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="“揚州八怪”是清代活躍在揚州畫壇上的一批具有創新精神的畫家。包括:鄭燮、羅聘、黃慎、李方膺、高翔、金農、李鱓、汪士慎八位畫家。
很多大名鼎鼎的後世畫家如吳讓之、昌碩、任伯年、齊白石、徐悲鴻、潘天壽等諸多畫家都在某些方面受到影響並自立門戶。
八怪紀念館由西方寺改建,八怪的代表人物金農晚年即生活於寺中,以其故居爲館,館內有”八怪“書畫及揚州書畫家代表作,現存明代的楠木大殿和千年古樹。"
                android:textSize="22sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="地址"
                android:textColor="#ff9933"
                android:textSize="22sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="揚州市廣陵區駝鈴巷18號"
                android:textSize="22sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="電話"
                android:textColor="#ff9933"
                android:textSize="22sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0514-87340275;0514-87337408"
                android:textSize="22sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="交通"
                android:textColor="#ff9933"
                android:textSize="22sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="103、107、12、17、1、216、26、27、30、33、40、66、7路專石塔寺站"
                android:textSize="22sp" />
        </LinearLayout>
    </ScrollView>

</LinearLayout>

上圖



這裏需要注意的一點是ScrollView的用法問題,它內部只能有一個子對象,而我們要寫好多個子對象,所以,我們把子對象都封裝到一個LinearLayout裏面,這樣就避免了使用ScrollView出錯問題。

到此,今天的內容就結束啦。









發佈了26 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章