Android學習筆記(25) — 硬鍵盤+GridView選擇Item問題

來源:互聯網
上載者:User

1、這幾天要實現硬鍵盤選擇GridView中的Item來執行不同的操作,可糾結了幾天終不得解,摸索了很久也在網上找了很多資料。最後,終於有了眉目,基本實現了其功能。寫此文來總結一下。

2、首先是GridView資料的添加:

gridview_item.xml

<?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:orientation="vertical"    android:scrollbars="vertical"    android:background="@drawable/image_selector" >    <ImageView        android:id="@+id/ItemImage"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal" />    <TextView        android:id="@+id/ItemName"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textColor="#000fff"        android:textSize="18sp"        android:layout_gravity="center" /></LinearLayout>

主要代碼:

final GridView gridview = new GridView(ViewFliperFlashActivity.this);ArrayList<HashMap<String, Object>> listPage = new ArrayList<HashMap<String, Object>>();for (int i = 0; i < Constant_Src.All_page_items[GridviewIndex].length; i++) {HashMap<String, Object> mMap = new HashMap<String, Object>();mMap.put("itemImage", Constant_Src.All_page_items[GridviewIndex][i]);mMap.put("itemName",Constant_Src.All_page_items_name[GridviewIndex][i]);listPage.add(mMap);}SimpleAdapter mAdapter = new SimpleAdapter(getApplicationContext(),listPage, R.layout.gridview_item, new String[] { "itemImage","itemName" },new int[] { R.id.ItemImage, R.id.ItemName });gridview.setAdapter(mAdapter);gridview.setNumColumns(3);gridview.setPadding(40, 20, 40, 20);gridview.setGravity(Gravity.CENTER);

選中Item時的監聽器

gridview.setOnItemSelectedListener(new OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {// TODO Auto-generated method stub//執行相對應操作}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {// TODO Auto-generated method stub//執行相對應操作}});

在使用硬鍵盤操作選擇Item時,因為如果焦點從GridView上的一個Item移出,使GridView失去焦點,當焦點返回GridView時,如果第一個獲得焦點的Item是上次最後移出那個Item此時不會觸發OnItemSelectedListener中的onItemSelected事件。

基於此,網上有人提供了這種方法:查看原始碼中GridView有一個default的方法setSelection(Int)第次做Item選擇以後會記錄下這個Item的postion.在焦點丟失時清空把這個置為-1就可以達到想要的目的。

是的,通過setSelection(Int)可實現Item的選擇,但是它有時候並沒有沒有觸發OnItemSelectedListener中的onItemSelected事件,如果你在選中Item是添加動畫效果,就可以很明顯發現沒有達到動畫效果。 原因是在gridview翻頁的時候它有時沒有觸發OnItemSelectedListener中的onItemSelected事件。

3、最後使用了下面一種方法解決了問題:

GridView預設的獲得的焦點位置是0,如果焦點進入GridView,第0個Item得到焦點也不會觸發ItemSelected事件,可以在載入完GridView的時候使用上面反射的代碼清除預設焦點,達到預期效果。

gridview.setOnFocusChangeListener(new View.OnFocusChangeListener() {@Overridepublic void onFocusChange(View v, boolean hasFocus) {if (!hasFocus) {try {@SuppressWarnings("unchecked")Class<GridView> c = (Class<GridView>) Class.forName("android.widget.GridView");Method[] flds = c.getDeclaredMethods();for (Method f : flds) {if ("setSelectionInt".equals(f.getName())) {f.setAccessible(true);f.invoke(v,new Object[] { Integer.valueOf(-1) });}}} catch (Exception e) {e.printStackTrace();}}}});

還有一點注意的是,需要在onItemSwlected與onNothingSelected中同時實現動畫效果,這樣便可實現了硬鍵盤選擇Item是的動畫效果。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.