Android 仿QQ 滑动删除

AndroidSwipeLayout是一个很强大的 Swipe Layout,和SwipeListView相比, 它不局限于ListView.

特性

  • 轻松的整合ListView GridView ViewGroup
  •  能回调 onOpen(), onUpdate
  •  可以通知子元素
  •  可以处理复杂的情况

下载

使用Androidstudio的直接在gradle下

下载完之后我们就可以直接使用了。

首先我们看看我们的列表的item XML里面如何使用的。

<?xml version="1.0" encoding="utf-8"?>
<com.daimajia.swipe.SwipeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:swipe="http://schemas.android.com/apk/res-auto"
    android:id="@+id/swipe"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    swipe:leftEdgeSwipeOffset="0dp"
    swipe:rightEdgeSwipeOffset="0dp">

    <RelativeLayout
        android:id="@+id/bottom_wrapper"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:background="#278D4C"
        android:gravity="center"
        android:weightSum="1">

        <ImageView
            android:id="@+id/delete"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:src="@mipmap/icon_delete" />
        <!--What you want to show-->
    </RelativeLayout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp">

            <LinearLayout
                android:id="@+id/ll_real_estate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp">

                <ImageView
                    android:id="@+id/ib_real_estate_check"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:src="@mipmap/contentbox_off" />
            </LinearLayout>

            <RelativeLayout
                android:id="@+id/rl_add_my_property_item_select"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_toRightOf="@+id/ll_real_estate">

                <FrameLayout
                    android:id="@+id/fl_real_estate"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">

                        <ImageView
                            android:id="@+id/iv_real_estate_image"
                            android:layout_width="80dp"
                            android:layout_height="80dp"
                            android:scaleType="fitXY"
                            android:src="@mipmap/detail_bg_mask" />

                        <TextView
                            android:id="@+id/tv_real_estate_property_name"
                            android:layout_width="80dp"
                            android:layout_height="wrap_content"
                            android:layout_alignBottom="@+id/iv_real_estate_image"
                            android:background="@color/new_detail_share_green"
                            android:gravity="center"
                            android:paddingBottom="5dp"
                            android:paddingTop="5dp"
                            android:singleLine="true"
                            android:text="物业名称"
                            android:textColor="@color/white"
                            android:textSize="13sp" />
                    </RelativeLayout>
                </FrameLayout>

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@+id/fl_real_estate">

                    <TextView
                        android:layout_marginTop="2dp"
                        android:id="@+id/tv_real_estate_title"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="left"
                        android:paddingLeft="10dp"
                        android:singleLine="true"
                        android:text="title"
                        android:textColor="@color/new_detail_share_green"
                        android:textSize="15sp"
                        android:textStyle="bold" />

                    <TextView
                        android:layout_marginTop="2dp"
                        android:id="@+id/tv_real_estate_region"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/tv_real_estate_title"
                        android:gravity="left"
                        android:paddingLeft="10dp"
                        android:singleLine="true"
                        android:text="address"
                        android:textColor="#6C6C6C"
                        android:textSize="13sp"
                        android:textStyle="bold|italic" />

                    <TextView
                        android:layout_marginTop="2dp"
                        android:id="@+id/tv_real_estate_area"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/tv_real_estate_region"
                        android:gravity="left"
                        android:textColor="@color/gray"
                        android:paddingLeft="10dp"
                        android:singleLine="true"
                        android:text="area"
                        android:textSize="12sp" />

                    <TextView
                        android:layout_marginTop="2dp"
                        android:id="@+id/tv_real_estate_price"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/tv_real_estate_region"
                        android:layout_marginLeft="20dp"
                        android:layout_toRightOf="@+id/tv_real_estate_area"
                        android:gravity="left"
                        android:singleLine="true"
                        android:textColor="@color/gray"
                        android:text="price"
                        android:textSize="12sp" />

                    <TextView
                        android:layout_marginBottom="5dp"
                        android:layout_marginTop="2dp"
                        android:id="@+id/tv_real_estate_mop"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/tv_real_estate_price"
                        android:gravity="left"
                        android:paddingLeft="10dp"
                        android:singleLine="true"
                        android:text="mop"
                        android:textColor="@color/new_detail_share_green"
                        android:textSize="15sp" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/tv_real_estate_mop"
                        android:background="@color/main_page_column_bg"
                        android:orientation="horizontal">

                        <LinearLayout
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.8">


                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:gravity="left"
                                android:paddingBottom="5dp"
                                android:paddingLeft="10dp"
                                android:paddingTop="5dp"
                                android:singleLine="true"
                                android:text="樓層"
                                android:textColor="@color/gray"
                                android:textSize="12sp" />

                            <TextView
                                android:textColor="@color/black"
                                android:id="@+id/estate_floor"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:gravity="left"
                                android:paddingBottom="5dp"
                                android:paddingLeft="2dp"
                                android:paddingTop="5dp"
                                android:singleLine="true"
                                android:text=""
                                android:textSize="15sp" />
                        </LinearLayout>

                        <LinearLayout
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="1.5">

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginLeft="10dp"
                                android:gravity="left"
                                android:paddingBottom="5dp"
                                android:paddingTop="5dp"
                                android:singleLine="true"
                                android:text="房間"
                                android:textColor="@color/gray"
                                android:textSize="12sp" />

                            <TextView
                                android:textColor="@color/black"
                                android:id="@+id/estate_room"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:gravity="left"
                                android:paddingBottom="5dp"
                                android:paddingLeft="3dp"
                                android:paddingTop="5dp"
                                android:singleLine="true"
                                android:text=""
                                android:textSize="15sp" />
                        </LinearLayout>

                        <LinearLayout
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="1">

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginLeft="10dp"
                                android:gravity="left"
                                android:paddingBottom="5dp"
                                android:paddingTop="5dp"
                                android:singleLine="true"
                                android:text="樓齡"
                                android:textColor="@color/gray"
                                android:textSize="12sp" />

                            <TextView
                                android:textColor="@color/black"
                                android:id="@+id/estate_property_value"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:gravity="left"
                                android:paddingBottom="5dp"
                                android:paddingLeft="3dp"
                                android:paddingTop="5dp"
                                android:singleLine="true"
                                android:text=""
                                android:textSize="15sp" />
                        </LinearLayout>
                    </LinearLayout>
                </RelativeLayout>
            </RelativeLayout>
        </RelativeLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:background="@color/rel_word_gray" />
    </LinearLayout>

</com.daimajia.swipe.SwipeLayout>



然后在我们是我们在适配器下的使用了,我这里使用的是自定义listView我们的适配器继承就改为继承它的

BaseSwipeAdapter,里面有几个方法,跟我们继承的BaseAdapter其实没多少区别,直接看代码。

private class EstateMarkAdapter extends BaseSwipeAdapter {
    private List<EstateProList.ResultEntity.NewsEntity> newses;


    public void setAdapterData(List<EstateProList.ResultEntity.NewsEntity> newses) {
        this.newses = newses;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return newses == null ? 0 : newses.size();
    }

    @Override
    public Object getItem(int position) {
        return newses.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getSwipeLayoutResourceId(int i) {
        return R.id.swipe;
    }

    @Override
    public View generateView(final int position, ViewGroup parent) {
        View v = LayoutInflater.from(getActivity().getApplicationContext()).inflate(R.layout.real_estate_my_property_item, null);
        return v;
    }

    @Override
    public void fillValues(final int position, View convertView) {
        try {
            ViewHold viewHold = null;
            final SwipeLayout swipeLayout = (SwipeLayout) convertView.findViewById(getSwipeLayoutResourceId(position));
            swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
                @Override
                public void onStartOpen(SwipeLayout swipeLayout) {

                }

                @Override
                public void onOpen(SwipeLayout swipeLayout) {
                    isOpenSwipeLayout = swipeLayout;
                }

                @Override
                public void onStartClose(SwipeLayout swipeLayout) {

                }

                @Override
                public void onClose(SwipeLayout swipeLayout) {
                    isOpenSwipeLayout = null;
                }

                @Override
                public void onUpdate(SwipeLayout swipeLayout, int i, int i1) {

                }

                @Override
                public void onHandRelease(SwipeLayout swipeLayout, float v, float v1) {

                }
            });
            convertView.findViewById(R.id.bottom_wrapper).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (isFavoriteOrHistory) {
                        attrType = "3";
                    } else {
                        attrType = "1";
                    }
                    String deleteID = newses.get(position).getId() + "";
                    CallAPIDeleteProVIPRelation(Constant.userInfo.getId(), deleteID, attrType, position, swipeLayout);
                }
            });

            if (convertView != null && convertView.getTag() != null && convertView.getTag() instanceof ViewHold) {
                viewHold = (ViewHold) convertView.getTag();
            } else {
                Log.d("elvin", "getView======= " + position);
                viewHold = new ViewHold();
                ImageView headImg = (ImageView) convertView.findViewById(R.id.iv_real_estate_image);
                headImg.setImageResource(R.mipmap.detail_bg_mask);
                viewHold.iv_real_estate_image = headImg;
                viewHold.ib_real_estate_check = (ImageView) convertView.findViewById(R.id.ib_real_estate_check);
                viewHold.rl_add_my_property_item_select = (RelativeLayout) convertView.findViewById(R.id.rl_add_my_property_item_select);
                viewHold.tv_real_estate_property_name = (TextView) convertView.findViewById(R.id.tv_real_estate_property_name);
                viewHold.tv_real_estate_title = (TextView) convertView.findViewById(R.id.tv_real_estate_title);
                viewHold.tv_real_estate_region = (TextView) convertView.findViewById(R.id.tv_real_estate_region);
                viewHold.tv_real_estate_area = (TextView) convertView.findViewById(R.id.tv_real_estate_area);
                viewHold.tv_real_estate_price = (TextView) convertView.findViewById(R.id.tv_real_estate_price);
                viewHold.tv_real_estate_mop = (TextView) convertView.findViewById(R.id.tv_real_estate_mop);
                viewHold.estate_floor = (TextView) convertView.findViewById(R.id.estate_floor);
                viewHold.estate_room = (TextView) convertView.findViewById(R.id.estate_room);
                viewHold.estate_property_value = (TextView) convertView.findViewById(R.id.estate_property_value);
                convertView.setTag(viewHold);
            }
            Glide.with(getActivity()).load(newses.get(position).getImg64()).into(viewHold.iv_real_estate_image);
            viewHold.tv_real_estate_property_name.setText(newses.get(position).getType());
            viewHold.tv_real_estate_title.setText(newses.get(position).getTitle());
            viewHold.tv_real_estate_region.setText(newses.get(position).getAddress());
            viewHold.tv_real_estate_area.setVisibility(View.VISIBLE);
            viewHold.tv_real_estate_price.setVisibility(View.VISIBLE);
            viewHold.tv_real_estate_area.setText("面積 :" + newses.get(position).getArea() + "" + newses.get(position).getAreaUnit());
            viewHold.tv_real_estate_price.setText("單價 :" + newses.get(position).getCurrency() + newses.get(position).getUnivalent());
            viewHold.tv_real_estate_mop.setText(newses.get(position).getCurrency() + newses.get(position).getTotal());
            viewHold.estate_floor.setText(newses.get(position).getFloor());
            viewHold.estate_room.setText(newses.get(position).getRoom());
            viewHold.estate_property_value.setText(newses.get(position).getAge());
            if (null != isOpenSwipeLayout) {
                swipeLayout.close();
            }
            /*设置是否选中状态*/
            if (newses.get(position).isFlag()) {
                viewHold.ib_real_estate_check.setBackgroundResource(R.mipmap.contentbox_on);
            } else {
                viewHold.ib_real_estate_check.setBackgroundResource(R.mipmap.contentbox_off);
            }

            viewHold.ib_real_estate_check.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    notifyDataSetChanged();
                    if (newses.get(position).getSelsta() == 1) {
                        newses.get(position).setFlag(!newses.get(position).isFlag());
                        notifyDataSetChanged();
                        boolean isAllSeleted = true;
                        for (EstateProList.ResultEntity.NewsEntity en : newses) {
                            if (!en.isFlag()) {
                                isAllSeleted = false;
                                break;
                            }
                        }
                        if (isAllSeleted) {
                            isAllSelectCheck = true;
                            ib_real_estate_check_or_uncheck.setBackgroundResource(R.mipmap.contentbox_on);
                        } else {
                            isAllSelectCheck = false;
                            ib_real_estate_check_or_uncheck.setBackgroundResource(R.mipmap.contentbox_off);
                        }
                    } else {
                        popUpAlertBox(getActivity().getString(R.string.selsta), "", null, null, null);
                    }
                }
            });

            viewHold.rl_add_my_property_item_select.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    notifyDataSetChanged();
                    if (newses.get(position).getSelsta() == 1) {
                        MyProDetailActivity.drawable = null;
                        MyApp.getInstance().getImageLoader().get(newses.get(position).getImg64(), new ImageLoader.ImageListener() {
                            @Override
                            public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b) {
                                if (imageContainer != null && imageContainer.getBitmap() != null) {
                                    float imgWidth = imageContainer.getBitmap().getWidth();
                                    float scaleWidth = Constant.SCREEN_WIDTH / imgWidth;
                                    Matrix matrix = new Matrix();
                                    matrix.postScale(scaleWidth, scaleWidth);
                                    Bitmap bitMap = Bitmap.createBitmap(imageContainer.getBitmap(), 0, 0, imageContainer.getBitmap().getWidth(), imageContainer.getBitmap().getHeight(), matrix, true);
                                    Drawable dw = new BitmapDrawable(bitMap);
                                    MyProDetailActivity.drawable = dw;
                                    Intent intent = new Intent(getActivity(), MyProDetailActivity.class);
                                    if (newses.get(position).getCollSta().equals("1")) {
                                        intent.putExtra("isFavorite", true);
                                    } else {
                                        intent.putExtra("isFavorite", false);
                                    }
                                    intent.putExtra("videoUrl", newses.get(position).getVideoUrl());
                                    intent.putExtra("img64", newses.get(position).getImg64());
                                    intent.putExtra("id", newses.get(position).getId());
                                    intent.putExtra("FromEstateLine", 2);
                                    getActivity().startActivity(intent);
                                    getActivity().overridePendingTransition(R.animator.fadein, R.animator.fadeout);
                                }
                            }

                            @Override
                            public void onErrorResponse(VolleyError volleyError) {

                            }
                        });
                    } else {
                        popUpAlertBox(getActivity().getString(R.string.selsta_check), "", null, null, null);
                    }

                }
            });


        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}


private class ViewHold {
    /*物业名称*/
    public TextView tv_real_estate_property_name;
    /*img*/
    public ImageView iv_real_estate_image;
    /*title*/
    public TextView tv_real_estate_title;
    /*详细地址*/
    public TextView tv_real_estate_region;
    /*面积*/
    public TextView tv_real_estate_area;
    /*尺寸*/
    public TextView tv_real_estate_price;
    /*币种*/
    public TextView tv_real_estate_mop;
    /*楼层*/
    public TextView estate_floor;
    /*房间*/
    public TextView estate_room;
    /*楼龄*/
    public TextView estate_property_value;

    /*选择框*/
    public ImageView ib_real_estate_check;

    /*点击item 设置选择状态*/
    public RelativeLayout rl_add_my_property_item_select;

}

 
上面的相信大家都很熟悉了,但是我们滑动的时候应该如何去处理你打开滑动删除之后,如果我下拉滑动列表跟点击查看当前的item
的时候改如何去控制呢?其实很简单,直接添加监听,然后再他打开与关闭的方法中去做判断。
addSwipeListener 

监听下的方法,我们只需要关注如下的2个方法。

 <span style="white-space:pre">		</span>    @Override
                    public void onOpen(SwipeLayout swipeLayout) {
                        isOpenSwipeLayout = swipeLayout;
                    }

                

                    @Override
                    public void onClose(SwipeLayout swipeLayout) {
                        isOpenSwipeLayout = null;
                    }


然后我们就可以在滑动的时候去监听是否是打开还是关闭如果当前处于打开状态,就直接close掉就好OK了,点击事件也一样。

看看效果图:


当我们滑动的时候就可以根据当前的isOpenSwipeLayout对象是否为NULL来处理。因为你滑动或者触摸列表的时候滑动删除不应该还处于打开状态。

 
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值