Android PopupWindow is a pop-up box with similar likes and comments. androidpopupwindow
This article simply imitates the likes and comments pop-up boxes in the circle of friends. Ignore the layout and other details, and focus on implementing the pop-up box and Position Control.
1. Dialog Box
The likes and comments in the circle of friends are composed of two parts:
2. Actual results
In this article, we will create a ListView, which is a simple imitation layout in its Item, and then focus on the pop-up window to ignore the specific layout details. The specific effect is as follows:
3. knowledge points
Knowledge Point list:
- ListView, custom Adapter, override the getView () method;
- PopupWindow: Use PopupWindow in the pop-up box. This is the carrier of thumb ups and comments, the specific issue involves the non-window position of PopupWindow, the disappearance of Click again, and the display position. (The position of PopupWindow is determined based on the location of more buttons. For details about the position of PopupWindow, see the position of Android PopupWindow in my other article );
- LayoutInflater: Uses LayoutInflater to dynamically load the layout of PopupWindow. For more information about LayoutInflater, see my other blog: Android LayoutInflater;
4. Artist Materials
Because. the apk is essentially a compressed package, which can be obtained through decompression. for more information about the image and layout files of the apk file, see my other blog post on how to obtain the Android image. In this way, you can obtain materials such as colors and more button styles for learning purposes only. Do not infringe copyright. Respecting intellectual property rights is both the general trend and will benefit every developer.
Store images in folder r
Find more buttons
5. key code
Development Environment: Android Studio 1.3.2 for Mac + ADT 21 + JDK 1.8.0.
MainAcitivity. java
Package com. example. cmm. helloworld; import android. app. alertDialog; import android. content. context; import android. graphics. drawable. bitmapDrawable; import android. support. v7.app. appCompatActivity; import android. OS. bundle; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. imageView; import android. widget. listView; import android. widget. popupWindow; import android. widget. textView; import java. util. arrayList; import java. util. list; public class MainActivity extends AppCompatActivity {private PopupWindow mMorePopupWindow; private int success; private int mShowMorePopupWindowHeight; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); ListView lv = (ListView) findViewById (R. id. listview); lv. setAdapter (new MyAdapter (MainActivity. this, getData ();} private List <Data> getData () {List <Data> data = new ArrayList <> (); data. add (new Data (R. drawable. xiaona, "Mint chestnut", "I have learned taekwondo, And I kneel down to conquer", "Yesterday"); data. add (new Data (R. drawable. xueyan, "happy", "traveling to the ends of the earth, only my home has the best scenery, Ah haha", "Yesterday"); data. add (new Data (R. drawable. leishao, "Chen Lei _ CL", "If I want to become a president later, I will come to borrow money from me. now", "Yesterday"); data. add (new Data (R. drawable. yuhong, "Everlasting", "House cars come to the bowl", "Yesterday"); data. add (new Data (R. drawable. lanshan, "lanshan", "You silly guys, I laugh and don't speak", "Yesterday"); return data;} class MyAdapter extends BaseAdapter {private List <Data> listdata; private Context context; public MyAdapter (Context context, List <Data> listdata) {this. context = context; this. listdata = listdata;} @ Override public int getCount () {return listdata. size () ;}@ Override public Object getItem (int arg0) {return listdata. get (arg0) ;}@ Override public long getItemId (int arg0) {return arg0 ;}@ Override public View getView (int position, View convertView, ViewGroup parent) {LayoutInflater inflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE); convertView = inflater. inflate (R. layout. listview_item, null, false); // ImageView ivPortrait = (ImageView) convertView. findViewById (R. id. portrait); TextView tvNickName = (TextView) convertView. findViewById (R. id. nick_name); TextView tvContent = (TextView) convertView. findViewById (R. id. content); TextView tvCreatedAt = (TextView) convertView. findViewById (R. id. created_at); ImageView moreBtn = (ImageView) convertView. findViewById (R. id. more_btn); // value Data data = listdata. get (position); ivPortrait. setImageResource (data. getPortraitId (); tvNickName. setText (data. getNickName (); tvContent. setText (data. getContent (); tvCreatedAt. setText (data. getCreatedAt (); // Click Event moreBtn for more buttons. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {showMore (v) ;}}); return convertView ;} /*** pop-up likes and comments ** @ param moreBtnView */private void showMore (View moreBtnView) {if (mMorePopupWindow = null) {LayoutInflater li = (LayoutInflater) MainActivity. this. getSystemService (Context. LAYOUT_INFLATER_SERVICE); View content = li. inflate (R. layout. layout_more, null, false); mMorePopupWindow = new PopupWindow (content, ViewGroup. layoutParams. WRAP_CONTENT, ViewGroup. layoutParams. WRAP_CONTENT); mMorePopupWindow. setBackgroundDrawable (new BitmapDrawable (); mMorePopupWindow. setOutsideTouchable (true); mMorePopupWindow. setTouchable (true); content. measure (View. measureSpec. UNSPECIFIED, View. measureSpec. UNSPECIFIED); mshowmorepopup1_wwidth = content. getMeasuredWidth (); mShowMorePopupWindowHeight = content. getMeasuredHeight (); View parent = mMorePopupWindow. getContentView (); TextView like = (TextView) parent. findViewById (R. id. like); TextView comment = (TextView) parent. findViewById (R. id. comment); // likes the listener like. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {final AlertDialog. builder alert = new AlertDialog. builder (MainActivity. this); alert. setTitle ("thumb up"); alert. setNegativeButton ("cancel", null); alert. show () ;}}); // comments listener. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {final AlertDialog. builder alert = new AlertDialog. builder (MainActivity. this); alert. setTitle ("comment"); alert. setNegativeButton ("cancel", null); alert. show () ;}}) ;}if (mMorePopupWindow. isShowing () {mMorePopupWindow. dismiss ();} else {int heightMoreBtnView = moreBtnView. getHeight (); mMorePopupWindow. showAsDropDown (moreBtnView,-mshowmorepopup1_wwidth,-(mshowmorepopup1_wheight + heightMoreBtnView)/2) ;}} class Data {private int portraitId; // private String nickName; // nickName private String content; // private String createdAt; // release time public Data (int portraitId, String nickName, String content, String createdAt) {this. portraitId = portraitId; this. nickName = nickName; this. content = content; this. createdAt = createdAt;} public int getPortraitId () {return portraitId;} public String getNickName () {return nickName;} public String getContent () {return content;} public String getCreatedAt () {return createdAt ;}}}
6. Thanks
In this article, the layout of listview is adapted from http://download.csdn.net/download/weiyirong/6709151,thank you for sharing the author.
If you need a complete project, please leave your email.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.