開發中碰到問題之後實作的,覺得可能有的開發者用的到或則希望獨立成一個小功能demo,是以就放出來這麼一個demo。
原本覺得是最後完成後發網站用戶端的,可是這樣展現不出一個功能一個功能的分析實作效果,而且周期時間長,是以就完成一部分,發一部分,敬請諒解。
下面的菜單彈出效果在很多的新聞閱讀器上都有,比如今日頭條、360新聞等。下
其實這個實作起來很簡單,看其效果,其實就是一個popupwindow,之後設定相應postion的按鈕點選屬性,之後擷取按鈕的位置,給它設定動畫顯示消失就可以出現了。
下面看看代碼的思路:
由于整體是一個listview,是以我把點選的事件寫到了對應的adapter擴充卡中。
public class myadapter extends baseadapter {
layoutinflater inflater = null;
activity activity;
arraylist<news> newslist;
private popupwindow popupwindow;
public myadapter(activity activity, arraylist<news> newslist) {
this.activity = activity;
this.newslist = newslist;
inflater = (layoutinflater) activity.getsystemservice(context.layout_inflater_service);
initpopwindow();
}
@override
public int getcount() {
return newslist != null ? newslist.size() : 0;
public news getitem(int position) {
if (newslist != null && newslist.size() != 0) {
return newslist.get(position);
}
return null;
public long getitemid(int position) {
return position;
public view getview(final int position, view convertview, viewgroup parent) {
view vi = convertview;
final viewholder holder;
if (vi == null) {
vi = inflater.inflate(r.layout.listview_item, null);
holder = new viewholder();
holder.item_title = (textview) vi.findviewbyid(r.id.item_title);
holder.item_content = (textview) vi.findviewbyid(r.id.item_content);
holder.button_showpop = (imageview) vi.findviewbyid(r.id.button_showpop);
vi.settag(holder);
} else {
holder = (viewholder) vi.gettag();
news news = getitem(position);
holder.item_title.settext(news.gettitle());
holder.item_content.settext(news.getcontent());
holder.button_showpop .setonclicklistener(new popaction(position));
return vi;
public class viewholder {
textview item_title;
textview item_content;
imageview button_showpop;
/**
* 初始化popwindow
* */
private void initpopwindow() {
view popview = inflater.inflate(r.layout.listview_pop, null);
popupwindow = new popupwindow(popview, layoutparams.wrap_content, layoutparams.wrap_content);
popupwindow.setbackgrounddrawable(new colordrawable(0));
//設定popwindow出現和消失動畫
popupwindow.setanimationstyle(r.style.popmenuanimation);
btn_pop_close = (imageview) popview.findviewbyid(r.id.btn_pop_close);
/** popwindow 關閉按鈕 */
private imageview btn_pop_close;
* 顯示popwindow
public void showpop(view parent, int x, int y,int postion) {
//設定popwindow顯示位置
popupwindow.showatlocation(parent, 0, x, y);
//擷取popwindow焦點
popupwindow.setfocusable(true);
//設定popwindow如果點選外面區域,便關閉。
popupwindow.setoutsidetouchable(true);
popupwindow.update();
if (popupwindow.isshowing()) {
btn_pop_close.setonclicklistener(new onclicklistener() {
public void onclick(view paramview) {
popupwindow.dismiss();
}
});
* 每個item中more按鈕對應的點選動作
public class popaction implements onclicklistener{
int position;
public popaction(int position){
this.position = position;
@override
public void onclick(view v) {
int[] arrayofint = new int[2];
//擷取點選按鈕的坐标
v.getlocationonscreen(arrayofint);
int x = arrayofint[0];
int y = arrayofint[1];
showpop(v, x , y, position);
}
就這麼多的内容,很簡單,日後碰到這類相關的效果,也就不用怕了。
下面是我經過上述代碼實作的效果: