我有一个ListView
,其中包含布局文件 entry.xml 给出的条目我想以ImageView
可见性设置为View.GONE
和{{ 1}}将其更改为onItemLongClick
。长按的View.VISIBLE
条目将保持突出显示。
然后,我想向ListView
添加onClickListener
(虽然可见),以便点按图片即可删除点击的ImageView
条目。
ListView
条目还需要第二个OnClickListener
,这样如果已经长时间点击并突出显示,则可以通过(短)点击该条目撤消。这会将ListView
恢复为ImageView
。
我如何实现这一目标?
listview.xml
View.GONE
entry.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>
我的自定义适配器
<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>
答案 0 :(得分:3)
试试这个:
private ImageView tmpImageView;
...
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long arg3){
tmpImageView = (ImageView) view.findViewById(R.id.YOUR_IMAGEVIEW_ID);
tmpImageView.setVisibility(View.VISIBLE);
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int arg2, long arg3){
tmpImageView.setVisibility(View.GONE);
}
...
希望这对你有用......
答案 1 :(得分:2)
添加OnItemLongClickListener ListView
以处理ImageView
同样添加OnItemClickListener ListView
来处理您想要的功能。
同样添加OnClickListener ImageView
并处理删除项目。您可以使用自定义Adapter
并覆盖解释的getView方法here
答案 2 :(得分:1)
Update your SaveAdapter like this.
->Implement listener for imageview.
->Add visible and hide function for imageview.
private class SaveAdapter extends ArrayAdapter<String> {
private int resourceId;
private SaveHolder[] holder;
int highLightedItemNumber = -1;
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();
}
if(highlightedItemNumber == position)
{
holder[position].iconDelete.setVisibility(View.VISIBLE);
}
else
{
holder[position].iconDelete.setVisibility(View.GONE);
}
holder[position].textName.setText("name");
holder[position].setOnClickListener(new OnClickListener(){
public void onClick(View view)
{
//Perform your delete operation here;
}
});
return row;
}
}
public void highlightImageView(int itemNumber)
{
highlightedItemPosition = itemNumber;
notifyDataSetChanged();
}
public void disableHighlightedItem()
{
highlightedItemPosition = -1;
notifyDataSetChanged();
}
static class SaveHolder{
TextView textName;
ImageView iconDelete;
}
}
现在在mainActivity中,您将创建listView实例。
ListView listview = (ListView) findViewById(R.id.yourlistviewId);
final SaveAdapter adapter = new SaveAdapter(this, resource); //your resource id.
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long id) {
adapter.disableHighlightedItem();
}
});
listview.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
adapter.highlightImageView(position);
}
});
答案 3 :(得分:0)
长按listitem会使图片可见,需要突出显示该Listitem(更改项目的背景) 我认为你不会在本节中遇到问题
onckick on image将删除该列表项。(这在适配器类中是可管理的)
为此你需要从你正在发送适配器类的arraylist中删除该项
arrayList.remove(position);
notifyDataSetChanged();