我有一个自定义列表适配器。这里是:
public class FilesAdapter extends ArrayAdapter<PutioFileLayout> {
Context context;
int layoutResourceId;
List<PutioFileLayout> data = null;
public FilesAdapter(Context context, int layoutResourceId, List<PutioFileLayout> 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;
FileHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new FileHolder();
holder.textName = (TextView) row.findViewById(R.id.text_fileListName);
holder.textDescription = (TextView) row.findViewById(R.id.text_fileListDesc);
holder.imgIcon = (ImageView) row.findViewById(R.id.img_fileIcon);
row.setTag(holder);
} else {
holder = (FileHolder) row.getTag();
}
PutioFileLayout file = data.get(position);
holder.textName.setText(file.name);
holder.textDescription.setText(file.description);
holder.imgIcon.setImageResource(file.icon);
return row;
}
static class FileHolder {
TextView textName;
TextView textDescription;
ImageView imgIcon;
}
}
很短很甜。我Spinner
在每一行的布局中都有 s,我希望用户能够单击它们,并为每个项目获取一个上下文菜单。如何在我的适配器中实现这一点?
我的row.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="64dp" >
<ImageView
android:id="@+id/img_fileIcon"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:scaleType="fitCenter"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@id/img_fileIcon"
android:orientation="vertical" >
<TextView
android:id="@+id/text_fileListName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="12dp"
android:ellipsize="end"
android:maxLines="1"
android:text="File name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<FrameLayout
android:id="@+id/descriptionFrame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="30dp" >
<TextView
android:id="@+id/text_fileListDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="File description" />
</FrameLayout>
</LinearLayout>
<Spinner
android:id="@+id/item_fileSpinner"
android:layout_width="44dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/spinner_background_ab_putio" />
</RelativeLayout>