使用带有ListView的OnItemLongClick监听器

时间:2013-11-08 11:14:34

标签: android listview android-listview onclicklistener

我有一个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>

4 个答案:

答案 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)

  1. 添加OnItemLongClickListener ListView以处理ImageView

    的可见性
  2. 同样添加OnItemClickListener ListView来处理您想要的功能。

  3. 同样添加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)

  1. 长按listitem会使图片可见,需要突出显示该Listitem(更改项目的背景) 我认为你不会在本节中遇到问题

  2. onckick on image将删除该列表项。(这在适配器类中是可管理的)

  3. 为此你需要从你正在发送适配器类的arraylist中删除该项

    arrayList.remove(position);
    notifyDataSetChanged();
    
    1. 点击listitem将改变旧状态下的项目。为了维护所有这个高亮和非高亮场景,你需要维护一个ArrayList.in开始填充0等于列表大小的arraylist。长按点击更改arraylist postion如果值为1,则单击更改为1到0.