這麼一想就有動力來更新了
之前講到在Spinner裡面設置了public成員 setTitle 和 setItems
那麼其實畫面的設置已經完成一半了
剩下的主要功能就是
展開下拉式選單 - show()
和收起下拉式選單 - hide()
再來是提供聽取Lisenter的方法 - setListener()
這些做完後, 就剩下拉式選單的畫面設置
還有Spinner元件本身使用的方式而已了
簡單吧 = =+
首先 show(), hide(), setListener()全都放在Spinner裡
都是他的成員
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
{
LayoutInflater inflater = mActivity.getLayoutInflater();
row = inflater.inflate(mLayoutResource, parent, false);
}
String desList = list[position];
updateListBackground(row, position);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(400, LayoutParams.MATCH_PARENT);
rlp.setMargins(10, 0, 0, 0);
TextView name = (TextView) row.findViewById(mHolderViewId);
name.setLayoutParams(rlp);
name.setText(desList);
name.setTag(desList);
updateListTriggerByType(row, this.mSpinnerType, desList);
return row;
}
/**
* show drop-down list.
* */
public void show(View parent)
{
mAttachedParent = (ViewGroup) parent;
Animation animax = new AlphaAnimation(0.0f, 0.1f);
animax.setDuration(200);
mSpinnerList.setAnimation(animax);
mAttachedParent.addView(mSpinnerList);
animax.start();
mIsExpanding = true;
}
/**
* Show your drop-down list at pointed position with pointed width.
* @param parent Parent view of your spinner.
* @param left The left position of spinner's left upper corner.
* @param top The top position of spinner's left upper corner.
* @param width Spinner's list width.
* */
public void showAtPosition(View parent, int left, int top, int width)
{
LayoutParams params = new LayoutParams(width, LayoutParams.MATCH_PARENT);
params.setMargins(left, top, 0, 0);
mSpinnerList.setLayoutParams(params);
show(parent);
}
/**
* hide drop-down list
* */
public void hide()
{
Animation animax = new AlphaAnimation(1.0f, 0.0f);
animax.setDuration(200);
mSpinnerList.startAnimation(animax);
mAttachedParent.removeView(mSpinnerList);
mIsExpanding = false;
mSpinnerListener.onCloseSpinner(this);
}
我因為需要設置好作為下拉式選單的ListView的位置
因此增加了一個成員 showAtPosition()
透過 LayoutParams.setLayoutParams(params); 去做位置的調整
再來, 下拉式選單"滑出"的效果則透過動畫Animation來實現
基本上我試過, "移動"跟"淡入/淡出"的效果, 做起來都不錯
另外我設置了一個private boolean variable: mIsExpanding
用來存選單開啟與否的狀態
再來是Listener
Listener的好處應該不用多說了
我用 Interface 來實現, 放在Spinner下
其中只設置了兩個方法, 都是用來確認Spinner的狀態
名稱都跟Android本身提供的Listener幾乎一樣, 所以應該很好懂
public interface ListSpinnerListener
{
/** Will be called after the spinner hiding its drop-down list.*/
public void onCloseSpinner(ListSpinner spinner);
/** Will be called if user click the spinner's row item. Current supported with spinner type STRING_SPINNER. */
public void onItemClicked(View holder);
}
然後為Spinner新增 setListener() 這個方法讓外部使用者透過他就能設好該Spinner的Listener
/**
* ListSpinner provide listener to check the action movement of listened spinner.
* */
public void setListener(ListSpinnerListener listener)
{
mSpinnerListener = listener;
}
最後, 簡單看一下 layout到底長什麼樣子上一篇提過 layout 只需要兩個就好, 一個是 title 用, 一個 item用
title長這樣
item長這樣
因為item每行的結構都一樣, 當然也是寫一行就好, 其他都交給程式來做
擴充型的item長這樣
所以現在回去看就能知道, 需要什麼樣的title與item
只要畫好單一的layout
再使用 setItem, setTitle, 一切就都沒問題了!
好
時間不夠了, 剩下的放第三篇吧
沒有留言:
張貼留言