可以把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);
}
}