使用开源SwipeRevealLayout来对item进行滑动删除



github地址:https://github.com/chthai64/SwipeRevealLayout

这里只是简单的介绍其中一个演示,其他类似,也可以去github看一下demo

studio中:

dependencies {
    compile 'com.chauthai.swipereveallayout:swipe-reveal-layout:1.2.2'
}

一切都只是在Adapter里面操作的

看布局,外面用到的SwipeRevealLayout包裹完,然后将滑出的控件(比如删除键)放到前面,后面是正常的item。

<com.chauthai.swipereveallayout.SwipeRevealLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:mode="same_level"
        app:dragEdge="left">

        <!-- Your secondary layout here -->
        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />

        <!-- Your main layout here -->
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

</com.chauthai.swipereveallayout.SwipeRevealLayout>

我的:

<?xml version="1.0" encoding="utf-8"?>
<com.chauthai.swipereveallayout.SwipeRevealLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/swipe_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    app:dragEdge="right"
    app:mode="same_level">

    <FrameLayout
        android:id="@+id/delete_layout"
        android:layout_width="70dp"
        android:layout_height="115dp"
        android:background="#ffcc0000">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:background="@android:color/holo_red_dark"
            android:textColor="@android:color/white"
            android:text="删除" />
    </FrameLayout>


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="115dp">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffffff">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp">

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

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="车牌号:"
                        android:textColor="#000" />

                    <TextView
                        android:id="@+id/accident_layout_item_carnum"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="川A12121"
                        android:textColor="#000" />
                </LinearLayout>

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

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="驾驶员:"
                        android:textColor="#000" />

                    <TextView
                        android:id="@+id/accident_layout_item_drivername"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="测试"
                        android:textColor="#000" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp">

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

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="事故程度:"
                        android:textColor="#000" />

                    <TextView
                        android:id="@+id/accident_layout_item_shiguchengdu"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="事故1"
                        android:textColor="#000" />
                </LinearLayout>

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

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="事故类型:"
                        android:textColor="#000" />

                    <TextView
                        android:id="@+id/accident_layout_item_type"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="类型1"
                        android:textColor="#000" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp">

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

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="事故日期:"
                        android:textColor="#000" />

                    <TextView
                        android:id="@+id/accident_layout_item_time"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="2016-05-03"
                        android:textColor="#000" />
                </LinearLayout>

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

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="处理意见:"
                        android:textColor="#000" />

                    <TextView
                        android:id="@+id/accident_layout_item_agress"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="同意"
                        android:textColor="#000" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp">

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

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="事故发生地点:"
                        android:textColor="#000" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="成都"
                        android:textColor="#000"
                        android:id="@+id/accident_layout_item_adress" />
                </LinearLayout>

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

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="备注:"
                        android:textColor="#000" />

                    <TextView
                        android:id="@+id/accident_layout_item_beizhu"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="报废"
                        android:textColor="#000" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/accident_layout_item_showimage"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="查看图片"
                    android:textColor="#000" />

                <Button
                    android:id="@+id/accident_layout_item_downimage"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="下载事故认定书"
                    android:textColor="#000" />

                <Button
                    android:id="@+id/accident_layout_item_xiugai"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="修改" />
                <!--<Button-->
                <!--android:id="@+id/accident_layout_item_del"-->
                <!--android:layout_width="wrap_content"-->
                <!--android:layout_height="wrap_content"-->
                <!--android:layout_weight="1"-->
                <!--android:text="删除"-->
                <!--android:textColor="#000"/>-->

            </LinearLayout>
        </LinearLayout>
    </FrameLayout>
</com.chauthai.swipereveallayout.SwipeRevealLayout>

Adapter的代码,数据那些可以忽略(项目中从服务器中拿的)

在Adapter中对删除键进行监听点击操作就OK。

package com.officialcar.offcialvehicles.ui.driver_function.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.chauthai.swipereveallayout.SwipeRevealLayout;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.officialcar.offcialvehicles.AppConfig;
import com.officialcar.offcialvehicles.R;
import com.officialcar.offcialvehicles.domian.accidemtData.AccidentItem;
import com.officialcar.offcialvehicles.utils.GsonUtils;

import org.kymjs.kjframe.KJHttp;
import org.kymjs.kjframe.http.HttpCallBack;
import org.kymjs.kjframe.http.HttpConfig;
import org.kymjs.kjframe.http.HttpParams;

import java.util.Map;

/**
 * 事故上传的adapter
 * Created by qwe on 2016/4/11.
 */
public class AccidentAdapter extends BaseAdapter {
    private AccidentItem accidentItem;
    private Context context;

    public AccidentAdapter(AccidentItem accidentItem, Context context) {
        this.accidentItem = accidentItem;
        this.context = context;
    }

    @Override
    public int getCount() {
        return accidentItem.getList().size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater layoutInflater = LayoutInflater.from(this.context);
        ViewHolder viewHolder = null ;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.accident_layout_item,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.accident_layout_item_shiguchengdu =
                    (TextView) convertView.findViewById(R.id.accident_layout_item_shiguchengdu);
            viewHolder.accident_layout_item_adress =
                    (TextView) convertView.findViewById(R.id.accident_layout_item_adress);
            viewHolder.accident_layout_item_agress =
                    (TextView) convertView.findViewById(R.id.accident_layout_item_agress);

            viewHolder.accident_layout_item_carnum =
                    (TextView) convertView.findViewById(R.id.accident_layout_item_carnum);
            viewHolder.accident_layout_item_drivername =
                    (TextView) convertView.findViewById(R.id.accident_layout_item_drivername);

            viewHolder.accident_layout_item_type =
                    (TextView) convertView.findViewById(R.id.accident_layout_item_type);
            viewHolder.accident_layout_item_time =
                    (TextView) convertView.findViewById(R.id.accident_layout_item_time);
            viewHolder.accident_layout_item_beizhu =
                    (TextView) convertView.findViewById(R.id.accident_layout_item_beizhu);
//            viewHolder.accident_layout_item_del =
//                    (Button) convertView.findViewById(R.id.accident_layout_item_del);
            viewHolder.accident_layout_item_xiugai =
                    (Button) convertView.findViewById(R.id.accident_layout_item_xiugai);
            viewHolder.accident_layout_item_downimage =
                    (Button) convertView.findViewById(R.id.accident_layout_item_downimage);
            viewHolder.accident_layout_item_showimage =
                    (Button) convertView.findViewById(R.id.accident_layout_item_showimage);

            viewHolder.deleteView=convertView.findViewById(R.id.delete_layout);

            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        //事故发生地点
        viewHolder.accident_layout_item_adress.setText(accidentItem.getList().get(position).getAccident_place());
        //处理结果
        viewHolder.accident_layout_item_agress.setText(accidentItem.getList().get(position).getAccident_end());
        //备注
        viewHolder.accident_layout_item_beizhu.setText(accidentItem.getList().get(position).getAccident_duty());
        //车牌号
        viewHolder.accident_layout_item_carnum.setText(accidentItem.getList().get(position).getAccident_car_num());
        //司机名字
        viewHolder.accident_layout_item_drivername.setText(accidentItem.getList().get(position).getAccident_driver_name());
        //事故程度
        viewHolder.accident_layout_item_shiguchengdu.setText(accidentItem.getList().get(position).getAccident_level_name());
        //事故日期
        viewHolder.accident_layout_item_time.setText(accidentItem.getList().get(position).getAccident_time_str());
        //事故类型
        viewHolder.accident_layout_item_type.setText(accidentItem.getList().get(position).getAccident_type_name());

        viewHolder.deleteView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NormalDialogStyleOne(context,position);

            }
        });
        viewHolder.accident_layout_item_showimage.setVisibility(View.GONE);
        viewHolder.accident_layout_item_downimage.setVisibility(View.GONE);
        viewHolder.accident_layout_item_xiugai.setVisibility(View.GONE);
        return convertView;
    }

    private void NormalDialogStyleOne(Context context, final int position) {
        final NormalDialog dialog = new NormalDialog(context);
        dialog.content("是否确定删除?")//
                .title("删除事故信息")
                .show();

        dialog.setOnBtnClickL(
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.dismiss();
                    }
                },
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
//                        T.showShort(mContext, "right");
                        HttpConfig config = new HttpConfig();
                        config.cacheTime = 0;
                        final KJHttp kjh = new KJHttp(config);
                        final HttpParams params = new HttpParams();
                        params.put("className", "com.ycya.service.CarAccidentService");
                        params.put("methodName", "deleteCarAccidentInfo");
                        params.put("id", accidentItem.getList().get(position).getId());

                        kjh.post(AppConfig.MY_URL, params, new HttpCallBack() {
                            @Override
                            public void onSuccess(Map<String, String> headers, byte[] t) {
                                super.onSuccess(t);
                                Map<String, Object> mapJson = GsonUtils.mapJson(t);
                                if ("true".equals(mapJson.get("flag").toString())) {
                                    accidentItem.getList().remove(position);
                                    notifyDataSetChanged();
                                    showToast("删除成功");
                                } else {
                                    showToast("删除失败");
                                }
                            }
                            @Override
                            public void onFailure(int errorNo, String strMsg) {
                                super.onFailure(errorNo, strMsg);
                                showToast("网络错误,删除失败!");
                            }
                        });
                        dialog.dismiss();
                    }
                });
    }


    private void showToast(String s) {
        Toast.makeText(context, s, Toast.LENGTH_SHORT).show();
    }

    public class ViewHolder{
        private Button accident_layout_item_del;
        private Button accident_layout_item_xiugai;
        private Button accident_layout_item_downimage;
        private Button accident_layout_item_showimage;
        private TextView  accident_layout_item_beizhu;
        private TextView  accident_layout_item_adress;
        private TextView  accident_layout_item_agress;
        private TextView  accident_layout_item_time;
        private TextView  accident_layout_item_type;
        private TextView  accident_layout_item_shiguchengdu;
        private TextView  accident_layout_item_drivername;
        private TextView  accident_layout_item_carnum;

        SwipeRevealLayout swipeLayout;
        View deleteView;
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值