Android uses Popupwindow to implement a group information example for Sina Weibo _android

Source: Internet
Author: User

Sina Weibo has recently seen the top of the micro-blog group effect is very cool, from the Internet to check some data to understand that the original is implemented with Popupwindow, today I also wrote an example to achieve this effect, I hope to help.

Popupwindow is what a pop-up window means, like the Start button under Windows. Popupwindow can achieve a floating-layer effect, and can customize the display location, appearance and exit animation.

The effect is as follows:

Realize the idea:

In a popupwindow to put a ListView, so as to achieve the implementation of packet information!

Specific main implementation code:
Group_list.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" 
  Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:layout_margin=" 0.0px " android:background= "@drawable/group_bg" android:orientation= "vertical" android:paddingleft= "0.0SP" Android:paddin gright= "0.0SP" > <textview android:id= "@+id/groupall" style= "@style/grouplist_item_textview" and Roid:layout_width= "Fill_parent" android:layout_height= "@dimen/group_item_height" android:background= "@drawable/g ROUPLIST_FIXED_ITEM_BG "android:gravity=" center "android:text=" all "/> <imageview android:id=" @+i D/iv_group_list_bg_divider "android:layout_width=" fill_parent "android:layout_height=" Wrap_content "Android : layout_margin= "0.0px" android:background= "@drawable/group_divider" android:padding= "0.0px"/> <listv Iew android:id= "@+id/lvgrouP "android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:layout_weight=" 0.0 " 
    Android:cachecolorhint= "#00000000" android:divider= "@drawable/group_divider" android:dividerheight= "2.0px" Android:drawselectorontop= "true" android:fadingedgelength= "0.0sp" android:listselector= "@drawable/grouplist_item 
 _bg "/> </LinearLayout>

Group_item_view.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= 
"http://schemas.android.com/apk/" Res/android " 
  android:layout_width=" fill_parent " 
  android:layout_height=" "@dimen/group_item_height" 
  android:orientation= "vertical" > 
 
  <textview 
    android:id= "@+id/groupitem" 
    @style Grouplist_item_textview " 
    android:layout_width=" fill_parent " 
    android:layout_height=" Fill_parent 
    " android:gravity= "center"/> 
 
</LinearLayout> 

The code in the activity:

Package Com.jiahui.popwindow; 
Import java.util.ArrayList; 
 
Import java.util.List; 
 
Import Com.jiahui.adapter.GroupAdapter; 
Import android.app.Activity; 
Import Android.content.Context; 
Import android.graphics.drawable.BitmapDrawable; 
Import Android.os.Bundle; 
Import Android.util.Log; 
Import Android.view.LayoutInflater; 
Import Android.view.View; 
Import Android.view.WindowManager; 
Import Android.widget.AdapterView; 
Import Android.widget.AdapterView.OnItemClickListener; 
Import Android.widget.ArrayAdapter; 
Import Android.widget.Button; 
Import Android.widget.ListView; 
Import Android.widget.PopupWindow; 
Import Android.widget.TextView; 
 
Import Android.widget.Toast; 
 
  public class Poupwindowdemoactivity extends activity {private Popupwindow Popupwindow; 
 
  Private ListView Lv_group; 
 
  Private view view; 
 
  Private View Top_title; 
 
  Private TextView Tvtitle; 
 
  Private list<string> groups; 
   public void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); 
 
    Setcontentview (R.layout.main); 
 
    Top_title = This.findviewbyid (R.id.top_title); 
 
    Tvtitle = (TextView) Top_title.findviewbyid (r.id.tvtitle); 
 
    Tvtitle.settext ("To be a low-key farmer"); Tvtitle.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {s 
      Howwindow (v); 
 
  } 
    }); /** * Show * @param parent/private void ShowWindow (View parent) {if Popupwindow = n 
 
      ull) {Layoutinflater layoutinflater = (layoutinflater) getsystemservice (Context.layout_inflater_service); 
 
      View = Layoutinflater.inflate (r.layout.group_list, NULL); 
      Lv_group = (ListView) View.findviewbyid (R.id.lvgroup); 
      Load Data groups = new arraylist<string> (); 
      Groups.add ("my Microblog"); 
      Groups.add ("friend"); 
      Groups.add ("relatives"); 
 
      Groups.add ("Stranger"); 
   Groupadapter groupadapter = new Groupadapter (this, groups);   Lv_group.setadapter (Groupadapter); 
    Create a Popuwidow object Popupwindow = new Popupwindow (view, 200, 250); 
    //Make it clustered popupwindow.setfocusable (true); 
 
    Set to allow Popupwindow.setoutsidetouchable to disappear (true); 
    This is to click "Back" to make it disappear, and it will not affect your background popupwindow.setbackgrounddrawable (new bitmapdrawable ()); 
    WindowManager WindowManager = (windowmanager) getsystemservice (Context.window_service); The position shown is: Half of the width of the screen-popupwindow half of the height of the int xpos = Windowmanager.getdefaultdisplay (). GetWidth ()/2-popupwind 
 
    Ow.getwidth ()/2;  LOG.I ("Coder", "Windowmanager.getdefaultdisplay (). GetWidth ()/2:" + Windowmanager.getdefaultdisplay (). GetWidth ()/ 
    2); 
 
    LOG.I ("Coder", "popupwindow.getwidth ()/2:" + popupwindow.getwidth ()/2); 
 
    LOG.I ("Coder", "xpos:" + xpos); 
 
    Popupwindow.showasdropdown (parent, xpos, 0); Lv_group.setonitemclicklistener (New Onitemclicklistener () {@Override public voiD Onitemclick (adapterview<?> adapterview, view view, int position, long id) {Toast.maketext (P 
 
        Oupwindowdemoactivity.this, "groups.get (position)" + groups.get (position), 1000). Show (); 
        if (Popupwindow!= null) {Popupwindow.dismiss (); 
  } 
      } 
    }); 
 } 
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.