2014-06-16 151 views
0

我正在嘗試高亮背景的一個GridView元素。無法選擇GridView項目

Activity.java:

private CustomAdapter ca; 

public void onCreate(Bundle _) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_layout); 

    // ... 

    GridView gw = (GridView) findViewById(R.id.gridview); 
    gw.setAdapter(ca = new CustomAdapter(this)); 
    gw.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      // attempt 1: 
      view.setSelection(true); 

      // attempt 2: 
      gw.setSelection(position); 

      // attempt 3: 
      gw.setItemChecked(position, true); 

      // tried everything WITH and WITHOUT this: 
      ca.notifyDataSetChanged(); 
     } 
    }); 
} 

CustomAdapter.java

public class CustomAdapter extends BaseAdapter { 

    private LayoutInflater inflater; 

    public CustomAdapter(Context c) { 
     inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     // Download items async, and call... 
     notifyDataSetChanged(); 
     // ... in a callback 
    } 

    // getItem(), getCount() and getItemId() implemented here 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View view; 
     if(convertView!=null) view = convertView; 
     else { 
      view = inflater.inflate(R.layout.grid_item, parent, false); 

      // calculate height so that an item is a square 
      int height = parent.getHeight(); 
      if(height>0) { 
      ViewGroup.LayoutParams params = view.getLayoutParams(); 
      params.height = height/3; 
      } 
     } 

     ImageView image = (ImageView) view.findViewById(R.id.image); 
     image.setImageBitmap(getItemImageBitmap(position)); 
     return view; 
    } 
} 

activity_layout.xml

<GridView android:id="@+id/gridview" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:stretchMode="columnWidth" 
      android:numColumns="3" 


      android:drawSelectorOnTop="false" 
      android:choiceMode="singleChoice" 
      android:listSelector="@drawable/grid_selector" 

      /> 
<!-- I've tried any combination of those last three params I could think of --> 

grid_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<ImageView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/image" 
    android:background="@drawable/grid_selector" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

grid_selector.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="@android:color/holo_red_dark" /> 
    <item android:drawable="@android:color/transparent" /> 
</selector> 

我試圖把grid_selector作爲項目背景或在GridView控件listSelector屬性。

我的目標是讓一個項目突出顯示在用戶點擊(和之後,直到其他項目被選中)。

+0

查看setItemChecked的文檔。 – njzk2

回答

0

所以對我來說是不可思議的組合是:

  1. gw.setItemChecked(position, true);Activity.java是不夠的,
  2. 沒有改變CustomAdapter.java
  3. listSelector="@drawable/grid_selector"必須從activity_layout.xml除去
  4. grid_item.xml必須背景
  5. 使用android:state_checked="true",而不是在grid_selector.xmlandroid:state_selected="true"
+0

與setChoiceMode()網格應該能夠處理檢查本身。 – njzk2

+0

我認爲自定義適配器會阻止它。 – meeDamian

+0

是的,你可能需要網格項的根來處理選中的狀態。 – njzk2