在$ P $的Andr​​oid GridView的项目更改影像pssed和改回来 [英] Android GridView item change image on pressed and change back

查看:142
本文介绍了在$ P $的Andr​​oid GridView的项目更改影像pssed和改回来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题说明

在我的应用我有 GridView控件在这12​​个项目。在 GridView控件每个项目都有自己的图标像它下面的图片所示。对于每一个图标,我有两个图像的一个pressed和一个正常的。

问题

我试图找到一些标准的机制,从正常状态改变图像presses状态时,该项目的用户preSS,然后松开按钮,但无法找到。相反的,我使用公共布尔onTouch(视图V,MotionEvent事件)方法,但它带来的一些副作用,例如当用户向下滚动或滑动屏幕来更改标签,物品被点击。

来源$ C ​​$ C

  @覆盖
公共布尔onTouch(视图V,MotionEvent事件){
    / *在触摸付诸行动。 * /
    INT行动= event.getActionMasked();
    如果(动作== MotionEvent.ACTION_DOWN){
        浮currentXPosition = event.getX();
        浮currentYPosition = event.getY();        最后pressedPosition = gvCategories.pointToPosition((INT)currentXPosition,(INT)currentYPosition);
        如果(最后pressedPosition℃的最后|| pressedPosition> sDefaultArray.length)
            返回false;        / *设置pressed图像。 * /
        。可绘制可绘制= getActivity()getResources()getDrawable(S pressedArray [最后pressedPosition]);
        分类分类=(类别)gvCategories.getItemAtPosition(最后pressedPosition);
        category.setImage(((BitmapDrawable)抽拉).getBitmap());
        CategoriesGridAdapter适配器=(CategoriesGridAdapter)gvCategories.getAdapter();
        adapter.notifyDataSetChanged();    }
    否则,如果(动作== || MotionEvent.ACTION_UP行动== MotionEvent.ACTION_CANCEL){        / *获取当前possition与最后一个比较。 * /
        浮currentXPosition = event.getX();
        浮currentYPosition = event.getY();
        INT currentPosition = gvCategories.pointToPosition((INT)currentXPosition,(INT)currentYPosition);
        如果(currentPosition == -1放大器;&放大器;最后pressedPosition == -1)
            返回false;
        如果(currentPosition ==最后pressedPosition&放大器;&安培;行动== MotionEvent.ACTION_UP){            分类分类=(类别)gvCategories.getItemAtPosition(currentPosition);
            Log.i(TAG,的String.format(类标题是:%S,category.getTitle()));            如果(类别== Category.More){                //tracker.trackEvent(\"Category单击的,多,,0L);                / *获取当前选择的语言。 * /
                最后一个共享preferences preferences = preferenceManager.getDefaultShared preferences(getActivity());
                最后弦乐LANG = preferences.getString(preferences.LANGUAGE,恩);
                / *加载应用程序中提供的所有类别。 * /
                updateCategoriesAdapter(郎,真);
            }
            其他{                //tracker.trackEvent(\"Category单击的,category.getTitle(),,0L);                CategoryDe​​tailsActivity.sFragmentManager = getFragmentManager();
                / *启动类别详细活动。 * /
                意向意图=新意图(getActivity()getApplicationContext(),CategoryDe​​tailsActivity.class。);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(CategoryDe​​tailsActivity.CATEGORIES_IDS,category.getIDs());
                intent.putExtra(CategoryDe​​tailsActivity.CATEGORY_NAME,category.getTitle());
                。getActivity()getApplicationContext()startActivity(意向)。
            }
        }        如果(最后pressedPosition == -1)
            返回false;        / *设置pressed图像。 * /
        可绘制可绘制= getActivity()getResources()getDrawable(sDefaultArray [最后pressedPosition])。
        分类分类=(类别)gvCategories.getItemAtPosition(最后pressedPosition);
        category.setImage(((BitmapDrawable)抽拉).getBitmap());
        CategoriesGridAdapter适配器=(CategoriesGridAdapter)gvCategories.getAdapter();
        adapter.notifyDataSetChanged();
    }    返回false;
}


解决方案

如果你已经取得的两幅图像,在使用一个Button元素的 GridView控件,并设置背景您的按钮,一个国家选择绘制的元素。参考此链接XML声明上使这里您选择绘制的例子。
对于您的情况,你将不得不为每个按钮创建不同的选择XML文件。

Problem Description

In my application I have GridView with 12 items in it. Every item in the GridView has its own icon like it is shown in the image below. For every icon I have two images one pressed and one normal.

Question

I try to find some standard mechanisms to change images from Normal state to Presses state when user press on the item and then release the button, but can't find. Instead of that I use public boolean onTouch(View v, MotionEvent event) method but it brings to some side effects for example when user scroll down or slide screen to change tab, items are clicked.

Source Code

@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;
}

解决方案

If you have the two images already made, use a Button element in your GridView and set the Background element of your button to a state selector drawable. refer to this link for XML declarations on making your selector drawable example here . For your scenario you will have to create different selector XML files for each button.

这篇关于在$ P $的Andr​​oid GridView的项目更改影像pssed和改回来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆