以下是我在gridviews中动态创建ImageButtons
的代码。当我运行活动时,ImageButtons
无法点击。我尝试添加
android:descendantFocusability="blocksDescendants"
到xml文件,但这没有解决问题。
编辑:我更新了适用于ImageViews的适配器,而不是ImageButtons,但问题仍然存在。
这是我的自定义适配器:
package com.example.myname.myproject;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
/**
* Created by myname on 3/31/17.
*/
public class ImageArrayAdapter extends BaseAdapter {
Context context;
int[] currentResources;
String[] currentIcons;
public ImageArrayAdapter(Context context, String[] currentIcons, int[] currentResources){
ImageArrayAdapter.this.context = context;
ImageArrayAdapter.this.currentIcons = currentIcons;
ImageArrayAdapter.this.currentResources = currentResources;
}
@Override
public int getCount() {
return currentResources.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(300, 300));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
imageView.setBackgroundColor(Color.TRANSPARENT);
} else {
imageView = (ImageView) convertView;
}
imageView.setClickable(true);
imageView.setImageResource(currentResources[position]);
return imageView;
}
}
以下是 感谢您的帮助。GridView
的<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.myname.myproject.AdminControlTabbed$PlaceholderFragment">
<GridView
android:id="@+id/templateGrid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:clickable="true"
android:descendantFocusability="blocksDescendants"
/>
答案 0 :(得分:2)
为什么不使用ImageViews代替ImageButtons。然后,您必须仅在已初始化它的活动类中为GridView设置OnItemClickListener。将适配器连接到它后,您可以附加一个clickListener,如下所示:
myGridView.setAdapter(this, currentIconsArray, currentResourcesArray);
myGridViewsetOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//code for handling onClick event
}
});
答案 1 :(得分:1)
Activity.dispatchTouchEvent(MotionEvent)
将其发送给其他听众