Android imitation QQ, micro-letter chat interface long by the prompt box effect _android

Source: Internet
Author: User

Let's take a look at the effect chart.

How to use

Sample code

Promptviewhelper pvhelper = new Promptviewhelper (mactivity);
Pvhelper.setpromptviewmanager (New Chatpromptviewmanager (mactivity));
Pvhelper.addprompt (Holder.itemView.findViewById (r.id.textview_content));

It's still easy to use.

First the new PromptViewHelper class, then set a hint view manager, and finally call the addPrompt method add view, this view is the view to add a hint box.

Realize the idea

Through the use of QQ, micro-letter This function, feel the hint box should be used PopupWindow to meet the needs.

So the overall idea is:

1. When the view load succeeds, add a long press event to it

2, the user long press the time new one PopupWindow , and show it, and set click External area can disappear
Architecture

To make the upper call simple and convenient, I'm going to encapsulate the cue box view into a class that includes the initial method, binding data, adding events, and so on, based on this consideration, first defining an abstract class and then having the specific implementation class implement the corresponding method, let's take a look at this abstract class first.

 public static abstract class Promptviewmanager {private View promptview; 
 protected activity; 
 Private string[] DataArray; 
 Private Location Location; 

 Public Onitemclicklistener Onitemclicklistener; 
  Public Promptviewmanager (activity activity, string[] DataArray, Location Location) {this.activity = activity; 
  This.dataarray = DataArray; 
  this.location = location; 
 Init (); } public void Setonitemclicklistener (Onitemclicklistener onitemclicklistener) {This.onitemclicklistener = OnItemClic 
 Klistener; 
 public void init () {Promptview = Inflateview (); 
 Binddata (Promptview, DataArray); 

 Public abstract View Inflateview (); 

 public abstract void Binddata (view view, string[] dataarray); 
 Public View Getpromptview () {return promptview; 
 Public Location getLocation () {return Location; }
 }

Note: There is a location object attribute in an abstract class, which is what this location is, because when we display this cue box view we will consider where it appears, the location is an enumeration object, It contains a number of location information;

public enum Location { 

 top_left (1), Top_center (2), Top_right (3), 
 Bottom_left (4), Bottom_center (5), bottom_ Right (6); 

 Icalculatelocation calculatelocation; 

 Private Location (int type) { 
 switch (type) {case  
 1:  
 calculatelocation = Icalculatelocation implementation class break  
 ; Case  

 2:  
 calculatelocation = Icalculatelocation implementation class break   
 ;  

 TODO 
 } 
 }

This enumeration object contains 6 kinds of location display types, and then an object is instantiated within the constructor based on the type, ICalculateLocation ICalculateLocation what is it?

Public interface Icalculatelocation { 
 int[] calculate (int[] srcviewlocation, view srcview, view promptview);

It is an interface that provides a calculate way to compute the x,y coordinates of the cue box view, which we use to compute the coordinates of different positions.

To this, the general structure has come out; first we define a PromtpViewManager manager to implement the load of the Cue box view, bind the data, add the event, and then implement the different computing classes by setting the location enumeration, compute the coordinates of the different positions, and then new one at the time of display. PopupWindow, by PromtpViewManager getting the Cue box view set to, and then PopupWindow by PromtpViewManager getting the location enumeration of the computed coordinates of the class, call the Calculate method to get x,y coordinates, and then PopupWindow the showAtLocation method to display the PopupWindow cue box.

Concrete implementation

First implement a PromtpViewManager management class

public class Chatpromptviewmanager extends Promptviewhelper.promptviewmanager {public Chatpromptviewmanager (activity 
 Activity, string[] DataArray, Location Location) {super (activity, DataArray, Location); } public Chatpromptviewmanager {This (activity, new string[]{"Copy", "Paste", "forward"}, Location.top_left 
 ); Public Chatpromptviewmanager (activity activity, Location Location) {This (activity, new string[]{"Copy", "Paste", "forward"} 
 , location); 
 @Override public View Inflateview () {Return to New Promptview (activity);  @Override public void Binddata (view view, string[] dataarray) {if (view instanceof Promptview) {Promptview  
   Promptview = (promptview) view; 
   Promptview.setcontentarray (DataArray); Promptview.setonitemclicklistener (New Promptview.onitemclicklistener () {@Override public void Onitemclick (in 
    T position) {if (Onitemclicklistener!= null) onitemclicklistener.onitemclick (position); } 
   }); } 
 }}

Materialized view, binding data, add events have been completed, the following is to calculate the view display coordinates, I have implemented a number of methods, posted a look, if you have their own needs for the position can implement a class replication method.

public class Topcenterlocation implements Icalculatelocation { 
 @Override public 
 int[] Calculate (int[) Srcviewlocation, view Srcview, view Promptview) { 
  int[] location = new Int[2]; 
  int offset = (promptview.getwidth ()-srcview.getwidth ())/2; 
  Location[0] = srcviewlocation[0]-offset; 
  LOCATION[1] = srcviewlocation[1]-promptview.getheight (); 
  Return location 
 }}

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can help, if there is doubt you can message exchange.

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.