【发布时间】:2015-09-25 17:13:59
【问题描述】:
我有自定义适配器实现的网格视图。触摸网格视图项目时,会选择整个项目。我想在网格视图中选择不同的子项时执行不同的操作。
在下面的示例中,我在项目中有两个图像和一个文本视图。我想对触摸 textview、image view 执行不同的操作。
这可以在 android 中做到吗?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listrow"
android:layout_width="fill_parent"
android:layout_height="66dp"
android:background="@drawable/grid_color_selector"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="fill_horizontal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:focusable="false"
android:focusableInTouchMode="false"
android:orientation="horizontal" >
<ImageView
android:id="@+id/thumbimage"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="4dp"
android:layout_weight="1"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:scaleType="fitXY"
android:src="@drawable/icon" />
<TextView
android:id="@+id/txttitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_weight="44"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="English Channels"
android:textColor="@color/Black"
android:textSize="16sp" />
<ImageView
android:id="@+id/share"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="4dp"
android:layout_weight="1"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:scaleType="fitXY"
android:src="@drawable/share" />
</LinearLayout>
自定义的gridview实现是
public class GridViewAdapterImpl extends ArrayAdapter < String > {
private final Activity context;
private final String[] data;
private int resource;
public GridViewAdapterImpl(Activity context, int resource, String[] fileNames) {
super(context, resource, fileNames);
this.resource = resource;
this.context = context;
this.data = fileNames;
}
@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(resource, parent, false);
holder = new ViewHolder();
holder.imageTitle = (TextView) row.findViewById(R.id.txttitle);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
holder.imageTitle.setText(data[position]);
return row;
}
static class ViewHolder {
TextView imageTitle;
}
Gridview 调用是
GridView gridView = (GridView) findViewById(R.id.gridView);
GridViewAdapterImpl customGridAdapter = new GridViewAdapterImpl(this, R.layout.row_list_videos, this.getVideoFiles()); gridView.setAdapter(customGridAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { } });
【问题讨论】:
标签: android gridview android-gridview