Recently, I saw that the Weibo group in the top column of Sina Weibo was very effective. I checked some information on the Internet and found that it was originally implemented using popupwindow. Today I wrote an example to achieve this effect, I hope to help you.
Popupwindow refers to the pop-up window, similar to the start button under windows. Popupwindow can realize the floating layer effect, and can customize the display position, the animation at the time of appearance and exit.
First, define the top bar of Sina Weibo, title_two_button.xml and Main. xml.
<? XML version = "1.0" encoding = "UTF-8"?> <Framelayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"> <textview Android: Id = "@ + ID/tvtitle" Android: layout_width = "fill_parent" Android: layout_height = "45dip" Android: Background = "@ drawable/search_bg" Android: gravity = "center" Android: text = "Heaven has no path" Android: textcolor = "@ color/Black" Android: textsize = "25dip"/> <button Android: Id = "@ + ID/btn_title_left" Android: layout_width = "50dip" Android: layout_height = "35dip" Android: layout_gravity = "center_vertical" Android: Background = "@ drawable/title_button" Android: textcolor = "@ color/White"/> <button Android: id = "@ + ID/btn_title_right" Android: layout_width = "50dip" Android: layout_height = "35dip" Android: layout_gravity = "right" Android: layout_marginright = "2dip" Android: layout_margintop = "4dip" Android: Background = "@ drawable/title_button2" Android: textcolor = "@ color/White"/> </framelayout>
<?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:background="@android:color/white" android:orientation="vertical" > <include android:id="@+id/top_title" layout="@layout/title_two_button" /></LinearLayout>
The main purpose is to implement such an interface:
Next, define the layout files group_list.xml and group_item_view.xml in the pop-up dialog box.
<?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:background="@drawable/group_bg" android:orientation="vertical" > <ImageView android:id="@+id/iv_group_list_bg_divider" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/group_divider" /> <ListView android:id="@+id/lvGroup" android:layout_width="fill_parent" android:layout_height="fill_parent" android:divider="@drawable/group_divider" /></LinearLayout>
<?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" android:textColor="#ffffff" android:textSize="18dp" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" /></LinearLayout>
Customize an adapter: groupadapter
import java.util.List;import cn.com.karl.popwindow.R;import android.content.Context;import android.graphics.Color;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;public class GroupAdapter extends BaseAdapter {private Context context;private List<String> list;public GroupAdapter(Context context, List<String> list) {this.context = context;this.list = list;}@Overridepublic int getCount() {return list.size();}@Overridepublic Object getItem(int position) {return list.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup viewGroup) {ViewHolder holder;if (convertView==null) {convertView=LayoutInflater.from(context).inflate(R.layout.group_item_view, null);holder=new ViewHolder();convertView.setTag(holder);holder.groupItem=(TextView) convertView.findViewById(R.id.groupItem);}else{holder=(ViewHolder) convertView.getTag();}holder.groupItem.setTextColor(Color.BLACK);holder.groupItem.setText(list.get(position));return convertView;}static class ViewHolder {TextView groupItem;}}
Finally, I want to implement the poupwindowdemoactivity core class in the pop-up dialog box.
Import Java. util. arraylist; import Java. util. list; import cn.com. karl. 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. window; import android. view. windowmanager; import android. widget. adapterview; I Mport android. widget. adapterview. onitemclicklistener; import android. widget. listview; import android. widget. popupwindow; import android. widget. textview; import android. widget. toast; public class poupwindowdemoactivity extends activity {private popupwindow; private listview lv_group; private view top_title; private textview tvtitle; private list <string> groups; Public void oncre Ate (bundle savedinstancestate) {super. oncreate (savedinstancestate); this. requestwindowfeature (window. feature_no_title); setcontentview (R. layout. main); top_title = This. findviewbyid (R. id. top_title); tvtitle = (textview) top_title.findviewbyid (R. id. tvtitle); tvtitle. settext ("Heaven has no path"); tvtitle. setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {showwindow (v) ;}}) ;} private voi D showwindow (view parent) {If (popupwindow = NULL) {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 ("all"); groups. add ("My weibo"); groups. add ("friend"); groups. add ("Relatives"); groups. add ("student"); groups. add ("Friend"); groups. add ("Stranger"); groupadapter = new groupadapter (this, groups); lv_group.setadapter (groupadapter); // create a popuwidow object popupwindow = new popupwindow (view, 300,350 );} // aggregate popupwindow. setfocusable (true); // you can click to disappear from the pop window. setoutsidetouchable (true); // click "back" to make it disappear without affecting your background popupwindow. setbackgrounddrawable (New bitmapdrawable (); windowmanager = (Windo Wmanager) getsystemservice (context. window_service); // The display position is: half of the screen width-half of the height of popupwindow int xpos = windowmanager. getdefadisplay display (). getwidth ()/2-popupwindow. getwidth ()/2; log. I ("coder", "xpos:" + xpos); popupwindow. showasdropdown (parent, xpos, 0); lv_group.setonitemclicklistener (New onitemclicklistener () {@ overridepublic void onitemclick (adapterview <?> Adapterview, view, int position, long ID) {toast. maketext (poupwindowdemoactivity. This, groups. Get (position), 1000). Show (); If (popupwindow! = NULL) {popupwindow. Dismiss ();}}});}}
This completes the effect of a pop-up dialog box. Let's take a look at it!
Let's look at the pop-up dialog box of Sina Weibo:
We have already completed this function. We can add this function to our Sina Weibo to add some special colors to our applications!