Android 自定义ListView 实现拖动排序效果

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_37730482/article/details/80676223

实现ListView拖动排序。。。

 

代码1:Activity

public class DragSortListViewActivity extends AppCompatActivity {

    private DragSortListView dragSortListView;
    private DragSortAdapter dragSortAdapter;
    private List<MyJaveBean> list;

    // 监听器在手机拖动停下的时候触发
    private DragSortListView.DropListener onDrop = new DragSortListView.DropListener() {
        @Override
        public void drop(int from, int to) {// from to 分别表示 被拖动控件原位置 和目标位置
            if (from != to) {
                MyJaveBean item = (MyJaveBean) dragSortAdapter.getItem(from);// 得到listview的适配器
                dragSortAdapter.remove(from);// 在适配器中”原位置“的数据。
                dragSortAdapter.insert(item, to);// 在目标位置中插入被拖动的控件。
            }
        }
    };
    // 删除监听器,点击左边差号就触发。删除item操作。
    private DragSortListView.RemoveListener onRemove = new DragSortListView.RemoveListener() {
        @Override
        public void remove(int which) {
            dragSortAdapter.remove(which);
        }
    };

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

        dragSortListView=findViewById(R.id.activity_dragsortlistview_listview);
        dragSortListView.setDropListener(onDrop);
        dragSortListView.setRemoveListener(onRemove);
        list=new ArrayList<MyJaveBean>();
        MyJaveBean myJaveBean1=new MyJaveBean();
        myJaveBean1.setName("张三");

        MyJaveBean myJaveBean2=new MyJaveBean();
        myJaveBean2.setName("李四");

        MyJaveBean myJaveBean3=new MyJaveBean();
        myJaveBean3.setName("王五");

        MyJaveBean myJaveBean4=new MyJaveBean();
        myJaveBean4.setName("韦德");

        MyJaveBean myJaveBean5=new MyJaveBean();
        myJaveBean5.setName("詹姆斯");

        MyJaveBean myJaveBean6=new MyJaveBean();
        myJaveBean6.setName("蒿俊闵");

        for(int i=0;i<10;i++){
            list.add(myJaveBean1);
            list.add(myJaveBean2);
            list.add(myJaveBean3);
            list.add(myJaveBean4);
            list.add(myJaveBean5);
            list.add(myJaveBean6);
        }

        dragSortAdapter=new DragSortAdapter(DragSortListViewActivity.this,list);
        dragSortListView.setAdapter(dragSortAdapter);
        dragSortListView.setDragEnabled(true); // 设置是否可拖动。

    }
}

 

 

代码2:Activity布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:dslv="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <com.wjn.myview.view.DragSortListView
        android:id="@+id/activity_dragsortlistview_listview"
        dslv:click_remove_id="@id/dragsort_listview_item_layout"
        dslv:collapsed_height="1px"
        dslv:drag_enabled="true"
        dslv:drag_handle_id="@id/dragsort_listview_item_textview"
        dslv:drag_scroll_start="0.33"
        dslv:drag_start_mode="onDown"
        dslv:float_alpha="0.6"
        dslv:remove_enabled="true"
        dslv:remove_mode="clickRemove"
        dslv:slide_shuffle_speed="0.3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"
        android:layout_marginRight="10dp"
        android:divider="#FFFFFF"
        android:dividerHeight="10dp"
        android:scrollbars="none">

    </com.wjn.myview.view.DragSortListView>

</LinearLayout>

 

 

代码3:Adapter

public class DragSortAdapter extends BaseAdapter {

    private Context context;
    private List<MyJaveBean> list;
    public DragSortAdapter(Context context, List<MyJaveBean> 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;
    }

    public void remove(int arg0) {// 删除指定位置的item
        list.remove(arg0);
        this.notifyDataSetChanged();// 不要忘记更改适配器对象的数据源
    }

    public void insert(MyJaveBean item, int arg0) {// 在指定位置插入item
        list.add(arg0, item);
        this.notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(null==convertView){
            convertView=View.inflate(context, R.layout.dragsort_listview_item,null);
            viewHolder=new ViewHolder();
            viewHolder.dellayout= (RelativeLayout) convertView.findViewById(R.id.dragsort_listview_item_layout);
            viewHolder.titletextview= (TextView) convertView.findViewById(R.id.dragsort_listview_item_textview);
            convertView.setTag(viewHolder);
        }else{
            viewHolder= (ViewHolder) convertView.getTag();
        }

        String name=list.get(position).getName();
        viewHolder.titletextview.setText(name);

        return convertView;
    }

    public static class ViewHolder{
        private TextView titletextview;
        private RelativeLayout dellayout;
    }

}

 

 

代码4:Item布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#CCCCCC">

        <TextView
            android:id="@+id/dragsort_listview_item_textview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/dragsort_listview_item_layout"
            android:ellipsize="end"
            android:gravity="center|left"
            android:lineSpacingExtra="5dp"
            android:padding="15dp"
            android:text="显示选择的标签"
            android:textColor="#696969"
            android:textSize="14sp" />

        <RelativeLayout
            android:id="@+id/dragsort_listview_item_layout"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerInParent="true">

            <ImageView
                android:layout_width="21dp"
                android:layout_height="21dp"
                android:layout_centerInParent="true"
                android:background="@mipmap/planandform_hui"
                android:scaleType="fitCenter" />
        </RelativeLayout>

    </RelativeLayout>


</LinearLayout>

 

 

效果:

 

完整代码:https://github.com/wujianning/CustomView

 

展开阅读全文

没有更多推荐了,返回首页