Android Imitation micro-trust friend punctuate Zane Comment pop-up feature _android

Source: Internet
Author: User

Contribution/download Source: Https://github.com/mmlovesyy/PopupWindowDemo

This article simply imitates the micro-letter friend Circle Point Zane Comment Pop-up box, the layout and so on details please ignore, emphatically realizes the pop-up box, sends the comment, and the pop-up position control.

1. Micro-letter Pop-up box

Micro-trust Friends Circle Point Zane Comment function, there are 2 components:

Click on the lower left corner of the "more" button, pop-up dialog box;

Click on the comments, pop-up the input box, add comments and real-time display in the page;

Micro-trust friend punctuate Zane comment function

2. Actual effect

This article will build a ListView, in its Item simple imitation micro-letter layout, and then focus on the implementation of pop-up windows, and can send comments, ignoring the details of the layout. The specific effect is as follows:

3. List of knowledge points

Listview

Custom Adapter, overriding the GetView () method;

Popupwindow

Pop-up using Popupwindow implementation, which is the point Zane Comment carrier, specifically to involve Popupwindow click on the Non window position and click again to disappear and show the location of the problem (according to the location of the corresponding more buttons to determine the position of Popupwindow, about Popupwindow display location, you can refer to my another article on the Android Popupwindow display location);

Layoutinflater

Using Layoutinflater to dynamically load the Popupwindow layout, learn more about Layoutinflater, see my other blog, Android Layoutinflater;

Bidirectional communication of activity and Item

By customizing the Oncommentlistener () to implement mainactivity (specifically, the input in the comment box at the bottom of the screen) and Itemview (dynamically obtaining the comments on the input and showing them in the Itemview), More knowledge See my other blog "Yan over leaving voice: by activity and Fragment communication method thought of";

Custom controls

Each Item in the ListView is a custom itemview, remember to rewrite the construction method, otherwise throw the Android.view.InflateException exception;

If you want to implement the micro-credit comments, user name and content reply text font color is different, and click the comment user name trigger page jump and other functions, see "Layout optimization Tips Notes" Clickablespan Chapter;

4. Art Material

Since. APK is essentially a compressed package, we can extract the image footage and layout files of the. apk file by decompressing it, and more ways to get the footage see my other blog post how to get a picture of the Android footage. In this way to get color, more buttons, such as the style of materials, only for learning, do not do infringement of copyright. Respecting knowledge copyright is both a trend and a matter that will benefit every developer.

Folder R Store Pictures

Find More Buttons

5. Key code

Development environment: Android Studio 1.4.1 for Mac + ADT + JDK 1.8.0.

Mainacitivity.java

Package Main.zhaizu.com.popupwindowdemo;
Import Android.content.Context;
Import Android.os.Bundle;
Import android.support.v7.app.AppCompatActivity;
Import Android.text.TextUtils;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.EditText;
Import Android.widget.ListView;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.Map;
Import main.zhaizu.com.popupwindowdemo.model.Comment;
Import Main.zhaizu.com.popupwindowdemo.model.Item;
Import Main.zhaizu.com.popupwindowdemo.ui.ItemView; public class Mainactivity extends Appcompatactivity {private ListView mlistview; private View mcommentview; private Myada
Pter Myadapter; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_main);
Mlistview = (ListView) Findviewbyid (R.id.listview); Myadapter = new Myadapter (This, getData());
Mlistview.setadapter (Myadapter);
Mcommentview = Findviewbyid (R.id.comment_view);
}//Build Data Private arraylist<item> GetData () {int item_count = 20;
arraylist<item> data = new arraylist<> ();
Data.add (New Item (R.drawable.xiaona, "mint chestnut", "I've learned taekwondo, all give me knees to sing Conquest", "Yesterday"));
Data.add (New Item (R.drawable.xueyan, "gladly", "go to the ends of the earth, only my home scenery the best, ah haha", "Yesterday"));
Data.add (New Item (R.drawable.leishao, "Chen Lei _cl", "Lao Tze will be the president after all, come to me to borrow money, now", "Yesterday"));
Data.add (New Item (R.drawable.yuhong, "Eternal Still", "the house car all comes into the bowl", "Yesterday"));
Data.add (New Item (R.drawable.lanshan, "Blue-shan", "You silly x, I laugh but not language", "Yesterday"));
return data; }//Custom adapter private class Myadapter extends Baseadapter implements Itemview.oncommentlistener {private Context Co
ntext
Private arraylist<item> Mdata;
Private Map<integer, itemview> mcachedviews = new hashmap<> (); Public Myadapter (context context, arraylist<item> mdata) {this.context = context; this.mdata = Mdata;} @Override P
ublic int GetCount () {return mdata.size ();}@Override public Object getitem (int position) {return mdata.get (position);} @Override public long getitemid (int position {position} @Override public view getview (int position, View Convertview, ViewGroup parent) {view view; if (CO Nvertview!= null) {view = Convertview;} else {Layoutinflater inflater = (layoutinflater) context.getsystemservice (Cont Ext.
Layout_inflater_service);
View = Inflater.inflate (R.layout.listview_item, NULL, FALSE);
} if (view instanceof Itemview) {Item data = (item) getitem (position);
((Itemview) view). SetData (data);
((Itemview) view). SetPosition (position);
((Itemview) view). Setcommentlistener (this);
Cacheview (position, (itemview) view);
} return view; @Override public void oncomment (int position) {Showcommentview (position);} private void Cacheview (int position, ITEMVI EW view) {Iterator<map.entry<integer, itemview>> entries = Mcachedviews.entryset (). iterator (); while ( Entries.hasnext ()) {Map.entry<integer, itemview> Entry= Entries.next ();
if (entry.getvalue () = = view && entry.getkey ()!= position) {Mcachedviews.remove (Entry.getkey ());
Mcachedviews.put (position, view);
} private void Showcommentview (final int position) {mcommentview.setvisibility (view.visible); Mcommentview.findviewbyid (R.id.submit). Setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v)
{EditText et = (edittext) Mcommentview.findviewbyid (R.id.edit);
String s = Et.gettext (). toString (); if (! Textutils.isempty (s)) {//update model Comment Comment = new Comment (s); Mdata.get (position). Getcomments (). Add (Comment)
;
Update view maybe Itemview Itemview = mcachedviews.get (position);
if (Itemview!= null && position = = Itemview.getposition ()) {itemview.addcomment ();} et.settext ("");
Mcommentview.setvisibility (View.gone);
}
}
}); }
}
}

Itemview.java

Package main.zhaizu.com.popupwindowdemo.ui;
Import Android.content.Context;
Import android.graphics.drawable.BitmapDrawable;
Import android.graphics.drawable.ColorDrawable;
Import Android.util.AttributeSet;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.ImageView;
Import Android.widget.LinearLayout;
Import Android.widget.PopupWindow;
Import Android.widget.TextView;
Import MAIN.ZHAIZU.COM.POPUPWINDOWDEMO.R;
Import main.zhaizu.com.popupwindowdemo.model.Comment;
Import Main.zhaizu.com.popupwindowdemo.model.Item;
/** * Created by CMM on 15/10/31. 
* * public class Itemview extends LinearLayout implements View.onclicklistener {private int mposition; private Item mdata;
Private ImageView Mportraitview;
Private TextView Musernameview;
Private TextView Mcontentview;
Private TextView Mcreatedatview;
Private LinearLayout mcommentlayout;
Private View Mmoreview;
Private Popupwindow Mmorepopupwindow; private int MshowmorepopupwindoWwidth;
private int mshowmorepopupwindowheight;
Private Oncommentlistener Mcommentlistener;  Public Itemview {Super (context);} public Itemview (context, AttributeSet attrs) {super,
ATTRS); public interface Oncommentlistener {void oncomment (int position);} @Override protected void Onfinishinflate () {super.
Onfinishinflate ();
Mportraitview = (ImageView) Findviewbyid (r.id.portrait);
Musernameview = (TextView) Findviewbyid (r.id.nick_name);
Mcontentview = (TextView) Findviewbyid (r.id.content);
Mcreatedatview = (TextView) Findviewbyid (R.ID.CREATED_AT);
Mcommentlayout = (linearlayout) Findviewbyid (r.id.comment_layout);
Mmoreview = Findviewbyid (R.ID.MORE_BTN); public void setposition (int mposition) {this.mposition = mposition;} public void Setcommentlistener (Oncommentlistener L) {this.mcommentlistener = l;} public void SetData (Item data) {mdata = data; Mportraitview.setimageresource (DATA.GETPO
Rtraitid ());
Musernameview.settext (Data.getnickname ()); MContentview.settext (Data.getcontent ());
Updatecomment ();
Mmoreview.setonclicklistener (this); /** * Pop-up point Zane comment Box * * @param morebtnview * * private void ShowMore (View morebtnview) {if (Mmorepopupwindow = null) {layou
Tinflater li = (layoutinflater) getcontext (). 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);
Mshowmorepopupwindowwidth = 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); //dot-Hop listener comment.setonclicklistener (this); } if (Mmorepopupwindow.isshowing ()) {Mmorepopupwindow.dismiss ();} else {int heightmorebtnview = Morebtnview.getheight (
); Mmorepopupwindow.showasdropdown (Morebtnview,-mshowmorepopupwindowwidth,-(Mshowmorepopupwindowheight +
Heightmorebtnview)/2);
The private void Updatecomment () {if (Mdata.hascomment ()) {mcommentlayout.removeallviews ());
Mcommentlayout.setvisibility (view.visible); For (Comment c:mdata.getcomments ()) {TextView t = new TextView (GetContext ()); T.setlayoutparams (New Linearlayout.layout
Params (New Viewgroup.layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
T.setbackgroundcolor (Getresources (). GetColor (R.COLOR.COLORCOMMENTLAYOUTBG));
T.settextsize (16);
T.setpadding (5, 2, 0, 3);
T.setlinespacing (3, (float) 1.5);
T.settext (C.getcomment ());
Mcommentlayout.addview (t);
} else {mcommentlayout.setvisibility (view.gone);}} @Override public void OnClick (View v) {int id = V.getid (); if (ID= = r.id.more_btn) {showmore (v);} else if (id = = r.id.comment) {if (Mcommentlistener!= null) {Mcommentlistener.oncomme
NT (mposition);
if (Mmorepopupwindow!= null && mmorepopupwindow.isshowing ()) {Mmorepopupwindow.dismiss ();}} The public int getPosition () {return mposition} is public void AddComment () {updatecomment ()}}

The above is a small series to introduce the Android imitation Micro-trust friend punctuate Zane comment pop-up box function, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.