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

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() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				//執行相對應操作
							}

			@Override
			public 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() {
			@Override
			public 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是的動畫效果。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章