zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

仿今日头条滑动评论效果详解手机开发

手机开发 详解 效果 评论 今日 滑动 头条
2023-06-13 09:20:13 时间

开发中碰到问题之后实现的,觉得可能有的开发者用的到或则希望独立成一个小功能DEMO,所以就放出来这么一个DEMO。

原本觉得是最后完成后发网站客户端的,可是这样体现不出一个功能一个功能的分析实现效果,而且周期时间长,所以就完成一部分,发一部分,敬请谅解。


下面的菜单弹出效果在很多的新闻阅读器上都有,比如今日头条、360新闻等。下

仿今日头条滑动评论效果详解手机开发

其实这个实现起来很简单,看其效果,其实就是一个PopupWindow,之后设定相应postion的按钮点击属性,之后获取按钮的位置,给它设置动画显示消失就可以出现了。

下面看看代码的思路:

由于整体是一个LISTVIEW,所以我把点击的事件写到了对应的Adapter适配器中。


[java]
view plain
copy
在CODE上查看代码片
派生到我的代码片 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);   }   }  }