从列表视图中删除项目

时间:2018-09-24 12:19:32

标签: android listview

我有一个列表视图,用于在Web视图中显示通知。我添加了一个imagebutton来删除该通知。我在xml文件中设置了onclick函数以删除该视图,但是在删除后留下空白,我使用下面给出的这种方法,我的XML文件是`

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LayoutNotifi"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingTop="7dp"
    android:paddingBottom="7dp"
    android:padding="5dp">
     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/Notification_ListView">
        <TextView
            android:id="@+id/tvNotifi_Name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="normal"
            android:textColor="#ffffff"
            android:textSize="14sp"
            android:maxWidth="280dp"
            android:minWidth="310dp"
            android:layout_marginRight="1dp"
            android:layout_marginBottom="2dp"
            />


        <ImageButton
            android:id="@+id/notification_delete_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/close"
            android:onClick="DeleteNotification"
            android:clickable="true"
            android:minHeight="10dp"
            android:paddingBottom="2dp"
            />
    </LinearLayout>
</LinearLayout>

` notification_delete_button上的onclick函数

  View viewp=findViewById(R.id.LayoutNotifi);
        view= (View) view.getParent();
        view= (View) view.getParent();
        view.setVisibility(view.GONE);

2 个答案:

答案 0 :(得分:1)

我已经解决了这个问题, 我是在notificationAdapter的getView函数中编写的,并且运行良好。

deleteButton=view.findViewById(R.id.notification_delete_button);
         deleteButton.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 listNotifi.remove(position);
                 notifyDataSetChanged();
             }
         });

答案 1 :(得分:0)

尝试使用VIEW.GONE

if (getParent() instanceof View)
    ((View) getParent()).invalidate();