0
点赞
收藏
分享

微信扫一扫

基于上一篇实现的SimpleListPopupWindow<T>


这次是个简单的PopupWindow,快速迭代用。

import java.util.List;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.mb.bgfitting.App;
import com.mb.bgfitting.CommonAdapter;
import com.mb.bgfitting.ViewHolder;
import com.mb.bgfitting.app.R;

/**
*
* @author pythoner
*
*/
public class SimpleListPopupWindow<T> extends PopupWindow {

private Context context;
private CommonAdapter<T> adapter;
private List<T> beans;
public SimpleListPopupWindow(Context context, View view, int width,List<T> b) {
super(view, width, LayoutParams.WRAP_CONTENT, true);
this.context = context;
this.beans = b;
this.setBackgroundDrawable(App.res.getDrawable(R.drawable.bg_popupwindow));
this.setOutsideTouchable(true);
this.setAnimationStyle(android.R.style.Animation_Dialog);
// this.update();
// this.setTouchable(true);
// this.setFocusable(false);

ListView listView = (ListView) view.findViewById(R.id.listView);
listView.setAdapter(adapter = new CommonAdapter<T>(context, beans,R.layout.item_for_popupwindow_simple) {
@Override
public void setValues(ViewHolder helper, T item, int position) {
helper.setText(R.id.item_0, item.toString());
}
});

listView.setOnItemClickListener(new ListView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> pearnt, View view,
int position, long id) {
// TODO Auto-generated method stub
dismiss();
if (onItemClickListener != null) {
onItemClickListener.onItemClick(position,beans.get(position));
}
}
});

}

public interface OnItemClickListener<T> {
void onItemClick(int position, T item);
}

OnItemClickListener<T> onItemClickListener;

public void setOnItemClickListener(OnItemClickListener<T> onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}

}


一个简单的Item:item_for_popupwindow_simple.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

<TextView
android:id="@+id/item_0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:gravity="center"
android:textColor="@color/base_text_black"
android:textSize="@dimen/font_middle"
android:singleLine="true"
/>

</RelativeLayout>



用法:


private void showPopWindow(List<String> list) {
View view = LayoutInflater.from(context).inflate(R.layout.popupwindow_simple, null);
final SimpleListPopupWindow<String> popupWindow = new SimpleListPopupWindow<String>(context,view, btn_appointmentIndex.getWidth(), list);
popupWindow.showAsDropDown(btn_appointmentIndex, 0, 0);
popupWindow.setOnItemClickListener(new SimpleListPopupWindow.OnItemClickListener<String>() {

@Override
public void onItemClick(int position,final String item) {
// TODO Auto-generated method stub

}
});
}



popupwindow_simple.xml:


<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="@android:color/transparent"
android:listSelector="@color/base_gray"
/>

举报

相关推荐

0 条评论