在我的应用程序中,我使用自定义网格视图显示带有文本的图像,但是当我使用setOnItemClick(...)单击特定网格项时,它无法调用,我的网格视图在xml文件中跟随,
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="100dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
在我的java文件活动中,
gridView = (GridView) findViewById(R.id.gridview);
customGridAdapter = new GridViewAdapter(this, R.layout.row_grid,
getData());
gridView.setAdapter(customGridAdapter);
System.out.println("call start");
gridView.setOnItemClickListener(new OnItemClickListener() { // it not call
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(GridCameraPreviewTestActivity.this,
position + "Selected", Toast.LENGTH_SHORT).show();
System.out.println("on grid click");
}
}
}
});
编辑:
我的row_grid.xml如下,
<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>
编辑2:
我的整个GridCameraPreviewTestActivity如下:
public class GridCameraPreviewTestActivity extends Activity implements
AdapterView.OnItemSelectedListener, OnClickListener,
OnItemClickListener {
private ResizableCameraPreview mPreview;
private ArrayAdapter<String> mAdapter;
private RelativeLayout mLayout;
private int mCameraId = 0;
private GridView gridView;
private GridViewAdapter customGridAdapter;
ApplicationInfo packageInfos;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridtest);
// Spinner for preview sizes
Spinner spinnerSize = (Spinner) findViewById(R.id.spinner_size);
mAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item);
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerSize.setAdapter(mAdapter);
spinnerSize.setOnItemSelectedListener(this);
// Spinner for camera ID
Spinner spinnerCamera = (Spinner) findViewById(R.id.spinner_camera);
ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCamera.setAdapter(adapter);
spinnerCamera.setOnItemSelectedListener(this);
adapter.add("0");
adapter.add("1");
adapter.add("2");
mLayout = (RelativeLayout) findViewById(R.id.layout);
// start
gridView = (GridView) findViewById(R.id.gridview);
customGridAdapter = new GridViewAdapter(this, R.layout.row_grid,
getData());
gridView.setAdapter(customGridAdapter);
System.out.println("call start");
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
System.out.println("on grid click");
Toast.makeText(GridCameraPreviewTestActivity.this,
position + "Selected", Toast.LENGTH_SHORT).show();
}
});
System.out.println("call over");
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
Log.w("CameraPreviewTestActivity", "onItemSelected invoked");
Log.w("CameraPreviewTestActivity", "position: " + position);
Log.w("CameraPreviewTestActivity", "parent.getId(): " + parent.getId());
switch (parent.getId()) {
case R.id.spinner_size:
Rect rect = new Rect();
mLayout.getDrawingRect(rect);
if (0 == position) { // "Auto" selected
mPreview.surfaceChanged(null, 0, rect.width(), rect.height());
} else {
mPreview.setPreviewSize(position - 1, rect.width(),
rect.height());
}
break;
case R.id.spinner_camera:
mPreview.stop();
mLayout.removeView(mPreview);
mCameraId = position;
createCameraPreview();
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// do nothing
}
@Override
protected void onResume() {
super.onResume();
createCameraPreview();
}
@Override
protected void onPause() {
super.onPause();
mPreview.stop();
mLayout.removeView(mPreview);
mPreview = null;
}
private void createCameraPreview() {
// Set the second argument by your choice.
// Usually, 0 for back-facing camera, 1 for front-facing camera.
// If the OS is pre-gingerbreak, this does not have any effect.
mPreview = new ResizableCameraPreview(this, mCameraId,
CameraPreview.LayoutMode.FitToParent, false);
LayoutParams previewLayoutParams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mLayout.addView(mPreview, 0, previewLayoutParams);
mAdapter.clear();
mAdapter.add("Auto");
List<Camera.Size> sizes = mPreview.getSupportedPreivewSizes();
for (Camera.Size size : sizes) {
mAdapter.add(size.width + " x " + size.height);
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
private ArrayList<ImageItem> getData() {
// other code here
}
return imageItems;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
}
}
任何帮助我解决这个问题, 谢谢你提前。
答案 0 :(得分:1)
删除android:focusable="true"
和android:clickable="true"
。如果行焦点布局onItemClick
中有焦点子节点未被触发。试试这个
<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:background="@drawable/grid_color_selector" >
<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>
答案 1 :(得分:0)
试试这个..
将android:descendantFocusability="blocksDescendants"
添加到row_grid.xml
答案 2 :(得分:-1)
尝试使用
gridView.setClickable(true);
前
gridView.setOnItemClickListener(new OnItemClickListener() { // it not call