Android PopupWindow 에 Listview 추가
13457 단어 PopupWindow
nearby_popupwindow_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="90dip"
android:layout_height="300dip"
android:orientation="vertical"
android:background="@drawable/popwin_bg" >
<ImageView
android:id="@+id/ImgPopWinHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dip"
android:layout_marginLeft="24dip"
android:src="@drawable/popwin_header_grey" />
<ListView
android:id="@+id/lvNearbyPopWin"
android:layout_width="90dip"
android:layout_height="200dip"
android:divider="@drawable/popwin_divider"
android:cacheColorHint="#00000000"
android:layout_below="@id/ImgPopWinHeader"
android:fadingEdge="none"
android:listSelector="@null"
/>
<ImageView
android:id="@+id/ImgPopWinFooter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/lvNearbyPopWin"
android:layout_marginTop="3dip"
android:layout_marginLeft="24dip"
android:src="@drawable/popwin_footer_grey" />
</RelativeLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="90dip"
android:layout_height="wrap_content">
<TextView
android:id="@+id/TvPopWinItem"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:textColor="@color/black"
android:layout_marginLeft="20dip"
android:layout_marginTop="6dip"/>
</LinearLayout>
자바 파일
windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popupViewLong = layoutInflater.inflate(R.layout.nearby_popupwindow_list, null,false);// false
lvNearbyPopWinLong = (ListView)popupViewLong.findViewById(R.id.lvNearbyPopWin);
ImgPopWinHeaderLong = (ImageView)popupViewLong.findViewById(R.id.ImgPopWinHeader);
ImgPopWinFooterLong = (ImageView)popupViewLong.findViewById(R.id.ImgPopWinFooter);
Btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showWindow(v,groups);
});
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {
Toast.makeText(NearbyActivity.this, groups.get(selectId),
Toast.LENGTH_SHORT).show();
if (popupWindow != null) {
popupWindow.dismiss();
}
}
});
list.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
nearByPopWinAdapter.notifyDataSetChanged();
lvNearbyPopWinLong.requestFocusFromTouch();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if(totalItemCount>5){
//
if(lvNearbyPopWinLong.getLastVisiblePosition() == (totalItemCount-1)){
ImgPopWinHeaderLong.setImageResource(R.drawable.popwin_header_red);
ImgPopWinFooterLong.setImageResource(R.drawable.popwin_footer_grey);
}else if(lvNearbyPopWinLong.getFirstVisiblePosition() == 0){ //
ImgPopWinHeaderLong.setImageResource(R.drawable.popwin_header_grey);
ImgPopWinFooterLong.setImageResource(R.drawable.popwin_footer_red);
}else{ //
ImgPopWinHeaderLong.setImageResource(R.drawable.popwin_header_red);
ImgPopWinFooterLong.setImageResource(R.drawable.popwin_footer_red);
}
}
}
});
private void showWindow(View parent,List<String> groups) {
if (popupWindow == null) {
// PopuWidow
int width = windowManager.getDefaultDisplay().getWidth() / 4 - 10;
popupWindow = new PopupWindow(view, width, 350, true);
}
// “ Back” ,
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.showAsDropDown(parent); // button
popupWindow.setFocusable(true);
popupWindow.update();
// popupWindow.showAsDropDown(parent, 1, 0);
// // : -PopupWindow
// int xPos = windowManager.getDefaultDisplay().getWidth() / 2
// - popupWindow.getWidth() / 2;
// Log.i("coder", "xPos:" + xPos);
// popupWindow.showAsDropDown(parent, xPos, 0);
}
ListView 의 높이,popwindow 의 높이 를 엄 격 히 제어 해 야 합 니 다.그렇지 않 으 면 굴 러 갈 수 없습니다.