2016-05-31 48 views
0

我正在使用網格視圖來顯示使用Glide的圖像。我如何在網格中選中圖像?此外,我需要限制用戶只選擇一個圖像,如果用戶再次單擊一個圖像,則應取消選擇第一個圖像。如何在Grid View中點擊/選擇網格

這裏是我的xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 

    <GridView 
     android:id="@+id/galleryGridView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:columnWidth="280dp" 
     android:gravity="center" 
     android:horizontalSpacing="2dp" 
     android:numColumns="3" 
     android:padding="2dp" 
     android:stretchMode="columnWidth" 
     android:verticalSpacing="2dp" 
     android:listSelector="@drawable/selected_grid"> 
    </GridView> 

</RelativeLayout> 

和Java代碼

GridView gallery = (GridView) findViewById(R.id.galleryGridView); 

     gallery.setAdapter(new ImageAdapter(this)); 

     gallery.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, 
            int position, long arg3) { 

         // Help me here 

      } 
     }); 
    } 

適配器

private class ImageAdapter extends BaseAdapter { 

     /** The context. */ 
     private Activity context; 

     /** 
     * Instantiates a new image adapter. 
     * 
     * @param localContext 
     *   the local context 
     */ 
     public ImageAdapter(Activity localContext) { 
      context = localContext; 
      images = getAllShownImagesPath(context); 
     } 

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

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

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

     public View getView(final int position, View convertView, 
          ViewGroup parent) { 

      if (convertView == null) { 
       picturesView = new ImageView(context); 
       picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
       picturesView 
         .setLayoutParams(new GridView.LayoutParams(270, 270)); 

      } else { 
       picturesView = (ImageView) convertView; 
      } 

      Glide.with(context).load(images.get(position)) 
        .placeholder(R.mipmap.ic_launcher).centerCrop() 
        .into(picturesView); 

      return picturesView; 
     } 
+0

設置你的GridView的listSelector添加ImageAdapter – Pavya

+0

的代碼,你可以處理它裏面適配器 – mdDroid

+0

@ Pravin ImageAdapter添加。 – Ramesh

回答

0

你可以通過這樣的

gallery.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, 
           int position, long arg3) { 


     for(int i = 0; i < arg0.getChildCount(); i++) 
     { 
      if(i == position) 
       arg0.getChildAt(i).setBackgroundColor(Color.BLUE); 
      else 
       arg0.getChildAt(i).setBackgroundColor(Color.TRANSPARENT); 

     } 

     } 
    }); 
012管理

或第二方式只是做一個簡單的選擇對的GridView list_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="false" android:drawable="@color/yourcolor" /> 
    <item android:drawable="@color/transparent" /> 
</selector> 

和像

android:listSelector="@drawable/list_selector"

+0

的無論是工作方法:( – Ramesh

+0

究竟是什麼問題? –

+0

他們什麼也沒做,這不是什麼突出彩色的圖像背後越來越所以它是不可見的。對於GIF圖像,我可以看到應用的顏色。 – Ramesh