gridview中的第一项不显示选定状态

时间:2014-06-11 12:20:56

标签: android gridview

我有一个gridview如下

<GridView
    android:id="@+id/gridView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_gravity="center"
    android:horizontalSpacing="1dp"
    android:listSelector="#00000000"
    android:numColumns="3" />

这是在oncreate中

gridView = (GridView) mRootView.findViewById(R.id.gridView);
gridView.setOnItemClickListener(defaultItemClickListener);
gridView.setOnItemLongClickListener(defaultItemLongClickListener);

这是我的适配器getView方法

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Viewholder vh = null;
    if (convertView == null) {
        convertView = inflater.inflate(resource, parent, false);
        vh = new Viewholder();
        vh.icon = (ImageView) convertView.findViewById(R.id.iv_icon);
            vh.title = (TextView) convertView.findViewById(R.id.tv_title);
        convertView.setTag(vh);
    } else {
        vh = (Viewholder) convertView.getTag();
    }
            Item item = getItem(position);
       if (item.getIcon() != null) {
        vh.icon.setImageDrawable(item.getIcon());
    }
    if (item.getTitle() != null) {
        vh.title.setText(item.getTitle());
    }

    return convertView;
}

图标的图像drawable是由此选择器

定义的StateListDrawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/ic_action_settings_mail" android:state_enabled="false"></item>
    <item android:drawable="@drawable/ic_action_settings_mail_pressed" android:state_enabled="true" android:state_pressed="true"></item>
    <item android:drawable="@drawable/ic_action_settings_mail" android:state_enabled="true" android:state_pressed="false"></item>
    <item android:drawable="@drawable/ic_action_settings_mail"></item>

</selector>

gridview,当我点击任何项目(我在给定的gridview中有6个项目)不在位置0时,选择器工作得很好

当我点击位置0单元格时,选择器不起作用。

0位置单元格有什么特别之处,为什么我的选择器不起作用呢?

xml of 1 item

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingBottom="5dp"
    android:paddingTop="5dp" >

    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/iv_icon"
        android:layout_centerHorizontal="true"
        android:gravity="center_horizontal"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:text="Chart"
        android:textColor="#fff"
        android:textSize="16sp" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

尝试使用此方法列出gridview数据

第1步:布局

<GridView
    android:id="@+id/gridView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:numColumns="2"
    android:smoothScrollbar="true"
    android:stretchMode="columnWidth"
    android:verticalSpacing="5dip"
    android:horizontalSpacing="5dip"
    android:scrollbarThumbVertical="@drawable/scrollbar"
    android:scrollbarThumbHorizontal="@drawable/scrollbar"
    android:listSelector="@android:color/transparent"
    android:scrollbarStyle="outsideOverlay"
    android:drawSelectorOnTop="true"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dip"
    />

第2步:onCreateView

   GridView gridView = (GridView) view.findViewById(R.id.gridView);

第3步:onCreateView中的SetAdapter

 Adapter mGridAdapter = new Adapter(getActivity().getApplicationContext(), mUsers, this);
    gridView.setAdapter(mGridAdapter);

步骤4:分配值需要列出例如:mUsers // Sample

  Map<String, String> tempMap = new HashMap<String, String>();
  tempMap.put(Reference.TITLE, UserTitle);
  mUsers.put(Integer.toString(mUsers.size()), tempMap);

步骤5:在适配器

private Context mContext;
private Map<String, Map<String, String>> mData = new HashMap<String, Map<String, String>>();

public TapAdapter(Context context, Map<String, Map<String, String>> catalog, Fragment fragment) {

    mContext = context;
    mData = catalog;
}

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

  if (convertView == null) {

        holder = new ViewHolder();

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.adapter_user_list, null);


        holder.sTxtTitle = mViewUtils.createTextView(convertView.findViewById(R.id.item_text_Username), true);

        convertView.setTag(holder);


    } else {
        holder = (ViewHolder) convertView.getTag();

    }

    if (data != null) {

        convertView.setTag(R.string.USER_NAME, data.get(Reference.TAP_USER_TITLE));


        holder.sTxtTitle.setText(data.get(Reference.TAP_USER_TITLE));


    }

    return convertView;

}


    Map<String, String> data = mData.get(Integer.toString(position));

    if (data != null) {
       convertView.setTag(R.string.USER_NAME, data.get(Reference.TAP_USER_TITLE));
       holder.sTapWrapper.setTag(R.string.USER_NAME,data.get(Reference.TAP_USER_TITLE));
    }
}

答案 1 :(得分:0)

尽管我记得我曾经有过与ListView类似的问题.. getView被调用几次并缓存数据,如果你没有其他的东西有时会导致奇怪的事情。所以我在评论中写下了你遗漏的其他内容。 我不确定,但有很多,因为我理解item.getIcon()将设置您创建的选择器..请尝试这个并更新我

@Override
public View getView(int position, View convertView, ViewGroup parent) {
Viewholder vh = null;
if (convertView == null) {
    convertView = inflater.inflate(resource, parent, false);
    vh = new Viewholder();
    vh.icon = (ImageView) convertView.findViewById(R.id.iv_icon);
        vh.title = (TextView) convertView.findViewById(R.id.tv_title);
    convertView.setTag(vh);
} else {
    vh = (Viewholder) convertView.getTag();
}
        Item item = getItem(position);
if (item.getIcon() != null) {
    vh.icon.setImageDrawable(item.getIcon());
} else {
   ///Put you else code here
}
if (item.getTitle() != null) {
    vh.title.setText(item.getTitle());
}  else {
   ///Put you else code here
}

return convertView;
}