当前位置: 代码迷 >> Android >> Android学习笔记(25) - 硬键盘+GridView选择Item有关问题
  详细解决方案

Android学习笔记(25) - 硬键盘+GridView选择Item有关问题

热度:31   发布时间:2016-05-01 14:45:32.0
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是的动画效果。



  相关解决方案