记住listview的两个新方法

记住listview的两个新方法


1、pointToPosition

//依据触摸点的坐标计算出点击的是ListView的哪个item

int selectedItem = pointToPosition((int) e.getX(), (int) e.getY());



2、getFirstVisiblePosition()

获得当前最顶上的第一个item 的位置


使用案例---检测当前fling的item位置

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,

float velocityY) {

if (!isDeleteShown && Math.abs(velocityX) > Math.abs(velocityY)) {

deleteButton = LayoutInflater.from(getContext()).inflate(

R.layout.delete_button, null);

deleteButton.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

itemLayout.removeView(deleteButton);

deleteButton = null;

isDeleteShown = false;

listener.onDelete(selectedItem);

}

});

itemLayout = (ViewGroup) getChildAt(selectedItem - getFirstVisiblePosition());

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

params.addRule(RelativeLayout.CENTER_VERTICAL);

itemLayout.addView(deleteButton, params);

isDeleteShown = true;

}

return false;

}


你可能感兴趣的:(记住listview的两个新方法)