listview點選item,在item的上邊彈出popwindow
阿新 • • 發佈:2018-12-31
public class ListActivity extends Activity {
private PopupWindow popupWindow;
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams( new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ListView listView = new ListView(this);
ll.addView(listView);
setContentView(ll);
MyAdapter adapter = new MyAdapter();
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
initPopView();
listView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_UP) {
if (popupWindow.isShowing()) {
popupWindow.dismiss();
}
}
return false;
}
});
}
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);
}
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 TextView textView = new TextView(ListActivity.this);
textView.setPadding(20, 20, 0, 20);
textView.setTextSize(25);
textView.setText("Hello Android");
textView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
int[] location = new int[2];
v.getLocationOnScreen(location);
int dalta = (300 - v.getWidth()) / 2;
Log.e(this.getClass().getName(), ":" + location[0] + "=======" + location[1]);
popupWindow.showAtLocation(textView, Gravity.CENTER|Gravity.TOP, location[0] - dalta, location[1] - 100);
mTextView.setText("第" + position + "個");
}
return false;
}
});
return textView;
}
}
private PopupWindow popupWindow;
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams( new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ListView listView = new ListView(this);
ll.addView(listView);
setContentView(ll);
MyAdapter adapter = new MyAdapter();
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
initPopView();
listView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_UP) {
if (popupWindow.isShowing()) {
popupWindow.dismiss();
}
}
return false;
}
});
}
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);
}
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 TextView textView = new TextView(ListActivity.this);
textView.setPadding(20, 20, 0, 20);
textView.setTextSize(25);
textView.setText("Hello Android");
textView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
int[] location = new int[2];
v.getLocationOnScreen(location);
int dalta = (300 - v.getWidth()) / 2;
Log.e(this.getClass().getName(), ":" + location[0] + "=======" + location[1]);
popupWindow.showAtLocation(textView, Gravity.CENTER|Gravity.TOP, location[0] - dalta, location[1] - 100);
mTextView.setText("第" + position + "個");
}
return false;
}
});
return textView;
}
}