如何在Gridview中仅突出显示带边框的一个项目

时间:2015-04-30 10:19:35

标签: android gridview

我试图通过为网格视图创建一个白色边框来显示所选项目。

我设法做到了,但我遇到的问题是如何“取消选择”之前选择的那个。关键是只保留一个带边框的项目。

这是我到目前为止所做的:

GridView gridView = (GridView) findViewById(R.id.gridViewFX);
        gridView.setAdapter(new ImageAdapter(this));
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                for(int i = 0; i< parent.getCount(); i++){
                 //Unselect all the views in parent

                }
                //Select the item clicked
                Log.v("FILTER NAME:", filterNames.get(position));
                selectedFilterName = filterNames.get(position);

                ViewHolder selectedHolder = (ViewHolder)view.getTag();
                RoundedImageView iv = selectedHolder.getImageView();
                iv.setBorderColor(Color.WHITE);
                iv.setBorderWidth(20.f);


            }

更新:这是我的ImageAdapter类和ViewHolder:

public class ImageAdapter extends BaseAdapter {

        private LayoutInflater inflater;
        private Context mContext;

        public ImageAdapter(Context c) {
            mContext = c;
            inflater = LayoutInflater.from(mContext);
        }

        public int getCount() {
            return bitmaps.size();
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            View view = convertView;
            if (view == null) {
                view = inflater.inflate(R.layout.item_grid_image_fx, parent, false);
                holder = new ViewHolder();
                assert view != null;
                holder.imageView = (RoundedImageView) view.findViewById(R.id.gridImageFXView);
                holder.textView = (TextView)view.findViewById(R.id.effectNameTextView);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }

            holder.imageView.setImageBitmap(bitmaps.get(position));
            holder.textView.setText("" + filterNames.get(position));

            return view;
        }
    }

    static class ViewHolder {
        public RoundedImageView getImageView() {
            return imageView;
        }


        RoundedImageView imageView;
        TextView textView;
    }

任何有关将for循环放入“取消选择”内容的帮助都将不胜感激!

3 个答案:

答案 0 :(得分:1)

在开始之前,你必须为视图设置Id。

第1步:

创建一个整数变量 int id = 0;

第2步:

onItemClick监听器内部更改值

id =在此处更改ID

第3步:

内部getView()

if (id == getLayoutId){
//Set Your Border
}else{
//Remove the border here
}

注意:

现在遇到另一个问题,您需要在单击同一项目时删除选择。所以

在onItemClick下需要条件

if (id == your_current_clicked_id){
id = 0;
}else{
id = //set_your_id;
}

答案 1 :(得分:1)

解决了! - 我终于明白了。这是解决方案: - 在上面的代码中替换它并且它可以工作

//Unselect all views before selecting new one
                int count = parent.getChildCount();
                for (int i = 0; i < count; i++) {
                    View childAt = parent.getChildAt(i);
                    ViewHolder viewHolder = (ViewHolder)childAt.getTag();
                    viewHolder.getImageView().setBorderWidth(.0f);
                }

答案 2 :(得分:0)

使用选择器

为您的选择器编写可绘制文件,例如将其命名为selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_pressed="true">
    <shape>
        <solid android:color="@color/blue" />
    </shape>
</item>

<item android:state_pressed="false">
    <shape>
       <solid android:color="@android:color/transparent" />
    </shape>
</item>
</selector>

然后在gridview的xml中添加以下两行

 android:drawSelectorOnTop="true"
 android:listSelector="@drawable/selector"