1. In the past few days, you have to use the hard keyboard to select items in the gridview to perform different operations. After several days, you may not be able to solve the problem. after a long time of exploration, you have also found a lot of information on the Internet. Finally, I finally got my eyes and basically realized its functions. Write this article to summarize.
2. Add the gridview data first:
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>
Main Code:
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);
Listener when item is selected
Gridview. setonitemselectedlistener (New onitemselectedlistener () {@ overridepublic void onitemselected (adapterview <?> Arg0, view arg1, int arg2, long arg3) {// todo auto-generated method stub // execute the corresponding operation} @ overridepublic void onnothingselected (adapterview <?> Arg0) {// todo auto-generated method stub // execute the corresponding operation }});
When selecting an item using a hard keyboard, because if the focus is removed from an item on the gridview, The gridview loses the focus. when the focus is returned to the gridview, if the first item with focus is removed from the last item, the onitemselected event in onitemselectedlistener is not triggered.
Based on this, someone on the Internet provides this method: Check that the source code contains a default method setselection (INT). After the item is selected for the first time, the postion of this item will be recorded. when the focus is lost, empty and set this to-1 to achieve the desired goal.
Yes, you can use setselection (INT) to select an item, but it does not trigger the onitemselected event in onitemselectedlistener sometimes. If you add an animation effect to the selected item, it is obvious that the animation effect is not achieved. The reason is that it sometimes does not trigger the onitemselected event in the onitemselectedlistener when the gridview page is turned over.
3. Finally, the following method is used to solve the problem:
The default focus position obtained by the gridview is 0. If the focus enters the gridview, The itemselected event is not triggered when the focus of the 0th items are obtained, you can clear the default focus using the code reflected above when loading the gridview to achieve the expected effect.
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();}}}});
Note that you need to implement the animation effect in both onitemswlected and onnothingselected, so that you can select the item on the hard keyboard.