Java Android drawSelectorOnTop 与 GridView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20254232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:36:47  来源:igfitidea点击:

Android drawSelectorOnTop with GridView

javaandroidandroid-gridviewandroid-selector

提问by lbrendanl

I am developing a tabbed application in which one of the fragments, CollectionsFragment, contains a GridView with an ImageView in each slot. I would like the to use a selector to give feedback to users when the user clicks on one of the images.

我正在开发一个选项卡式应用程序,其中一个片段 CollectionsFragment 包含一个 GridView,每个插槽中都有一个 ImageView。当用户单击其中一张图像时,我希望使用选择器向用户提供反馈。

I have successfully implemented the selector, however, my problem is that the selector is only drawing in the background of the image, but I would like to the selector to draw over the entire image. I have seen this problem referenced elsewhere, however, the solution selected by many, setting the drawSelectorOnTop property of the GridView, is not working for me.

我已经成功实现了选择器,但是,我的问题是选择器仅在图像的背景中绘制,但我希望选择器绘制整个图像。我在其他地方看到过这个问题,但是,许多人选择的解决方案,设置了 GridView 的 drawSelectorOnTop 属性,对我不起作用。

The relevant fragment with the relevant adapter code:

具有相关适配器代码的相关片段:

public class CollectionsFragment extends Fragment {
    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
             View view = inflater.inflate(R.layout.activity_collections, container, false);
             // Grid view is inside the xml view inflated above
             GridView gridView = (GridView)view.findViewById(R.id.gridview);
             gridView.setDrawSelectorOnTop(true);
             ((GridView) gridView).setAdapter(new CustomGridViewAdapter(getActivity()));
             return view;
        }

        private class CustomGridViewAdapter extends BaseAdapter {
            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                View v = view;
                ImageView picture;
                TextView name;

                if(v == null) {
                    v = inflater.inflate(R.layout.collections_item, viewGroup, false);
                    v.setTag(R.id.picture, v.findViewById(R.id.picture));
                    v.setTag(R.id.text, v.findViewById(R.id.text));
                }

                picture = (ImageView)v.getTag(R.id.picture);

                name = (TextView)v.getTag(R.id.text);

                Item item = (Item)getItem(i);
                name.setText(item.name);

                picture.setImageResource(item.drawableId);
                picture.setBackgroundResource(R.drawable.selector);

                return v;
            }
        }
}

And my selector for completeness sake:

为了完整起见,我的选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" 
      android:drawable="@color/buttonhighlight"/> <!-- pressed state -->
<item android:state_focused="true" 
      android:drawable="@color/buttonhighlight"/> <!-- focused state -->
<item android:drawable="@android:color/transparent"/> <!-- default state --> 
</selector>

Thanks for any help,

谢谢你的帮助,

采纳答案by Vikram

I think you are mistaken about setDrawSelectorOnTop(boolean). The selectordrawable that is being referenced here is GridView's internal selectordrawable.

我想你误会了setDrawSelectorOnTop(boolean)。该selector正被引用在这里绘制是GridView的内部selector绘制。

Even in the simplest implementation of GridView, when a grid item is clicked, the blue border is drawn around it. This is because, by default, gridview's own selector is drawn behindthe item. If you call setDrawSelectorOnTop(true), the selector (blue) will be drawn over the item.

即使在 最简单的实现中GridView,当单击一个网格项时,它周围会绘制蓝色边框。这是因为,默认情况下,gridview 自己的选择器是绘制behind项目的。如果您调用setDrawSelectorOnTop(true),选择器(蓝色)将被绘制在项目上。

But setDrawSelectorOnTop(boolean)has nothing to do with the selector you are setting in the adapter. Whether you pass true, or false, the ImageView's selector's behavior won't change.

setDrawSelectorOnTop(boolean)与您在适配器中设置的选择器无关。无论您是通过true还是false,ImageView 的选择器的行为都不会改变。

Solution:

解决方案:

Instead of setting the selector on each ImageView inside the adapter, make the GridView use your selector drawable:

不要在适配器内的每个 ImageView 上设置选择器,而是让 GridView 使用您的选择器可绘制:

GridView gridView = (GridView)view.findViewById(R.id.gridview);
gridView.setDrawSelectorOnTop(true);

// Make GridView use your custom selector drawable
gridView.setSelector(getResources().getDrawable(R.drawable.selector));

Now, there's no need for:

现在,不需要:

picture.setBackgroundResource(R.drawable.selector);

Edit:

编辑:

Although I don't recommend this (obvious overhead), it should work:

虽然我不推荐这个(明显的开销),但它应该可以工作:

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    View v = view;
    ImageView picture;

    ....
    ....

    LayerDrawable ld = new LayerDrawable(new Drawable[] 

                           // Drawable from item
                           { getResources().getDrawable(item.drawableId), 

                           // Selector
                           getResources().getDrawable(R.drawable.selector)});

    // Set the LayerDrawable
    picture.setImageDrawable(ld);

    // Don't need this
    // picture.setBackgroundResource(R.drawable.selector);

    return v;
}

回答by VJ Vélan Solutions

  1. Try setting the xml attribute android:drawSelectorOnTopin your activity_collections.xmlfile

  2. See if placing gridView.setDrawSelectorOnTop(true);after gridView.setAdapter();helps. Sometimes, the order matters (weird)

  3. If all else fails, you may have to switch GridView to some other view where setDrawSelectorOnTop() is proven to work consistently.

  1. 尝试attribute android:drawSelectorOnTop在您的activity_collections.xml文件中设置 xml

  2. 看看放在gridView.setDrawSelectorOnTop(true);后面是否有gridView.setAdapter();帮助。有时,顺序很重要(奇怪)

  3. 如果所有其他方法都失败了,您可能必须将 GridView 切换到其他一些视图,其中 setDrawSelectorOnTop() 被证明可以一致地工作。

HTH

HTH