public class ListActivity extends Activity implements OnTouchListener {
private ListView listView;
private MyAdapter adapter;
private PopupWindow popupWindow;
private TextView mTextView;
private int pos = -1;
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
listView = new ListView(this);
listView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
ll.addView(listView);
setContentView(ll);
adapter = new MyAdapter();
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
initPopView();
listView.setOnTouchListener(this);
}
private void initPopView() {
View view = LayoutInflater.from(this).inflate(R.layout.pop_view, null);
mTextView = (TextView) view.findViewById(R.id.textview);
popupWindow = new PopupWindow(view, 300, 100);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
}
@SuppressWarnings("deprecation")
GestureDetector gestureDetector = new GestureDetector(new GestureDetector.OnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
Log.e(this.getClass().getName(), "onSingleTapUp");
pos = -1;
adapter.notifyDataSetChanged();
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
Log.e(this.getClass().getName(), "onShowPress");
pos = -1;
adapter.notifyDataSetChanged();
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
// TODO Auto-generated method stub
Log.e(this.getClass().getName(), "onScroll");
float x = e1.getX();
float y = e1.getY();
float xTemp = e2.getX();
float yTemp = e2.getY();
int width = (int) (xTemp - x);
int height = (int) Math.abs(yTemp - y);
pos = ((ListView) view).pointToPosition((int) x, (int) y);
int currentPos = ((ListView) view).pointToPosition((int) xTemp, (int) yTemp);
if (currentPos == pos && width <= -80) {
adapter.notifyDataSetChanged();
return true;
}
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
Log.e(this.getClass().getName(), "onLongPress");
if (null == view) {
return;
}
pos = -1;
adapter.notifyDataSetChanged();
int arg2 = ((ListView) view).pointToPosition((int) e.getX(), (int) e.getY());
View arg1 = ((ListView) view).getChildAt(arg2);
int[] location = new int[2];
arg1.getLocationOnScreen(location);
int dalta = (300 - arg1.getWidth()) / 2;
Log.e(this.getClass().getName(), ":" + location[0] + "=======" + location[1]);
popupWindow.showAtLocation(arg1, Gravity.LEFT | Gravity.TOP, location[0] - dalta,
location[1] - 100);
mTextView.setText("第" + arg2 + "个");
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
});
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return 10;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
if (null == convertView) {
convertView = LayoutInflater.from(ListActivity.this).inflate(R.layout.vw_list_item,
null);
holder = new ViewHolder();
holder.button = (Button) convertView.findViewById(R.id.button);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (position == pos) {
Animation animation = AnimationUtils.loadAnimation(ListActivity.this,
R.anim.translate_in);
holder.button.startAnimation(animation);
holder.button.setVisibility(View.VISIBLE);
} else {
holder.button.clearAnimation();
holder.button.setVisibility(View.GONE);
}
return convertView;
}
}
class ViewHolder {
Button button;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.e(this.getClass().getName(), "onTouch");
pos = -1;
view = v;
gestureDetector.onTouchEvent(event);
return false;
}
private ListView listView;
private MyAdapter adapter;
private PopupWindow popupWindow;
private TextView mTextView;
private int pos = -1;
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
listView = new ListView(this);
listView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
ll.addView(listView);
setContentView(ll);
adapter = new MyAdapter();
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
initPopView();
listView.setOnTouchListener(this);
}
private void initPopView() {
View view = LayoutInflater.from(this).inflate(R.layout.pop_view, null);
mTextView = (TextView) view.findViewById(R.id.textview);
popupWindow = new PopupWindow(view, 300, 100);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
}
@SuppressWarnings("deprecation")
GestureDetector gestureDetector = new GestureDetector(new GestureDetector.OnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
Log.e(this.getClass().getName(), "onSingleTapUp");
pos = -1;
adapter.notifyDataSetChanged();
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
Log.e(this.getClass().getName(), "onShowPress");
pos = -1;
adapter.notifyDataSetChanged();
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
// TODO Auto-generated method stub
Log.e(this.getClass().getName(), "onScroll");
float x = e1.getX();
float y = e1.getY();
float xTemp = e2.getX();
float yTemp = e2.getY();
int width = (int) (xTemp - x);
int height = (int) Math.abs(yTemp - y);
pos = ((ListView) view).pointToPosition((int) x, (int) y);
int currentPos = ((ListView) view).pointToPosition((int) xTemp, (int) yTemp);
if (currentPos == pos && width <= -80) {
adapter.notifyDataSetChanged();
return true;
}
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
Log.e(this.getClass().getName(), "onLongPress");
if (null == view) {
return;
}
pos = -1;
adapter.notifyDataSetChanged();
int arg2 = ((ListView) view).pointToPosition((int) e.getX(), (int) e.getY());
View arg1 = ((ListView) view).getChildAt(arg2);
int[] location = new int[2];
arg1.getLocationOnScreen(location);
int dalta = (300 - arg1.getWidth()) / 2;
Log.e(this.getClass().getName(), ":" + location[0] + "=======" + location[1]);
popupWindow.showAtLocation(arg1, Gravity.LEFT | Gravity.TOP, location[0] - dalta,
location[1] - 100);
mTextView.setText("第" + arg2 + "个");
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
});
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return 10;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
if (null == convertView) {
convertView = LayoutInflater.from(ListActivity.this).inflate(R.layout.vw_list_item,
null);
holder = new ViewHolder();
holder.button = (Button) convertView.findViewById(R.id.button);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (position == pos) {
Animation animation = AnimationUtils.loadAnimation(ListActivity.this,
R.anim.translate_in);
holder.button.startAnimation(animation);
holder.button.setVisibility(View.VISIBLE);
} else {
holder.button.clearAnimation();
holder.button.setVisibility(View.GONE);
}
return convertView;
}
}
class ViewHolder {
Button button;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.e(this.getClass().getName(), "onTouch");
pos = -1;
view = v;
gestureDetector.onTouchEvent(event);
return false;
}