【问题标题】:Android GridView item change image on pressed and change backAndroid GridView 项目在按下时更改图像并变回
【发布时间】:2014-09-04 19:37:24
【问题描述】:

问题描述

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

问题

当用户按下项目然后释放按钮时,我尝试找到一些标准机制来将图像从正常状态更改为按下状态,但找不到。而不是我使用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;
}

【问题讨论】:

  • 您是否尝试使用selector drawable 作为您的图像?
  • 如果您已经制作了两个图像,请在您的GridView 中使用Button 元素并将您的按钮的Background 元素设置为可绘制的状态选择器。有关使选择器可绘制的 xml 声明,请参阅此链接。 mkyong.com/android/android-imagebutton-selector-example
  • @tyczj 如何将选择器用于多个图像,因为我有一个视图和多个图像?可以举个例子吗?
  • @kabuto178 但是如何为单个视图项设置不同的选择器?
  • @ViToBrothers 为每个按钮创建不同的选择器。

标签: java android image gridview


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多