侧滑删除or长按删除 安卓新人 自己理解的删除

侧滑删除是每一个新人接触安卓时都非常激动的一个控件,我也如此,我是一名安卓1年的新人,也是根据自己的理解来实现这么一个侧滑删除(也叫长按删除).
虽然很粗糙,但是很ok.先上图.

看起来八九不离十吧.大致的操作也就不多说了.
所以下自己的做法:
1.listvie加载很多个布局
2.每个item里面是一个horizonScrollview里面嵌套两个relativeLayout
就这么简单,直接上代码

MainAcitity主acitivty

public class MainActivity extends Activity {

    private ListView  listview;
    private List<Map<String, String>>list = new ArrayList<Map<String,String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setviews();

        setAdapter();

    }


    private void setviews() {
        listview = (ListView) findViewById(R.id.listview);

    }

    private void setAdapter() {
        list = new ArrayList<Map<String,String>>();
        for (int i = 0; i < 20; i++) {
            Map<String, String>map = new HashMap<String, String>();
            map.put("1", "这是第"+i+"个布局");
            list.add(map);
        }

        MyAdapter adapter = new MyAdapter(this, list);
        listview.setAdapter(adapter);

    }

}


MyAdapter新手必备

public class MyAdapter extends BaseAdapter{

    private Context context;
    private List<Map<String, String>>list = new ArrayList<Map<String,String>>();

    public MyAdapter(Context context , List<Map<String, String>>list) {
        this.context = context;
        this.list = list;
    }

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

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        holder = new ViewHolder();
        convertView = View.inflate(context, R.layout.item, null);
        holder.tv = (TextView) convertView.findViewById(R.id.tv);
        holder.holrzon_sc = (HorizontalScrollView) convertView.findViewById(R.id.holrzon_sc);
        holder.rl01 = (RelativeLayout) convertView.findViewById(R.id.rl01);
        holder.rl02 = (RelativeLayout) convertView.findViewById(R.id.rl02);
        convertView.setTag(holder);

        holder.tv.setText(list.get(position).get("1"));
        holder.rl01.setOnLongClickListener(new  OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                holder.holrzon_sc.smoothScrollTo(200,0);//HorizonScrollview滑动到100dp的位置
                return true;//放在和setOnClickListener冲突
            }
        });
        holder.rl01.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.holrzon_sc.smoothScrollTo(0,0);//HorizonScrollview滑动到0dp的位置
            }
        });

        holder.rl02.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                list.remove(position);//删除对应position
                notifyDataSetChanged();//重新加载
            }
        });
        holder.holrzon_sc.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });

        return convertView;
    }

    class ViewHolder{
        TextView tv;
        HorizontalScrollView holrzon_sc;
        RelativeLayout rl01;
        RelativeLayout rl02;
    }

}

//xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <HorizontalScrollView
        android:id="@+id/holrzon_sc"
        android:layout_width="wrap_content"
        android:background="@null"
        android:scrollbars="@null"
        android:layout_height="wrap_content" >

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

            <RelativeLayout
                android:id="@+id/rl01"
                android:layout_width="360dp"
                android:layout_height="80dp"
                android:background="#ffffff" >

                <TextView
                    android:id="@+id/tv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="20dp"
                    android:text="第一个"
                    android:textColor="#1a1a1a"
                    android:textSize="16sp" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="0.5dp"
                    android:layout_alignParentBottom="true"
                    android:background="#dcdcdc" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/rl02"
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:background="#f02030" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:text="删除"
                    android:textColor="#ffffff"
                    android:textSize="16sp" />
            </RelativeLayout>
        </LinearLayout>
    </HorizontalScrollView>

</RelativeLayout>

//mainactivity的xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView 
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@null"
        android:divider="@null"
        android:scrollbars="@null"
        android:listSelector="@android:color/transparent"
        ></ListView>

</RelativeLayout>

内容很简单,也很好理解,做的很简陋,也只能说能力有限,继续完善加油吧.我是安卓1年新人.

你可能感兴趣的:(android,ListView,删除,侧滑删除,长按删除)