ter.from(c);
? ? }
?
? ? public int getCount() {
? ? ? return mFoundList.size();
? ? }
?
? ? public Object getItem(int position) {
? ? ? return mFoundList.get(position);
? ? }
?
? ? public long getItemId(int position) {
? ? ? return 0;
? ? }
?
? ? // create a new ItemView for each item referenced by the Adapter
? ? public View getView(int position, View convertView, ViewGroup parent) {
?
? ? ? View v = convertView;
? ? ? ImageView picture;
? ? ? TextView name;? ? ? ?
? ? ? TextView price;
?
? ? ? if(v == null) {
? ? ? ? v = inflater.inflate(R.layout.view_grid_item, parent, false);
? ? ? ? v.setTag(R.id.picture, v.findViewById(R.id.picture));
? ? ? ? v.setTag(R.id.grid_name, v.findViewById(R.id.grid_name));
? ? ? ? v.setTag(R.id.grid_price, v.findViewById(R.id.grid_price));
? ? ? }
? ? ? picture= (ImageView) v.getTag(R.id.picture);
? ? ? name= (TextView) v.getTag(R.id.grid_name);
? ? ? price= (TextView) v.getTag(R.id.grid_price);
?
? ? ? final MenuItem foundItem = (MenuItem) mFoundList.get(position);
?
? ? ? InputStream inputStream = null;
? ? ? AssetManager assetManager = null;
? ? ? try {
? ? ? ? assetManager = getAssets();
? ? ? ? inputStream =? assetManager.open(foundItem.imageName);
? ? ? ? picture.setImageBitmap(BitmapFactory.decodeStream(inputStream));
? ? ? } catch (Exception e) {
? ? ? ? Log.d("ActionBarLog", e.getMessage());
? ? ? } finally {
? ? ? }
? ? ? name.setText(foundItem.name);
? ? ? price.setText(foundItem.price);
?
? ? ? return v;
? ? }
? }
}
代码示例 4 续
需要在布局上考虑的另外一个问题就是屏幕的横向和纵向模式. 底下是?res/layout-land 目录中的?search_query_grid_results.xml?文件. 你可以从这儿发现 numColumns 被设置成了4, ?除了这个值是2以外,?res/layout-port 文件跟这个文件是一样的.
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:paddingLeft="5dp"
? android:paddingRight="5dp"
? android:paddingBottom="5dp"
? android:paddingTop="5dp"
? ? android:orientation="vertical">
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:orientation="horizontal">
? ? ? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? style="@style/FragmentTitle"
? ? ? ? ? ? android:text="Results For: " />
? ? ? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? style="@style/OrderTitle"/>
? ? ? ?
? ? ? ? ? android:id="@+id/search_results"
? ? ? android:layout_width="fill_parent"
? ? ? android:layout_height="0dp"
? ? ? android:paddingTop="10dp"
? ? ? android:numColumns="4"
? ? ? android:verticalSpacing="10dp"
? ? ? android:horizontalSpacing="10dp"
? ? ? android:layout_weight="1"
? ? ? android:stretchMode="columnWidth"
? ? ? android:gravity="center"/>
代码示例 5: 搜索视图的横向布局xml

图 2: 当用户点击时单项的详细视图
要退出搜索视图,我们比较喜欢用横扫的手势让它向左或者向右滑走,类似于菜单剩余部分的视图页滚动效果. GestureDetector 在这样一个列表视图之上运行起来很不错, 但当与网格视图一起结合起来的时候并不会有效果. 因而我们只能转而去使用一个 GestureOverlayView. 你见更需要使用 GestureBuilder 应用程序构建一个手势库,这个应用程序可以再SDK示例 (例如.?android\sdk\samples\android-19\legacy\GestureBuilder) 中找到. 在你的蛇摆上构建并运行这个应用,并使用它来命名和创建出手势. 把所有你需要的手势都添加好了(在我们的案例中,就是向左扫动和向右扫动), 然后就将‘gestures’文件从你的设备复制到 res/raw 目录中. 应用会告诉你将手势文件保存到哪个地方. 在我这儿,所有要做的就是通过USB连接我们设备,手势文件就在root目录中.

图 3: 手势构建应用程序以及我们所添加的手势截图
你的文件一就位,用下面的代码修改 SearchResultsActivity 类:
GestureLibrary gestureLibrary;
GestureOverlayView gestureOverlayView;
代码示例 6 : ?GestureOverlayView 的变量声明
在 onCreate 方法中, 初始化视图,加载库文件,并设置当用户执行一个匹配的手势时要做什么的侦听器. 确保能匹配到你在库文件中创建的名称. 动画我们则准备在 overrideP