【问题标题】:How to highlight selected gridview items如何突出显示选定的gridview项目
【发布时间】:2014-08-13 07:41:51
【问题描述】:

我使用 gridview CHOICE_MODE_MULTIPLE_MODAL 在我的 gridview 中选择多个项目(图像)。

我使用MultiChoiceModeListener来实现这个,问题是选中的项目没有被突出显示

选择器 xml:

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

   </selector>

当我触摸一个项目时,它会被蓝色覆盖(我认为这是因为我使用了gridView.setDrawSelectorOnTop(true);)...只要我抬起手指,蓝色就会被移除。

我希望所选项目获得 highlightedsemi transparent blue 颜色,并且它应该保持突出显示,直到我取消选择该项目。

Java 代码:

public class HappyFragment extends ParentFragment {

    public HappyImageAdapter imageAdapter;
    private List<ResolveInfo> mApps;
    GridView gridView;

     public static HappyFragment newInstance() {
         HappyFragment fragment = new HappyFragment();
         return fragment;
     }

     @Override
     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

//          if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
//            //  mContent = savedInstanceState.getString(KEY_CONTENT);
//          }
     }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.happy_fragment_layout, container, false);
           //  ImageView iv= (ImageView) view.findViewById(R.id.imageView1); 
             gridView = (GridView)view.findViewById(R.id.gvHappy);

            // Instance of ImageAdapter Class
             imageAdapter = new HappyImageAdapter(getActivity());
            gridView.setAdapter(imageAdapter);
            gridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
            gridView.setMultiChoiceModeListener(new MultiChoiceModeListener());
            gridView.setDrawSelectorOnTop(true);
            gridView.setSelector(getResources().getDrawable(R.drawable.gridview_selector));

            gridView.setOnItemLongClickListener(new OnItemLongClickListener() {

                public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                    Toast.makeText(getActivity(), String.valueOf(imageAdapter.mThumbIds[position]), Toast.LENGTH_SHORT).show();
                    return true;
                }
            }); 

            gridView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
                    if(BaseActivity.isinint) { // check if any app cares for the result
                        int ImageResourse=imageAdapter.mThumbIds[position];
                    Uri path = Uri.parse("android.resource://dragonflymobile.stickers.lifestickers/" + ImageResourse);

                        Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND, path); //Create a new intent. First parameter means that you want to send the file. The second parameter is the URI pointing to a file on the sd card. (openprev has the datatype File)

                        ((Activity)getActivity()).setResult(Activity.RESULT_OK, shareIntent); //set the file/intent as result
                        ((Activity)getActivity()).finish(); //close your application and get back to the requesting application like GMail and WhatsApp
                        return; //do not execute code below, not important
                    } else {
                        Intent intent = new Intent(getActivity(), PreviewActivity.class);
                        intent.putExtra("Image Int", imageAdapter.mThumbIds[position]);
                        startActivity(intent);
                    }
                }
            });
            return view;
        }

        @Override
        public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
         //   outState.putString(KEY_CONTENT, mContent);
        }

        //multi select mode codes
        public class MultiChoiceModeListener implements GridView.MultiChoiceModeListener {
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                mode.setTitle("Select Items");
                mode.setSubtitle("One item selected");
                return true;
            }

            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return true;
            }

            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                return true;
            }

            public void onDestroyActionMode(ActionMode mode) {
            }

            public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
                int selectCount = gridView.getCheckedItemCount();
                switch (selectCount) {
                case 1:
                    mode.setSubtitle("One item selected");
                    break;
                default:
                    mode.setSubtitle("" + selectCount + " items selected");
                    break;
                }
            }
}
}

【问题讨论】:

  • 我用CHOICE_MODE_SINGLE 为ListView 完成了该操作,您可以在CHOICE_MODE_MULTIPLE_MODAL 为GridView 尝试它,看看here

标签: android gridview


【解决方案1】:

按照这个,

1.将您的网格视图子项放入FrameLayout

2.give FrameLayout 属性android:foreground="?android:attr/activatedBackgroundIndicator"

就是这样...无需使用额外的库

【讨论】:

  • 请注意,使用android:background="?android:attr/activatedBackgroundIndicator" 以防您想更改网格项目后面而不是前面的突出显示颜色。
【解决方案2】:

好的,我找到了这个第 3 方库,它可以用更少的代码做到这一点 这里是githublink

这是一个带有图像的gridview教程link

【讨论】:

    【解决方案3】:

    如果有人想要没有第三方库的解决方案,只需将此代码放入

    onItemCheckedStateChanged(这里 gv 是您的 gridView 名称)

        if(checked){
                    View tv = (View) gv.getChildAt(position);
                    tv.setBackgroundColor(Color.RED);
                }else{
                    View tv = (View) gv.getChildAt(position);
                    tv.setBackgroundColor(Color.TRANSPARENT);
                }
    

    【讨论】:

    • 完美解决方案..而不是 Color.RED 我使用了 getResources().getColor(R.color.borderColor)&lt;color name="borderColor"&gt;#28AABA&lt;/color&gt; 。这给出了与画廊中相同的颜色。 :)
    【解决方案4】:
    gridView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
        @Override
        public void onItemCheckedStateChanged(android.view.ActionMode actionMode, int i, long l, boolean b) {
            // Capture total checked items
            final int checkedCount = gridView.getCheckedItemCount();
            // Set the CAB title according to total checked items
            actionMode.setTitle(checkedCount + " Selected");
    
            if(gridView.getChildAt(i).isSelected()) {
                gridView.getChildAt(i).setBackgroundColor(Color.parseColor("#FF4081"));
            }else{
                gridView.getChildAt(i).setBackgroundColor(Color.parseColor("#b1e76905"));
            }
            // Calls toggleSelection method from ListViewAdapter Class
            adapter.toggleSelection(i);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-16
      • 2015-07-11
      • 1970-01-01
      • 2012-01-23
      • 2012-02-22
      • 1970-01-01
      相关资源
      最近更新 更多