获取GridView中单击项的位置

时间:2014-07-22 14:18:08

标签: android android-gridview onitemclicklistener

我正在尝试使用GridView创建一个图标选择器。我有活动的布局,网格行和适配器。我从资源数组中获取图像。当我现在点击图片时,我只是想得到它的位置,但没有任何反应,我没有得到Toast的消息。在这里寻找不同的解决方案,但现在没有任何效果。

这是我的活动XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/app_bg" >

<GridView
    android:id="@+id/gv_icons"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="4dp"
    android:columnWidth="70dp"
    android:gravity="center"
    android:numColumns="auto_fit"
    android:verticalSpacing="4dp"
    android:drawSelectorOnTop="true"
    android:stretchMode="columnWidth"
    android:clickable="false"
    android:focusable="false" >
</GridView>

</LinearLayout>

网格行:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@drawable/grid_color_selector"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:padding="5dp" >

<ImageView
    android:id="@+id/image"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:scaleType="fitCenter"
    android:focusable="false"
    android:clickable="false" >
</ImageView>

</LinearLayout>

适配器:

public class GridView_Adapter extends ArrayAdapter {
private Context context;
private int layout_resource_id;
private ArrayList data = new ArrayList();

public GridView_Adapter(Context context, int layout_resource_id,
        ArrayList data) {
    super(context, layout_resource_id, data);
    this.layout_resource_id = layout_resource_id;
    this.context = context;
    this.data = data;
}

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

    if (row == null) {
        holder = new ViewHolder();
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layout_resource_id, parent, false);
        holder.image = (ImageView) row.findViewById(R.id.image);
        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }

    Trip_Icon icon = (Trip_Icon) data.get(position);
    holder.image.setImageBitmap(icon.getImage());
    return row;
}

static class ViewHolder {
    ImageView image;
}
}

活动:

public class Activity_Icon_Picker extends Activity {

private GridView grid_view;
private GridView_Adapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_icon_picker);

    grid_view = (GridView) findViewById(R.id.gv_icons);
    adapter = new GridView_Adapter(this, R.layout.row_grid, get_icons());
    grid_view.setAdapter(adapter);

    grid_view.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
            Toast.makeText(Activity_Icon_Picker.this, position + "#Selected",
                    Toast.LENGTH_SHORT).show();
        }
    });
}

private ArrayList get_icons() {
    final ArrayList arr_trip_icons = new ArrayList();
    TypedArray imgs = getResources().obtainTypedArray(R.array.trip_icons_blac);
    for (int i = 0; i < imgs.length(); i++) {
        Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),
                imgs.getResourceId(i, -1));
        arr_trip_icons.add(new Trip_Icon(bitmap));
    }
    imgs.recycle();
    return arr_trip_icons;
}
}

我为什么不在这里收到Toast留言?

1 个答案:

答案 0 :(得分:2)

更改您的父LinearLayout

的此属性

android:clickable="true"
android:focusable="true"

android:clickable="false"
android:focusable="false"

grid_row.xml档案中。

同时删除此

android:clickable="false"
android:focusable="false"

来自GridView