我试图让setOnItemClickListener
工作,但我无法让它工作。我是新手android开发者。
LocationGalleryActivity.java
:
import java.util.ArrayList;
import java.util.Vector;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
public class LocationGalleryActivity extends ActionBarActivity {
private GridView gridView;
private GridViewAdapter customGridAdapter;
private Vector<PhotoModel> vphotos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_gallery);
//Conectamos a la base de datos
LocationsSQLite dbloc = new LocationsSQLite(this, "DBLocations.db",
null, 1);
//Obtenemos el vector de imagenes
vphotos = dbloc.getPhotos();
gridView = (GridView) findViewById(R.id.gridView);
customGridAdapter = new GridViewAdapter(this, R.layout.row_grid_image,
getData());
gridView.setAdapter(customGridAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Log.i("GridView", "Se ha pulsado");
/*Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(
"content://media/internal/images/media"));
startActivity(intent); */
Toast.makeText(getApplicationContext(), position + "#Selected",
Toast.LENGTH_SHORT).show();
}
});
}
/**
* Obtenemos las imagenes para mostrar en el GridLayout
*
* @return Array de ImageItems (Imagene y su nombre)
*/
private ArrayList<ImageItem> getData() {
final ArrayList<ImageItem> imageItems = new ArrayList<ImageItem>();
// Vamos obteniendo las imagenes del array
for (int i = 0; i < vphotos.size(); i++) {
PhotoModel p = vphotos.get(i);
//Obtenemos la miniatura de la imagen
Bitmap bitmap = BitmapFactory.decodeFile(p.getPath()
+ ".thumbnails/" + p.getName());
imageItems.add(new ImageItem(bitmap, p.getName()));
}
return imageItems;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.location_gallery, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_location_gallery.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"
android:descendantFocusability="blocksDescendants"
tools:context=".MainActivity" >
<GridView
android:id="@+id/gridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:columnWidth="100dp"
android:drawSelectorOnTop="true"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp" >
</GridView>
</RelativeLayout>
GridViewAdapter.java:
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class GridViewAdapter extends ArrayAdapter<ImageItem> {
private Context context;
private int layoutResourceId;
private ArrayList<ImageItem> data = new ArrayList<ImageItem>();
public GridViewAdapter(Context context, int layoutResourceId,
ArrayList<ImageItem> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.imageTitle = (TextView) row.findViewById(R.id.text);
holder.image = (ImageView) row.findViewById(R.id.image);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
ImageItem item = data.get(position);
holder.imageTitle.setText(item.getTitle());
holder.image.setImageBitmap(item.getImage());
return row;
}
static class ViewHolder {
TextView imageTitle;
ImageView image;
}
}
row_grid_image.xml:
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
android:padding="5dp"
android:clickable="true"
android:background="@drawable/grid_color_selector"
android:focusable="true">
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp" >
</ImageView>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:textSize="12sp" >
</TextView>
</LinearLayout>
带来了一段时间,无法让它发挥作用。
答案 0 :(得分:0)
尝试删除android:clickable =&#34; true&#34;来自row_grid_image.xml中的LinearLayout
答案 1 :(得分:0)
我刚刚解决了对row_click_image.xml的更改:
android:clickable="true"
android:focusable="true"
为:
android:clickable="false"
android:focusable="false"