1. 程式人生 > >從零開始寫一個小米便籤

從零開始寫一個小米便籤

2015.10.21(第五天)

目標:採用ListView實現列表

前言:整個實現的過程是檢視+控制器+資料填充

檢視通:過ListView控制元件來實現,以列表的形式顯示資料。

控制器:通過Adapter來實現,一共有四種:ArrayAdapter、SimpleAdapter、自定義Adapter、SimpleCursorAdapter。把資料來源中的資料以某種樣式(xml檔案)顯示在檢視中。

資料:1.宣告檢視物件,根據id找到此物件;2宣告控制器物件,根據構造方法例項化此物件;3.繫結Adapter物件到Adapter上。

第一步:在xml中新增ListView控制元件。

    <ListView
        android:id="@+id/list_home"
        android:layout_below="@id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_content"
 >
    </ListView>
這樣就為ListView控制元件指定了一個id,然後將寬度和高度都設定為match_parent,這樣ListView也就佔據了整個佈局的空間。其中,
        android:layout_below="@id/relativeLayout1"
這一句,可以將ListView的佈局放置在標題layout的下面。

第二步:新建一個數據類來儲存資料

新建Note.class

package com.hust.xiaomi;

public class Note {
	String title;
	String content;
	
	public Note(String title, String content) {
		// TODO Auto-generated constructor stub
		this.title = title;
		this.content= content;
	}
}


第三步:利用BaseAdapter製作有自己佈局的ListView

	public class ListViewAdapter extends BaseAdapter {
		List<Note> mlistInfo;//新建一個List變數,型別為Note,變數名為mlistInfo

		public ListViewAdapter(List<Note> mlistInfo) {
			this.mlistInfo = mlistInfo;
		}

		@Override
		public int getCount() {//返回包含的Item的總個數
			// TODO Auto-generated method stub
			return mlistInfo.size();
		}

		@Override
		public Object getItem(int position) {//獲得相應資料集合中特定位置的資料項
			// TODO Auto-generated method stub
			return mlistInfo.get(position);
		}

		@Override
		public long getItemId(int position) {//返回該position對應item的id adapterview也有類似方法:
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {//返回了每個item項所顯示的view
			// TODO Auto-generated method stub
			if (convertView == null) {
				LayoutInflater inflater = (LayoutInflater) MainActivity.this
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(R.layout.list_item_detail, null);
			}
			Note note = mlistInfo.get(position);
			TextView title = (TextView) convertView.findViewById(R.id.title);
			title.setText(note.title);
			TextView text = (TextView) convertView.findViewById(R.id.info);
			text.setText(note.content);
			return convertView;
		}
	}

重寫Baseadapter時,我們需要重寫四個方法:getCount,getItem(int position),getItemId(int position),getView方法。

getCount:決定了listview一共有多少個item。

getView:返回了每個item項所顯示的view。

getItem:getItem方法不是在Baseadapter類中被呼叫的,而是在Adapterview中被呼叫的。

結果:


調整佈局後:


2015.10.22-2015.10.23(第六天、第七天)

目標:實現第二介面的選擇背景功能。

前言:點選圖片,利用onClick函式來響應點選圖片的過程。

第一步:新建第二個介面的佈局檔案activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/secondlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/widget_big_green"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/five"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/image3"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="29dp"
        android:scaleType="fitCenter"
        android:src="@drawable/zhengwen_xuanyanse" />

    <RelativeLayout
        android:id="@+id/relativeLayout11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/five"
        android:layout_alignTop="@id/five"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/five1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitCenter"
            android:src="@drawable/zhengwen_xuanyanse_dangqian" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout22"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/five"
        android:layout_toRightOf="@+id/relativeLayout11"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/five2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:scaleType="fitCenter"
            android:src="@drawable/zhengwen_xuanyanse_dangqian" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout33"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/five"
        android:layout_toRightOf="@+id/relativeLayout22"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/five3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:scaleType="fitCenter"
            android:src="@drawable/zhengwen_xuanyanse_dangqian" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout44"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/five"
        android:layout_toRightOf="@+id/relativeLayout33"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/five4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitCenter"
            android:src="@drawable/zhengwen_xuanyanse_dangqian" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout55"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/five"
        android:layout_toRightOf="@+id/relativeLayout44"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/five5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitCenter"
            android:src="@drawable/zhengwen_xuanyanse_dangqian" />
    </RelativeLayout>

    <ImageView
        android:id="@+id/image3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="18dp"
        android:layout_toRightOf="@+id/relativeLayout55"
        android:scaleType="fitCenter"
        android:src="@drawable/notes_btn_changecolors" />

</RelativeLayout>
第二步:在Main_activity.xml新增跳轉程式碼
	class MyClickListener implements View.OnClickListener {
		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(MainActivity.this, SecondActivity.class);
			startActivity(intent);
		}
	}
第三步:實現第二個頁面的邏輯
package com.hust.xiaomi;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.xiaomi.R;

public class SecondActivity extends Activity {

	private ImageView select = null;
	private ImageView five = null;
	private ImageView five1 = null;
	private ImageView five2 = null;
	private ImageView five3 = null;
	private ImageView five4 = null;
	private ImageView five5 = null;

	private RelativeLayout layout = null;

	private RelativeLayout relativeLayout1 = null;
	private RelativeLayout relativeLayout2 = null;
	private RelativeLayout relativeLayout3 = null;
	private RelativeLayout relativeLayout4 = null;
	private RelativeLayout relativeLayout5 = null;

	private boolean isVisible = false;
	private boolean isVisible1 = true;
	private boolean isVisible2 = true;
	private boolean isVisible3 = true;
	private boolean isVisible4 = true;
	private boolean isVisible5 = true;

	private int imageidsSecond[] = { R.drawable.widget_big_yellow,
			R.drawable.widget_big_red, R.drawable.widget_big_blue,
			R.drawable.widget_big_green, R.drawable.widget_big_gray };

	private int flag = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);

		select = (ImageView) findViewById(R.id.image3);
		layout = (RelativeLayout) findViewById(R.id.secondlayout);

		five = (ImageView) findViewById(R.id.five);
		five1 = (ImageView) findViewById(R.id.five1);
		five2 = (ImageView) findViewById(R.id.five2);
		five3 = (ImageView) findViewById(R.id.five3);
		five4 = (ImageView) findViewById(R.id.five4);
		five5 = (ImageView) findViewById(R.id.five5);
		relativeLayout1 = (RelativeLayout) findViewById(R.id.relativeLayout11);
		relativeLayout2 = (RelativeLayout) findViewById(R.id.relativeLayout22);
		relativeLayout3 = (RelativeLayout) findViewById(R.id.relativeLayout33);
		relativeLayout4 = (RelativeLayout) findViewById(R.id.relativeLayout44);
		relativeLayout5 = (RelativeLayout) findViewById(R.id.relativeLayout55);
		five1.setVisibility(View.INVISIBLE);
		five2.setVisibility(View.INVISIBLE);
		five3.setVisibility(View.INVISIBLE);
		five4.setVisibility(View.INVISIBLE);
		five5.setVisibility(View.INVISIBLE);
		five.setVisibility(View.INVISIBLE);
		select.setOnClickListener(new MyClickListener());
		relativeLayout1.setOnClickListener(new MyClickListener1());
		relativeLayout2.setOnClickListener(new MyClickListener2());
		relativeLayout3.setOnClickListener(new MyClickListener3());
		relativeLayout4.setOnClickListener(new MyClickListener4());
		relativeLayout5.setOnClickListener(new MyClickListener5());

	}

	class MyClickListener implements View.OnClickListener {
		@Override
		public void onClick(View v) {
			if (isVisible) {
				five.setVisibility(View.INVISIBLE);
				five1.setVisibility(View.INVISIBLE);
				five2.setVisibility(View.INVISIBLE);
				five3.setVisibility(View.INVISIBLE);
				five4.setVisibility(View.INVISIBLE);
				five5.setVisibility(View.INVISIBLE);

				isVisible = false;
			} else {
				five.setVisibility(View.VISIBLE);
				switch (flag) {
				case 1:
					five1.setVisibility(View.VISIBLE);
					break;
				case 2:
					five2.setVisibility(View.VISIBLE);
					break;
				case 3:
					five3.setVisibility(View.VISIBLE);
					break;
				case 4:
					five4.setVisibility(View.VISIBLE);
					break;
				case 5:
					five5.setVisibility(View.VISIBLE);
					break;
				}
				isVisible = true;
			}
		}
	}

	class MyClickListener1 implements View.OnClickListener {
		@Override
		public void onClick(View v) {
			flag = 1;
			layout.setBackgroundResource(imageidsSecond[flag - 1]);
			five.setVisibility(View.INVISIBLE);
			five1.setVisibility(View.INVISIBLE);
			five2.setVisibility(View.INVISIBLE);
			five3.setVisibility(View.INVISIBLE);
			five4.setVisibility(View.INVISIBLE);
			five5.setVisibility(View.INVISIBLE);
			isVisible = false;
			if (isVisible) {
				if (isVisible1) {
					five1.setVisibility(View.INVISIBLE);

					isVisible1 = false;
				} else {
					// five1.setVisibility(View.VISIBLE);

					isVisible1 = true;
				}
			}
		}
	}

	class MyClickListener2 implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			flag = 2;
			layout.setBackgroundResource(imageidsSecond[flag - 1]);
			five.setVisibility(View.INVISIBLE);
			five1.setVisibility(View.INVISIBLE);
			five2.setVisibility(View.INVISIBLE);
			five3.setVisibility(View.INVISIBLE);
			five4.setVisibility(View.INVISIBLE);
			five5.setVisibility(View.INVISIBLE);
			isVisible = false;
			if (isVisible) {
				if (isVisible2) {
					five2.setVisibility(View.INVISIBLE);
					isVisible2 = false;
				} else {
					// five2.setVisibility(View.VISIBLE);

					isVisible2 = true;
				}
			}
		}
	}

	class MyClickListener3 implements View.OnClickListener {
		@Override
		public void onClick(View v) {
			flag = 3;
			layout.setBackgroundResource(imageidsSecond[flag - 1]);
			five.setVisibility(View.INVISIBLE);
			five1.setVisibility(View.INVISIBLE);
			five2.setVisibility(View.INVISIBLE);
			five3.setVisibility(View.INVISIBLE);
			five4.setVisibility(View.INVISIBLE);
			five5.setVisibility(View.INVISIBLE);
			isVisible = false;
			if (isVisible) {
				if (isVisible3) {
					five3.setVisibility(View.INVISIBLE);
					isVisible3 = false;
				} else {
					// five3.setVisibility(View.VISIBLE);

					isVisible3 = true;
				}
			}
		}
	}

	class MyClickListener4 implements View.OnClickListener {
		@Override
		public void onClick(View v) {
			flag = 4;
			layout.setBackgroundResource(imageidsSecond[flag - 1]);
			five.setVisibility(View.INVISIBLE);
			five1.setVisibility(View.INVISIBLE);
			five2.setVisibility(View.INVISIBLE);
			five3.setVisibility(View.INVISIBLE);
			five4.setVisibility(View.INVISIBLE);
			five5.setVisibility(View.INVISIBLE);
			isVisible = false;
			if (isVisible) {
				if (isVisible4) {
					five4.setVisibility(View.INVISIBLE);
					isVisible4 = false;
				} else {
					// five4.setVisibility(View.VISIBLE);

					isVisible4 = true;
				}
			}
		}
	}

	class MyClickListener5 implements View.OnClickListener {
		@Override
		public void onClick(View v) {
			flag = 5;
			layout.setBackgroundResource(imageidsSecond[flag - 1]);
			five.setVisibility(View.INVISIBLE);
			five1.setVisibility(View.INVISIBLE);
			five2.setVisibility(View.INVISIBLE);
			five3.setVisibility(View.INVISIBLE);
			five4.setVisibility(View.INVISIBLE);
			five5.setVisibility(View.INVISIBLE);
			isVisible = false;
			if (isVisible) {
				if (isVisible5) {
					five5.setVisibility(View.INVISIBLE);
					isVisible5 = false;
				} else {
					// five5.setVisibility(View.VISIBLE);

					isVisible5 = true;
				}
			}
		}
	}

}