Gridview项目没有响应click

时间:2014-08-14 12:58:24

标签: android xml gridview

我尝试过对这个问题的所有类似答案都无济于事。我有一个嵌套在布局中的网格视图,其上有2个其他textview,我有一个成功填充网格视图的自定义适配器,但是我无法让网格项响应点击。我的XML如下所示,带有点击监听器的片段

Grid_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_parent_rounded_corner"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/ll_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/feed_item_margin"
    android:layout_marginRight="@dimen/feed_item_margin"
    android:layout_marginTop="@dimen/feed_item_margin"
    android:orientation="vertical"
    android:paddingBottom="5dp"
    android:paddingTop="5dp" >

    <com.android.volley.toolbox.NetworkImageView
        android:id="@+id/profileOfffer"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:scaleType="fitCenter" >
    </com.android.volley.toolbox.NetworkImageView>

    <LinearLayout
        android:id="@+id/bottom"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/names"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="@dimen/feed_item_profile_name"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/price"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#70B440"
            android:textSize="@dimen/feed_item_profile_name" />

    </LinearLayout>
</LinearLayout>

class_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F3F3F3"
android:orientation="vertical" 
android:descendantFocusability="blocksDescendants">

<TextView
    android:id="@+id/user_salute_"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:paddingTop="10dp"
    android:text="@string/header_share"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#000000" />

<TextView
    android:id="@+id/points_"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:paddingBottom="10dp"
    android:paddingTop="10dp"
    android:text="@string/sub_header_share"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#000000" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:contentDescription="@string/app_name"
    android:drawableLeft="@drawable/sale"
    android:paddingBottom="10dp"
    android:text="@string/offers"
    android:textColor="#3E9DE1" />

<GridView
    android:id="@+id/gridview_"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnWidth="100dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="2"
    android:scrollbars="none"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp" />

点击代码段:

 gridView.setAdapter(adapter);
            gridView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id)        {
 //Fragment transaction logic here
 Log.i("Test","Clicked position"+position);
} });

获取查看方法:

   @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    View vi=convertView;

    if (convertView == null){
        vi = inflater.inflate(R.layout.grid_home_item, parent, false);
    holder = new ViewHolder();
    holder.name = (TextView)vi.findViewById(R.id.names);
    holder.information = (TextView)vi.findViewById(R.id.price);
    holder.Pic = (NetworkImageView)vi.findViewById(R.id.profileOfffer);

    vi.setTag(holder);
 }
    else {
        holder = (ViewHolder) vi.getTag();
    }

    Offers_Item item = offersItems.get(position);
    holder.name.setFocusable(false);
    holder.name.setText(item.getOfferName());

    holder.Pic.setImageUrl(item.getImage(), imageLoader);
    holder.Pic.setFocusable(false);

  if (!TextUtils.isEmpty(item.getOfferValue())  && !(item.getOfferValue().contentEquals("0")) &&!(item.getOfferValue().contentEquals("0.00"))) {
        holder.information.setText("NOW! KES. "+item.getOfferValue());
        holder.information.setFocusable(false);
        holder.information.setVisibility(View.VISIBLE);
    } else {
        holder.information.setVisibility(View.GONE);
        holder.information.setFocusable(false);
    }




    return vi;
}

1 个答案:

答案 0 :(得分:1)

Grid_layout.xml中的ScrollView使用该事件。所以请处理它。