我有一个ListView
包含布局文件entry.xml给出的条目我想从ImageView
可见性设置开始View.GONE
并将其onItemLongClick
更改为View.VISIBLE
. 长时间单击的ListView
条目将保持突出显示。
然后我想添加一个onClickListener
(ImageView
虽然它是可见的),以便ListView
可以通过点击图像来删除单击的条目。
OnClickListener
条目上还需要有第二个,ListView
以便如果长时间单击并突出显示它可以通过(短)单击条目来撤消。这将恢复ImageView
到View.GONE
.
我怎样才能做到这一点?
列表视图.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
入口.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dp" >
<ImageView
android:id="@+id/entry_deleteicon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:baselineAlignBottom="false"
android:maxHeight="40dp"
android:maxWidth="45dp"
android:src="@drawable/delete"
android:visibility="gone" />
<TextView
android:id="@+id/entry_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="text" />
</RelativeLayout>
我的自定义适配器
private class SaveAdapter extends ArrayAdapter<String> {
private int resourceId;
private SaveHolder[] holder;
public SaveAdapter(Context context, int resource) {
super(context, resource);
this.resourceId=resource;
this.holder = new SaveHolder[5];
}
@Override
public int getCount() {
return 5;
}
@Override
public View getView(final int position, View row, ViewGroup parent) {
holder[position] = null;
if(row==null){
LayoutInflater inflater = getActivity().getLayoutInflater();
row = inflater.inflate(resourceId, parent, false);
holder[position] = new SaveHolder();
holder[position].textName = (TextView) row.findViewById(R.id.entry_name);
holder[position].iconDelete = (ImageView) row.findViewById(R.id.entry_deleteicon);
row.setTag(holder[position]);
}else{
holder[position] = (SaveHolder) row.getTag();
}
holder[position].textName.setText("name");
return row;
}
}
static class SaveHolder{
TextView textName;
ImageView iconDelete;
}