QuickSearchBox的一點改進

來源:互聯網
上載者:User

        Google的Android源碼包含較多內建應用程式的代碼,比如QuickSearchBox,Music,Gallery3D等等,這絕對是一筆寶貴的財富,哈哈。

        最近拜讀了QuickSearchBox部分源碼,QuickSearchBox可以搜尋連絡人、音樂、資訊、應用程式等等。期間發現一個小小的問題,當我們在搜尋應用程式沒有找到匹配的結果時,程式沒有給出任何提示,這從使用者角度來說是不太合理的。於是決定花點時間改進該程式。

        搜尋網頁面如所示,選擇應用程式,

        在點擊搜尋後,QuickSearchBox將跳轉到搜尋內容對用的程式中,比如搜尋應用程式時會跳轉到ApplicationProvider中,該程式源碼在package/providers目錄裡,包含三個java檔案,ApplicationLauncher、ApplicationsAdapter、ApplicationsProvider。

        ApplicationsAdapter利用bindView將搜尋的結果顯示為自訂的列表布局。ApplicationsProvider則提供了手機中應用程式的資訊,包括程式表徵圖、名稱等,封裝了資料庫的操作以提供更方便的操作。ApplicationLauncher主要是頁面的展示和邏輯操作。

private void showSearchResults(String query) {        setTitle(query);        mCursor = Applications.search(getContentResolver(), query);        startManagingCursor(mCursor);        ApplicationsAdapter adapter = new ApplicationsAdapter(this, mCursor);        setListAdapter(adapter);    }

        showSearchResults為將搜尋的資料對應至列表中。

        再來看看xml布局代碼。

<RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:paddingLeft="4dip"    android:paddingRight="4dip"    android:layout_width="match_parent"    android:layout_height="56dip" >    <ImageView android:id="@+id/icon1"        android:layout_width="48dip"        android:layout_height="48dip"        android:scaleType="centerInside"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:layout_alignParentBottom="true" />    <TextView android:id="@+id/text1"        android:textAppearance="?android:attr/textAppearanceLarge"        android:singleLine="true"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:paddingLeft="4dip"        android:layout_centerVertical="true"        android:layout_toRightOf="@id/icon1" /></RelativeLayout>

        搜尋結果有資料展示時如, 但沒有資料展示時則除了title,頁面中間只是黑色背景,沒有任何提示。

        解決這個問題有幾種方案,第一種為直接彈一個Toast,提示顯示“未找到應用程式”,自動浮現並消失,這個辦法很簡單,代碼如下,

private void showSearchResults(String query) {        setTitle(query);        mCursor = Applications.search(getContentResolver(), query);        startManagingCursor(mCursor);        ApplicationsAdapter adapter = new ApplicationsAdapter(this, mCursor);        if(adapter.isEmpty()) {            Toast.makeText(this, R.string.application_search_not_found, Toast.LENGTH_LONG).show();        }        setListAdapter(adapter);}

        第二種方案為跳轉到一個新的Activity,提示資訊,但是從程式的架構來講,不太合理,因為ApplicationProvider為後台提供資料的程式,讓該部分程式跳轉至新Activity不符合架構結構,因此放棄。

        第三種方案,這個是我們容易忽略的方案。ApplicationLauncher繼承了ListActivity,我們可以指定自己的布局,布局中必須包含一個id為“@android:id/list”的ListView,為了讓布局在ListView無資料時顯示提示資訊,需要加一個id為“@android:id/empty”的view,此處採用TextView即可,此外要注意設定TextView為gone。具體xml代碼修改如下:

<RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:paddingLeft="4dip"    android:paddingRight="4dip"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <ListView        android:id="@android:id/list"        android:layout_width="match_parent"        android:layout_height="match_parent" />    <ImageView android:id="@+id/icon1"        android:layout_width="48dip"        android:layout_height="48dip"        android:scaleType="centerInside"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:layout_alignParentBottom="true" />    <TextView android:id="@+id/text1"        android:textAppearance="?android:attr/textAppearanceLarge"        android:singleLine="true"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:paddingLeft="4dip"        android:layout_centerVertical="true"        android:layout_toRightOf="@id/icon1" />    <TextView      android:id="@android:id/empty"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:gravity="center"      android:layout_gravity="center"      android:visibility="gone"      android:text="@string/application_search_not_found" /></RelativeLayout>

        另外在ApplicationLauncher的onCreate方法中要添加setContentView方法。

        super.onCreate(savedInstanceState);        setContentView(R.layout.application_list_item);        Intent intent = getIntent();

        將修改的工程編譯,然後將apk檔案adb push到手機上,bingo~

       當然,可能還有更多的方案,折騰無極限,歡迎大家提出,文章不當的地方歡迎指出。

        註:本文參加第二屆 Google 暑期大學生部落格分享大賽 - 2011 Android 成長篇

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.