将选定的视图设置为突出显示 - 滚动时随机更改视图

发布于 2024-12-08 16:50:36 字数 1450 浏览 5 评论 0原文

我尝试过


大家好,我已经制作了一个 GridView,我已将其填充到我的数据库中。现在我从 Google GridView 教程中制作了一个 onItemClickListner。现在我尝试使当用户单击 GridView 中的图片时,它保持选中状态并获取背景颜色,以便用户看到他选择的图像。 但这并没有按照应有的方式进行。我用布尔值创建了一个 If ,当用户单击下一个视图时,旧视图会再次变得透明,而新单击的视图会获得背景色。但是在我选择一个视图并上下滚动后。突出显示的视图有时会发生随机样式的变化。

问题


所以我的问题是我必须在 onItemClickListner 中更改什么,这个随机视图更改停止(?),我没有找到这样做的方法。有没有更简单的方法来突出显示所选视图? 为了理解“选定视图”=用户单击它,比它被选择并且应该突出显示。

代码


这是我的onItemClickListner的代码:

    //Hier wird die Grindview gefüllt
    final GridView gridview = (GridView) findViewById(R.id.SmileyGrind);
    gridview.setAdapter(new ImageAdapter(this, info));


    //Hier wird gemerkt welches bild
    gridview.setOnItemClickListener(new OnItemClickListener() {

boolean color = false;
View vv;


        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(SFilterConfigActivity.this, "" + position, Toast.LENGTH_SHORT).show();
           // gridview.setSelection(position);

            if ( color == false){
                v.setBackgroundColor(0xFF00FF00);
                vv = v;
                color = true;

            }
            else {
                vv.setBackgroundColor(0x00000000);
                v.setBackgroundColor(0xFF00FF00);
                vv = v;


            }


        }      
    });

谢谢您提前提供帮助!

What I tried


Hello Guys, I have made a GridView which I've filled over my database. Now I made an onItemClickListner, from the Google GridView tutorial. Now I tried to make that when the user clicks on a picture in the GridView, it keeps selected and gets a backgroundcolor so the user sees which Image he chooses.
But exactly this isn't working how it should. I made a If with a boolean which allways when the User clicks on the next View, that the old View gets transperent again and the new clicked View got a backgroundcolor. But after I selected a View and I scroll up and down. The highlighted View changes sometimes randomstyled.

Question


So my question is what I have to change in my onItemClickListner, that this random View changing stops (?), I didn't found out a way to do so. And is there a easier way to highlight the selected View?
For the understanding "selected View" = User clicks on it, than its selected and should be highlighted.

Code


Here's the Code of my onItemClickListner:

    //Hier wird die Grindview gefüllt
    final GridView gridview = (GridView) findViewById(R.id.SmileyGrind);
    gridview.setAdapter(new ImageAdapter(this, info));


    //Hier wird gemerkt welches bild
    gridview.setOnItemClickListener(new OnItemClickListener() {

boolean color = false;
View vv;


        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(SFilterConfigActivity.this, "" + position, Toast.LENGTH_SHORT).show();
           // gridview.setSelection(position);

            if ( color == false){
                v.setBackgroundColor(0xFF00FF00);
                vv = v;
                color = true;

            }
            else {
                vv.setBackgroundColor(0x00000000);
                v.setBackgroundColor(0xFF00FF00);
                vv = v;


            }


        }      
    });

Thx for you help in Advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人│生佛魔见 2024-12-15 16:50:36

我有同样的问题,但它是与 ListView 一起使用的。按照这个,它也适用于 GridView。

if(position == grid.getSelectedItemPosition())
   holder._linear.setBackgroundColor(Color.GRAY);           
else  
   holder._linear.setBackgroundColor(Color.BLACK);

并获取您用于 GridView 的布局的父布局,并在布局的 onClick 方法上添加 this 。

 holder._linear.setBackgroundColor(Color.GRAY); 

这将改变布局的背景颜色,上面的代码将解决您的随机背景问题。

I had the same issue but it was with ListView .follow this it will work for GridView too.

if(position == grid.getSelectedItemPosition())
   holder._linear.setBackgroundColor(Color.GRAY);           
else  
   holder._linear.setBackgroundColor(Color.BLACK);

and get the Parent Layout of the Layout that you are using for your GridView and on onClick method of layout just add this .

 holder._linear.setBackgroundColor(Color.GRAY); 

This will change the Background color of the layout and the above code will solve your random background problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文