请教一下关于事件处理

DukeDing 发布于 2015/05/21 14:22
阅读 84
收藏 0
MainActivity.java 文件:

package com.duke.gridviewtest;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.GridView;

public class MainActivity extends Activity {
    private GridView mGridView = null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mGridView = (GridView) findViewById(R.id.gridview);

        ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < 10; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("ItemImage", R.drawable.icon);
            map.put("ItemText", "NO." + String.valueOf(i));
            lstImageItem.add(map);
        }

        GridViewAdapter saImageItems = new GridViewAdapter(this, lstImageItem);
        mGridView.setAdapter(saImageItems);

        mGridView.setSelector(R.color.full_transparent_color);

        mGridView.setOnItemClickListener(new ItemClickListener());

        mGridView.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                System.out.println("GridView onItemClick: onItemLongClick = " + position + ", id = " + id);

                return false;
            }
        });

        mGridView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                System.out.println("GridView onTouch, action = " + event.getAction());

                return true;
            }
        });
    }

    class ItemClickListener implements OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            System.out.println("GridView onItemClick: position = " + position + ", id = " + id);

            HashMap<String, Object> item = (HashMap<String, Object>) parent.getItemAtPosition(position);
            setTitle((String) item.get("ItemText"));
        }
    }
}

GridViewAdapter.java 文件:
package com.duke.gridviewtest;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class GridViewAdapter extends BaseAdapter {
    private ArrayList<HashMap<String, Object>> mItemList = null;
    private LayoutInflater inflater = null;

    public GridViewAdapter(Context context, ArrayList<HashMap<String, Object>> itemList) {
        mItemList = itemList;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        int count = mItemList.size();
        System.out.println("getCount + " + count);

        return count;
    }

    @Override
    public Object getItem(int position) {
        HashMap<String, Object> itemData = mItemList.get(position);
        return itemData;
    }

    @Override
    public long getItemId(int position) {
        if (position > getCount()) {
            return 0;
        }

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.grid_item_layout, null);
        }

        HashMap<String, Object> map = mItemList.get(position);
        Object icon = map.get("ItemImage");
        Object str = map.get("ItemText");

        ImageView image = (ImageView) convertView.findViewById(R.id.ItemImage);
        image.setImageResource(R.drawable.icon);

        TextView text = (TextView) convertView.findViewById(R.id.ItemText);
        text.setText((String) str);

        convertView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                System.out.println("GridViewAdapter Item onTouch, action = " + event.getAction());
                return false;
            }
        });

        return convertView;
    }
}

// activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.duke.gridviewtest.MainActivity" >

    <GridView
	    android:id="@+id/gridview"
	    android:layout_width="match_parent" 
	    android:layout_height="match_parent"
	    android:numColumns="auto_fit"
	    android:verticalSpacing="10dp"
	    android:horizontalSpacing="10dp"
	    android:columnWidth="90dp"
	    android:stretchMode="columnWidth"
	    android:gravity="center"
    />

</RelativeLayout>

// grid_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
         xmlns:android="http://schemas.android.com/apk/res/android" 
         android:layout_height="wrap_content" 
         android:paddingBottom="4dip" 
         android:layout_width="fill_parent">
         <ImageView 
               android:layout_height="wrap_content" 
               android:id="@+id/ItemImage" 
               android:layout_width="wrap_content" 
               android:layout_centerHorizontal="true"> 
         </ImageView>
         <TextView 
               android:layout_width="wrap_content" 
               android:layout_below="@+id/ItemImage" 
               android:layout_height="wrap_content" 
               android:text="TextView01" 
               android:layout_centerHorizontal="true" 
               android:id="@+id/ItemText">
         </TextView>
</RelativeLayout>



GridView 设置OnItemClickListener,OnItemLongClickListener。

GridView item 设置 OnTouchListener。

想实现的功能是:触摸到某个item的时候,TextView字体变颜色,但是又要求能响应OnItemClickListener和OnItemLongClickListener。

在GridView item OnTouchListener,onTouch中处理,down事件时候设置字体颜色,up事件字体颜色还原。但是发现不会执行up事件。如果是down事件,返回true,则能响应up事件,但是不能响应OnItemClickListener和OnItemLongClickListener。

求助各位大神,有没有什么好的解决办法,指点一下,非常感谢。




加载中
返回顶部
顶部