ListView实现Item上下拖动交换位置 并且实现下拉刷新 上拉加载更多

 

ListView实现Item上下拖动交换位置  并且实现下拉刷新  上拉加载更多

 

package com.example.ListViewDragItem;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ArrayAdapter;
import com.example.ListViewDragItem.refreshlist.CustomDragListView;
import com.example.ListViewDragItem.refreshlist.XListView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MyActivity extends Activity implements CustomDragListView.OnChangeListener, XListView.IXListViewListener {
    private CustomDragListView customDragListView;
    private ArrayAdapter adapter;

    private List<String> list = new ArrayList<String>();

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        InitList();
        customDragListView = (CustomDragListView) findViewById(R.id.customDragListView);
        adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
        customDragListView.setAdapter(adapter);
        customDragListView.setOnChangeListener(this);
        customDragListView.setXListViewListener(this);
        customDragListView.setPullLoadEnable(true);
//        customDragListView.setPullRefreshEnable(true);
    }

    private void InitList() {
        for (int i = 1; i <= 50; i++) {
            list.add("zhangSan" + i + i + i + i + i + i);
        }
    }

    /**
     * item交换事件
     */
    @Override
    public void onMove(int from, int to) {
        int headerCount = customDragListView.getHeaderViewsCount();
        if (to - headerCount < list.size()
                && from - headerCount < list.size()) {
            Collections.swap(list, from - headerCount, to - headerCount);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onUp() {

    }

    /**
     * listView刷新事件
     */
    @Override
    public void onRefresh() {
        customDragListView.stopRefresh();
    }

    /**
     * listView加载更多事件
     */
    @Override
    public void onLoadMore() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                customDragListView.stopLoadMore();
            }
        }, 1000);
    }
}

震动权限:

 <uses-permission android:name="android.permission.VIBRATE"/>

 

源码下载:ListVIewDragItem

posted @ 2015-07-28 14:45  别人眼中的过客  阅读(859)  评论(0编辑  收藏  举报