在GridView中向ImageView添加边框

时间:2015-04-23 18:13:18

标签: android

如果再次单击同一图像,如何在单击图像后在GridView内向ImageView添加边框并消失边框?

gv_gridview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long Id) {
            // TODO Auto-generated method stub
            Toast.makeText(getBaseContext(),"You clikced on "+position,Toast.LENGTH_SHORT ).show();
            //  gv_gridview.setSelected(true);
            //  gv_gridview.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
            //  gv_gridview.setDrawSelectorOnTop(true);
        }
    });

2 个答案:

答案 0 :(得分:1)

这是网格项目选择问题

我假设您的网格项是ImageView。

<强>解决方案

在此处查找工作示例项目:https://github.com/jskierbi/sample-gridview-select

  1. 您需要将CheckedImageView类添加到项目中,因此网格视图可以为您检查项目。
  2. import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.Checkable;
    import android.widget.ImageView;
    
    public class CheckedImageView extends ImageView implements Checkable {
    
        boolean mFlgChecked = false;
    
        private static final int[] CHECKED_STATE_SET = {
                android.R.attr.state_checked
        };
    
        public CheckedImageView(Context context, AttributeSet attrs) {
    
            super(context, attrs);
        }
    
        @Override
        public int[] onCreateDrawableState(int extraSpace) {
    
            final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
            if (isChecked()) {
                mergeDrawableStates(drawableState, CHECKED_STATE_SET);
            }
            return drawableState;
        }
    
        @Override
        public boolean isChecked() {
    
            return mFlgChecked;
        }
    
        @Override
        public void setChecked(boolean checked) {
    
            mFlgChecked = checked;
            refreshDrawableState();
        }
    
        @Override
        public void toggle() {
    
            mFlgChecked = !mFlgChecked;
        }
    }
    

    2。添加颜色(res / values / colors.xml):

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <color name="image_selected">#0000FF</color>
        <color name="transparent">#00000000</color>
    
    </resources>
    
    1. 添加选择器,这将定义哪种颜色用于图像的状态(res / drawable / checked_image.xml):
    2. <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
      
          <item android:state_checked="true" android:drawable="@color/image_selected" />
          <item android:drawable="@color/transparent" />
      
      </selector>
      
      1. 添加网格项布局(res / layout / grid_view_item.xml)
      2. 请记住更改com.example.CheckedImageView包名称,以便它反映您放置CheckedImageView类的位置。

        <?xml version="1.0" encoding="utf-8"?>
        <com.example.CheckedImageView android:id="@+id/image"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/checked_image"
            android:cropToPadding="true"
            android:padding="6dp" />
        
        1. 最后,在适配器中使用此布局。以下是我的Activity类的完整代码:
        2. import android.app.Activity;
          import android.os.Bundle;
          import android.support.annotation.DrawableRes;
          import android.util.Log;
          import android.view.View;
          import android.view.ViewGroup;
          import android.widget.BaseAdapter;
          import android.widget.GridView;
          import android.widget.ImageView;
          
          
          public class MainActivity extends Activity {
          
              private static final @DrawableRes int[] IMAGES = {
                      R.drawable.a001, R.drawable.a002, R.drawable.a003, R.drawable.a004, R.drawable.a005,
                      R.drawable.a006, R.drawable.a007, R.drawable.a008, R.drawable.a009, R.drawable.a010,
                      R.drawable.a011, R.drawable.a012, R.drawable.a013, R.drawable.a014, R.drawable.a015,
                      R.drawable.a016, R.drawable.a017, R.drawable.a018
              };
          
              private GridView mGridView;
          
              @Override
              protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_main);
          
                  mGridView = ((GridView) findViewById(R.id.gridview));
                  mGridView.setChoiceMode(GridView.CHOICE_MODE_SINGLE);
                  mGridView.setAdapter(new BaseAdapter() {
                      @Override public int getCount() {
                          return IMAGES.length;
                      }
                      @Override public Object getItem(int position) {
                          return IMAGES[position];
                      }
                      @Override public long getItemId(int position) {
                          return IMAGES[position];
                      }
                      @Override public View getView(int position, View convertView, ViewGroup parent) {
                          if (convertView == null || !(convertView instanceof ImageView)) {
                              ImageView imageView = (ImageView) getLayoutInflater().inflate(R.layout.grid_view_item, parent, false);
                              imageView.setImageResource(IMAGES[position]);
                              convertView = imageView;
                          }
                          return convertView;
                      }
                  });
              }
          }
          
          1. 最后,您可以使用mGridView.getCheckedItemPosition()获取已检查项目的位置。
          2. 屏幕:

            selected item

答案 1 :(得分:0)

不确定这是否是您要寻找的,但是您可以尝试以下操作...

在您的<GridView>标签中

android:horizontalSpacing="1dp"
android:verticalSpacing="1dp"