仅在单击时更改可绘制的项目状态 - GalleryView

发布于 2024-11-19 19:13:33 字数 210 浏览 3 评论 0原文


我使用 GalleryView 通过 BaseAdapter 显示字符串
根据项目状态(选择器)更改背景和文本颜色没有问题

但我只想在单击该项目而不仅仅是选择该项目时更改背景和文本颜色,GalleryView 项目状态会随着您四处滑动时调用的选定事件而变化(自动选择居中的项目)

用 2 个字来说,如果未单击该项目,则背景不得更改
我该如何处理呢?

I use a GalleryView to display string with a BaseAdapter
No problem to change the background and text color depending on the item state (selector)

But I want to change the background and text color only if the item is clicked not just selected, the GalleryView item state changes with the selected event which is called when you fling around (the centered item is automatically selected)

In 2 words the background mustn't change if the item isn't clicked
How can I go with it ?

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

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

发布评论

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

评论(2

兮颜 2024-11-26 19:13:34

您必须使用属性 android:listSelector 在 xml 中实现列表选择器,然后将此样式设置为列表视图。

You must implement a list selector in you xml using the attribute android:listSelector and then set this style to your list view.

慕巷 2024-11-26 19:13:33

您必须捕获选定的事件并将视图的背景更改为未选定。
存储上次单击的位置以避免更改其选定的背景。此外,存储上次单击的视图,以便在单击另一个项目而不进行快速操作时恢复未选择的背景:

    gallery = (Gallery) findViewById(R.id.gallery);
    gallery.setAdapter(new DaysGalleryAdapter(this, new int[]{R.layout.gallery_item}, new int[]{R.id.tv_gallery}));
    gallery.setOnItemClickListener(mOnGalleryClick);
    gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

           @Override
           public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
               if(position != savedClickedPosition) 
                   view.setBackgroundResource(R.drawable.bg_unselected);
           }

           @Override
           public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

           }

   }); 

   private OnItemClickListener mOnGalleryClick = new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        lastItemSelected.setBackgroundResource(R.drawable.tab_bg_unselected);
        view.setBackgroundResource(R.drawable.tab_bg_selected);
        lastItemSelected = view;
        savedClickedPosition = position;
        // Do stuff
    }       
   };

如果对您来说为时已晚,Fluckysan 至少希望它对遇到相同问题的任何人有所帮助。
快乐编码!

You must capture the selected event and change the background of the view to unselected.
Store last clicked position to avoid changing its selected background. Also, store last clicked view to restore unselected background when another item is clicked without flinging:

    gallery = (Gallery) findViewById(R.id.gallery);
    gallery.setAdapter(new DaysGalleryAdapter(this, new int[]{R.layout.gallery_item}, new int[]{R.id.tv_gallery}));
    gallery.setOnItemClickListener(mOnGalleryClick);
    gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

           @Override
           public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
               if(position != savedClickedPosition) 
                   view.setBackgroundResource(R.drawable.bg_unselected);
           }

           @Override
           public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

           }

   }); 

   private OnItemClickListener mOnGalleryClick = new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        lastItemSelected.setBackgroundResource(R.drawable.tab_bg_unselected);
        view.setBackgroundResource(R.drawable.tab_bg_selected);
        lastItemSelected = view;
        savedClickedPosition = position;
        // Do stuff
    }       
   };

If it's too late for you Fluckysan at least hope it helps to anyone who's having the same problem.
Happy coding!

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