Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android零散,零散鐘點工

Android零散,零散鐘點工

編輯:關於android開發

Android零散,零散鐘點工


2016-03-13

Android零散

ListView中嵌套GridView

要實現分組列表這樣的效果:點擊ListView中的分組名稱,即展開此分組顯示其包含的項目。
public class UnfoldGridView extends GridView {
public UnfoldGridView(Context context) {
super(context);
}

public UnfoldGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public UnfoldGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = View.MeasureSpec.makeMeasureSpec(900000, View.MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}

makeMeasureSpec中給一個很大的size,然後使用AtMost使其保持夠大即可。

setAdapter和addHeaderView、addFooterView

可以使用addHeaderView和addFooterView來為ListView添加首尾的個性視圖。兩者都可以多次調用來添加多個header和footer。

When first introduced, this method could only be called before setting the adapter with setAdapter(ListAdapter). Starting with KITKAT, this method may be called at any time. If the ListView's adapter does not extend HeaderViewListAdapter, it will be wrapped with a supporting instance of WrapperListAdapter.

所以,為了保持兼容性,無論是在API 19之前或之後,保持addHeaderView和addFooterView在setAdapter之前執行。addHeaderView如果在setAdapter之後執行,那麼在API 19之前的版本直接回報錯。而addFooterView在setAdapter之後執行的話,雖然不引起運行時錯誤——但是更迷惑的是——添加的視圖是看不到了。
ListView的getItemViewType

一個頁面中當要連續顯示多個不同的列表時,或者間隔性地顯示多種不同的View時,需要用到ListView的兩個方法:

@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
@Override
public int getViewTypeCount() {
return super.getViewTypeCount();
}

getItemViewType方法需要注意的是,其定義的ITEM_XX這樣的整數常量,其取值范圍需要在0~getViewTypeCount()-1之間,下面是getItemViewType返回值的描述:

An integer representing the type of View. Two views should share the same type if one can be converted to the other in getView. Note: Integers must be in the range 0 to getViewTypeCount - 1. IGNORE_ITEM_VIEW_TYPE can also be returned.

在區間外的viewType值,會引起運行時的indexoutofboundexception錯誤,這個是ListView自身的限制。

startActivityForResult和活動的launchMode

Activity_A啟動Activity_B後,需要Activity_B在完成操作後返回的一些數據:

//Activity_A中
private final int REQUEST_CODE_EDIT_ITEM = 2;
public void startPageBForEdit() {
Intent start = new Intent(this, EditActivity.class);
startActivityForResult(start, REQUEST_CODE_EDIT_ITEM);
}
//Activity_B中
public void setResult() {
Intent data = new Intent();
data.putExtra("itemDelete", true);
setResult(RESULT_OK, data);
}

在Activity_A中接收數據:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_EDIT_ITEM && resultCode == RESULT_OK) {
//成功返回結果了
}
}

上面是典型的代碼片段,需要注意的是:對於對應的目標Activity啟動模式(在manifest中)指定為singleTask和singleInstance的Activity,使用startActivityForResult後,當前Activity的onActivityResult會立即執行,其resultCode為RESULT_CANCEL,並且data為null.
Service的onStartCommand的返回值

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}

onStartCommand中的flags和其返回值都和Service的啟動緊密相關。Service的定位就像是沒有View的Activity,應用長期後台的情況下Service可能會暫時性被殺死(隨著進程被殺死)——之後又會再次被系統啟動。
每次調用startService來執行一些動作時,onStartCommand被執行,其intent參數代表分發過來的意圖描述數據(intnt就是一個攜帶有關“要做什麼”的信息對象)。
使用startService來啟動一個已經運行中的,正在啟動中的,重新啟動中的Service時,系統會在這些不同的Service狀態下對onStartCommand的調用產生一些差異。
//待續。。。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved