当前位置: 移动技术网 > IT编程>移动开发>Android > android仿新闻阅读器菜单弹出效果实例(附源码DEMO下载)

android仿新闻阅读器菜单弹出效果实例(附源码DEMO下载)

2019年07月24日  | 移动技术网IT编程  | 我要评论

新溧阳论坛,心情日记大全,唯美的古风句子

开发中碰到问题之后实现的,觉得可能有的开发者用的到或则希望独立成一个小功能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; 
  } 
 
  @override 
  public news getitem(int position) { 
    if (newslist != null && newslist.size() != 0) { 
      return newslist.get(position); 
    } 
    return null; 
  } 
 
  @override 
  public long getitemid(int position) { 
    return position; 
  } 
 
  @override 
  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); 
    } 
  } 
} 

就这么多的内容,很简单,日后碰到这类相关的效果,也就不用怕了。

下面是我经过上述代码实现的效果:

下面放上该效果源码demo的下载地址:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网