如何在android中使用onlick事件删除gridview的最后一项

时间:2016-06-23 18:49:38

标签: java android gridview onclicklistener

我的问题是删除gridview中的最后一个元素,这个gridview有一个适配器,并且由它们的arraylist的内容控制。

我看到问题在于单击gridview的子元素,因为直接setOnItemClickListener的remove元素没问题。 这是我的代码:

//evento click para cada item del adaptador
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> m_parent, View o_view, int e_position, long e_id) {
        if(flag) {
            View view = o_view.findViewById(R.id.close_image);
            view.setTag(R.id.close_image, e_position);

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int posicion = (int) v.getTag(R.id.close_image);
                    gridAdapter.removeAt(posicion);
                }
            });
        }
    }
});

最后一个元素不会消失或者不会从设计中删除,仍然存在于arraylist中。 适配器:

public abstract class AdapterGridView extends ArrayAdapter {
    private Context context;
    private int layoutResourceId;
    private ArrayList data = new ArrayList();

    public AdapterGridView(Context context, int layoutResourceId, ArrayList data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int e_position, View convertView, ViewGroup parent) {
        if( convertView == null) {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            convertView = inflater.inflate(layoutResourceId, parent, false);
        }

        onEntrada(data.get(e_position), convertView);

        return convertView;
    }

    public boolean removeAt(int e_index) {
        Object o_out = data.remove(e_index);
        notifyDataSetChanged();
        return ( o_out != null );
    }

    public abstract void onEntrada(Object o_entrada, View o_view);
}

设计元素gridview

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:background="#fff"
            android:padding="5dp">
            <ImageView
                android:id="@+id/image"
                android:contentDescription="@string/app_name"
                android:src="@drawable/logo1"
                android:layout_gravity="center"
                android:layout_width="100dp"
                android:layout_height="100dp"/>
            <ImageView
                android:id="@+id/close_image"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_alignRight="@id/image"
                android:layout_alignEnd="@id/image"
                android:src="@drawable/delete2"
                android:visibility="gone"
                android:contentDescription="@string/app_name"/>
</RelativeLayout>

gridview属性

<GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/textFoot"
        android:layout_margin="5dp"
        android:columnWidth="100dp"
        android:drawSelectorOnTop="true"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="5dp"
        android:focusable="true"
        android:clickable="true"
        android:scrollbars="horizontal"
        android:layout_below="@id/head_directorio"/>

0 个答案:

没有答案