java - Android GridView 项目在按下时更改图像并更改回来

标签 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;
}

最佳答案

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

关于java - Android GridView 项目在按下时更改图像并更改回来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25673713/

相关文章:

java - Graphics2D.drawArc 中的高度参数

java - 迁移到 JDK 11 后 Spring Boot 测试中的 Mockito 错误

java - 应该在构造函数中完成多少

java - 如何检查文本字段的空值并将其放入 if else 语句以将其重定向到错误页面?

java - 使用 Camera API 制作适用于所有设备的 android 相机应用程序

image - 如何在 Google App Engine 中加载 Blobproperty 图像?

android - 无法在 Redmi 手机上运行 react-native 应用程序?

android - PhoneGap 3.3 无法解析import org.apache.cordova

javascript - 图像数组中的随机元素及其在页面刷新时的各自链接

java - 为什么我得到 NoClassDefFoundError 异常?