CostomListView下拉刷新

可以把listView下拉
public class MainActivity extends Activity {
ArrayListlist =new ArrayList();
MyAdatpter myAdatpter;
CustomListView customListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < 10; i++) {
list.add(“data”+i);
}
customListView=(CustomListView) findViewById(R.id.customListView);
myAdatpter = new MyAdatpter(this, list);
customListView.setAdapter(myAdatpter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

public class MyAdatpter extends BaseAdapter {
Context context;
ArrayList list;

public MyAdatpter(Context context, ArrayList<String> list) {
    super();
    this.context = context;
    if(list==null){
        this.list=new ArrayList<String>();
    }else{
        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(int position, View convertView, ViewGroup parent) {
    TextView tv =new TextView(this.context);
    tv.setText(list.get(position));
    return tv;

}

}

public class CustomListView extends ListView {
View headerView;
int height;
// 小写变大写 选中之后ctrl+shift+x
private final int STATE_DONE = 1;
private final int STATE_PULL = 2;
private final int STATE_RELEASE = 3;
private final int STATE_REFRESHING = 4;
int currentState;
int downY;

public CustomListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    try {
        // 设置初始状态
        currentState = STATE_DONE;
        headerView = View.inflate(getContext(), R.layout.listview_header,
                null);
        addHeaderView(headerView);
        // height=headerView.getHeight();
        // Measured测量
        // 测量headerview大小,0是一种测量方式
        headerView.measure(0, 0);
        height = headerView.getMeasuredHeight();
        // headerView.setPadding(0, 100, 0, 0);
        headerView.setPadding(0, -height, 0, 0);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    int action = ev.getAction();
    try {
        switch (action) {
        case MotionEvent.ACTION_DOWN:// 按下
            downY = (int) ev.getY();
            if (currentState == STATE_DONE) {
                currentState = STATE_PULL;
            }
            break;
        case MotionEvent.ACTION_MOVE:// 移动
            if (currentState == STATE_PULL) {
                int currentY = (int) ev.getY();
                int currentX = (int) ev.getX();
                int top = currentY;
                Log.i("坐标", "currentY=" + currentY + ",top=" + top);
                headerView.setPadding(currentX, top, 0, 0);
                this.setPadding(currentX, currentY, 0, 0);
            }
            break;
        case MotionEvent.ACTION_UP:// 松开

            break;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return super.onTouchEvent(ev);
}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值