Android GridView项目按下后更改图像并更改回来

时间:2014-09-04 19:37:25

标签: java android image gridview

问题描述

在我的应用程序中,我有GridView,其中包含12个项目。 GridView中的每个项目都有自己的图标,如下图所示。对于每个图标,我有两个图像,一个按下,一个正常。

GridView with different item and images

问题

我尝试找到一些标准机制,当用户按下项目然后释放按钮但无法找到时,将图像从正常状态更改为按下状态。而不是我使用public boolean onTouch(View v, MotionEvent event)方法,但它带来了一些副作用,例如当用户向下滚动或滑动屏幕以更改标签时,单击项目。

源代码

@Override
public boolean onTouch(View v, MotionEvent event) {
    /* Get Action on Touch. */
    int action = event.getActionMasked();
    if (action == MotionEvent.ACTION_DOWN) {
        float currentXPosition = event.getX();
        float currentYPosition = event.getY();

        lastPressedPosition = gvCategories.pointToPosition((int) currentXPosition, (int) currentYPosition);
        if(lastPressedPosition < 0 || lastPressedPosition > sDefaultArray.length)
            return false;

        /* Set Pressed Image. */
        Drawable drawable = getActivity().getResources().getDrawable(sPressedArray[lastPressedPosition]);
        Category category = (Category) gvCategories.getItemAtPosition(lastPressedPosition);
        category.setImage(((BitmapDrawable) drawable).getBitmap());
        CategoriesGridAdapter adapter = (CategoriesGridAdapter)gvCategories.getAdapter();
        adapter.notifyDataSetChanged();

    }
    else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {

        /* Get current possition to compare with the last one. */
        float currentXPosition = event.getX();
        float currentYPosition = event.getY();
        int currentPosition = gvCategories.pointToPosition((int) currentXPosition, (int) currentYPosition);
        if(currentPosition == -1 && lastPressedPosition == -1)
            return false;
        if (currentPosition == lastPressedPosition && action == MotionEvent.ACTION_UP) {

            Category category = (Category) gvCategories.getItemAtPosition(currentPosition);
            Log.i(TAG, String.format("Category Title is: %s", category.getTitle()));

            if (category == Category.More) {

                //tracker.trackEvent("Category Clicked", "More", "", 0L);

                /* Get current selected language. */
                final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
                final String lang = preferences.getString(Preferences.LANGUAGE, "en");
                /* Load All categories available in the application. */
                updateCategoriesAdapter(lang, true);
            }
            else {

                //tracker.trackEvent("Category Clicked", category.getTitle(), "", 0L);

                CategoryDetailsActivity.sFragmentManager = getFragmentManager();
                /* Start categories detail activity. */
                Intent intent = new Intent(getActivity().getApplicationContext(), CategoryDetailsActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(CategoryDetailsActivity.CATEGORIES_IDS, category.getIDs());
                intent.putExtra(CategoryDetailsActivity.CATEGORY_NAME, category.getTitle());
                getActivity().getApplicationContext().startActivity(intent);
            }


        }

        if(lastPressedPosition == -1)
            return false;

        /* Set Pressed Image. */
        Drawable drawable = getActivity().getResources().getDrawable(sDefaultArray[lastPressedPosition]);
        Category category = (Category) gvCategories.getItemAtPosition(lastPressedPosition);
        category.setImage(((BitmapDrawable) drawable).getBitmap());
        CategoriesGridAdapter adapter = (CategoriesGridAdapter)gvCategories.getAdapter();
        adapter.notifyDataSetChanged();
    }

    return false;
}

1 个答案:

答案 0 :(得分:1)

如果您已经制作了两张图像,请在GridView中使用Button元素,并将按钮的Background元素设置为可绘制的状态选择器。关于使你的选择器可绘制example here的XML声明,请参阅此链接。 对于您的场景,您必须为每个按钮创建不同的选择器XML文件。