This series of blog posts are: (android high imitation series) toutiao.com-news reader (1)
After a problem occurs during development, developers may want to create a small function DEMO independently or use it. Therefore, such a DEMO is released.
It was originally thought that the website client was developed after the final completion, but this does not reflect the Analysis and Implementation Effect of a function and a long period of time. Therefore, we will complete part of the process and release part of it. Please forgive me.
The following menu pop-up effects are available in many news readers, such as toutiao and 360 news. Lower
In fact, this implementation is very simple. Looking at the effect, it is actually a PopupWindow, and then set the attributes of the corresponding postion button and then get the button location, set the animation to disappear.
Let's take a look at the Code:
As the whole is a LISTVIEW, I write the clicked event to the corresponding 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_titl E); 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 v I;} public class ViewHolder {TextView item_title; TextView item_content; ImageView button_showpop;}/*** initialize 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); // set the pop window to appear and disappear. setAnimat IonStyle (R. style. popMenuAnimation); btn_pop_close = (ImageView) popView. findViewById (R. id. btn_pop_close);}/** popWindow close button */private ImageView btn_pop_close;/*** display popWindow **/public void showPop (View parent, int x, int y, int postion) {// set the position of popwindow to display popupWindow. showAtLocation (parent, 0, x, y); // obtain popwindow focus popupWindow. setFocusable (true); // set popwindow to close if you click the outside area. PopupWindow. setOutsideTouchable (true); popupWindow. update (); if (popupWindow. isShowing () {} btn_pop_close.setOnClickListener (new OnClickListener () {public void onClick (View paramView) {popupWindow. dismiss () ;}}) ;}/ *** click action corresponding to the more button in each ITEM **/public class popAction implements OnClickListener {int position; public popAction (int position) {this. position = position ;}@ Overridepublic void onClick (View v) {int [] arrayOfInt = new int [2]; // obtain the coordinate v of the clicked button. getLocationOnScreen (arrayOfInt); int x = arrayOfInt [0]; int y = arrayOfInt [1]; showPop (v, x, y, position );}}}
So much content is very simple. You don't have to worry about such related effects in the future.
The following figure shows the effects of the above Code:
:
Gao imitation toutiao.com (ii) is expected to be released tomorrow. It is generally a whole client, and some functions are still being improved. For example, the drag effect has not yet been achieved when selecting news categories, currently, only the click effect is achieved, so it is still in the exploration phase.