android 仿 新聞閱讀器 菜單彈出效果(附源碼DEMO)

來源:互聯網
上載者:User

這一系列博文都是:(android高仿系列)今日頭條 --新聞閱讀器 (一)

開發中碰到問題之後實現的,覺得可能有的開發人員用的到或則希望獨立成一個小功能DEMO,所以就放出來這麼一個DEMO。

原本覺得是最後完成後髮網站用戶端的,可是這樣體現不出一個功能一個功能的分析實現效果,而且周期時間長,所以就完成一部分,發一部分,敬請諒解。


下面的菜單彈出效果在很多的新聞閱讀器上都有,比如今日頭條、360新聞等。下

其實這個實現起來很簡單,看其效果,其實就是一個PopupWindow,之後設定相應postion的按鈕點擊屬性,之後擷取按鈕的位置,給它設定動畫顯示消失就可以出現了。

下面看看代碼的思路:

由於整體是一個LISTVIEW,所以我把點擊的事件寫到了對應的Adapter適配器中。

public class MyAdapter extends BaseAdapter {LayoutInflater inflater = null;Activity activity;ArrayList newslist;private PopupWindow popupWindow;public MyAdapter(Activity activity, ArrayList newslist) {this.activity = activity;this.newslist = newslist;inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);initPopWindow();}@Overridepublic int getCount() {return newslist != null ? newslist.size() : 0;}@Overridepublic News getItem(int position) {if (newslist != null && newslist.size() != 0) {return newslist.get(position);}return null;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic 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;}@Overridepublic 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);}}}
就這麼多的內容,很簡單,日後碰到這類相關的效果,也就不用怕了。

下面是我經過上述代碼實現的效果:


高仿今日頭條(二)預計明天發布,大致已經是個整體用戶端了,有的功能還沒實現還在進一步完善,比如說新聞類別選擇那邊,拖動效果還未實現,目前只實現了點擊效果,所以還在慢慢摸索階段。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.