gpt4 book ai didi

java - setOnItemClickListener 不适用于 ArrayAdapter

转载 作者:行者123 更新时间:2023-11-29 21:21:37 24 4
gpt4 key购买 nike

当我尝试点击列表中的项目时,它不会通过 onItemClick 方法。

GridViewAdapter.java

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;
}
}

主 Activity .java

public class MainActivity extends Activity {
private GridView gridView;
private GridViewAdapter customGridAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView);
customGridAdapter = new GridViewAdapter(this, R.layout.row_grid, getData());
gridView.setAdapter(customGridAdapter);
Log.d("yyoyoyoyo", "jhgvkbkhbjkhbj");
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(MainActivity.this, position + "#Selected",
Toast.LENGTH_SHORT).show();
}
});
}

private ArrayList<ImageItem> getData() {
final ArrayList<ImageItem> imageItems = new ArrayList<ImageItem>();
// retrieve String drawable array
TypedArray imgs = getResources().obtainTypedArray(R.array.image_ids);
for (int i = 0; i < imgs.length(); i++) {
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),
imgs.getResourceId(i, -1));
imageItems.add(new ImageItem(bitmap, "Image#" + i));
}
return imageItems;
}

}

行网格.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="false"
android:focusableInTouchMode="false"
android:descendantFocusability="blocksDescendants"
>

<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:focusable="false"
android:focusableInTouchMode="false"
>
</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"
android:focusable="false"
android:focusableInTouchMode="false"
>
</TextView>

activity_main.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:background="#f0f0f0"
android:clickable="false"
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:gravity="center"
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
android:drawSelectorOnTop="true"
android:stretchMode="columnWidth"
android:descendantFocusability="blocksDescendants"
>
</GridView>

</RelativeLayout>

我已经在 StackOverFlow 上尝试了大多数关于这个主题的解决方案,但没有一个对我有用。此应用程序列出所有图像,但当您单击 onItemClick 时,不会触发。可能我的代码有问题。

谢谢!!

最佳答案

删除 row_grid.xml 中的 android:clickable="true"。并在 activity_main.xml

中添加 android:clickable="true"

同时删除

android:focusable="false" 
android:focusableInTouchMode="false"

由于 row_grid.xml 是可点击的,它会阻止网格的 onclick 监听器响应。

关于java - setOnItemClickListener 不适用于 ArrayAdapter<ImageView>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20658641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com