Android用PopupWindow实现新浪微博的分组信息

在上一篇文章(http://blog.csdn.net/jiahui524/article/details/7269738)中我曾经用重写Dialog的方法来实现过对于分组信息的一些实现。后来在我的文章下面的评论中有人告诉我可以用PopupWindow来实现分组。并且新浪微博用的就是这种方法。非常感谢网友songfantasy的提醒。于是乎,今天特再写上一篇文章的续集,希望能帮到大家一些。

废话不多说!看效果,继续我的风格,先上图,再上代码,最终上源代码!

最终实现效果:

 

实现思路:

在一个PopupWindow里放一个ListView,从而来达到分组信息的实现!

具体主要实现代码:

group_list.xml:

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:layout_margin="0.0px"  
  6.     android:background="@drawable/group_bg"  
  7.     android:orientation="vertical"  
  8.     android:paddingLeft="0.0sp"  
  9.     android:paddingRight="0.0sp" >  
  10.   
  11.     <TextView  
  12.         android:id="@+id/groupAll"  
  13.         style="@style/grouplist_item_textview"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="@dimen/group_item_height"  
  16.         android:background="@drawable/grouplist_fixed_item_bg"  
  17.         android:gravity="center"  
  18.         android:text="全部" />  
  19.   
  20.     <ImageView  
  21.         android:id="@+id/iv_group_list_bg_divider"  
  22.         android:layout_width="fill_parent"  
  23.         android:layout_height="wrap_content"  
  24.         android:layout_margin="0.0px"  
  25.         android:background="@drawable/group_divider"  
  26.         android:padding="0.0px" />  
  27.   
  28.     <ListView  
  29.         android:id="@+id/lvGroup"  
  30.         android:layout_width="fill_parent"  
  31.         android:layout_height="fill_parent"  
  32.         android:layout_weight="0.0"  
  33.         android:cacheColorHint="#00000000"  
  34.         android:divider="@drawable/group_divider"  
  35.         android:dividerHeight="2.0px"  
  36.         android:drawSelectorOnTop="true"  
  37.         android:fadingEdgeLength="0.0sp"  
  38.         android:listSelector="@drawable/grouplist_item_bg" />  
  39.   
  40. </LinearLayout>  


group_item_view.xml:

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="@dimen/group_item_height"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/groupItem"  
  9.         style="@style/grouplist_item_textview"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="fill_parent"  
  12.         android:gravity="center" />  
  13.   
  14. </LinearLayout>  



 

 

Activity中的代码:

[java]  view plain copy print ?
  1. package com.jiahui.popwindow;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import com.jiahui.adapter.GroupAdapter;  
  7.   
  8. import android.app.Activity;  
  9. import android.content.Context;  
  10. import android.graphics.drawable.BitmapDrawable;  
  11. import android.os.Bundle;  
  12. import android.util.Log;  
  13. import android.view.LayoutInflater;  
  14. import android.view.View;  
  15. import android.view.WindowManager;  
  16. import android.widget.AdapterView;  
  17. import android.widget.AdapterView.OnItemClickListener;  
  18. import android.widget.ArrayAdapter;  
  19. import android.widget.Button;  
  20. import android.widget.ListView;  
  21. import android.widget.PopupWindow;  
  22. import android.widget.TextView;  
  23. import android.widget.Toast;  
  24.   
  25. public class PoupWindowDemoActivity extends Activity {  
  26.   
  27.     private PopupWindow popupWindow;  
  28.   
  29.     private ListView lv_group;  
  30.   
  31.     private View view;  
  32.   
  33.     private View top_title;  
  34.   
  35.     private TextView tvtitle;  
  36.   
  37.     private List<String> groups;  
  38.   
  39.     public void onCreate(Bundle savedInstanceState) {  
  40.         super.onCreate(savedInstanceState);  
  41.         setContentView(R.layout.main);  
  42.   
  43.         top_title = this.findViewById(R.id.top_title);  
  44.   
  45.         tvtitle = (TextView) top_title.findViewById(R.id.tvtitle);  
  46.   
  47.         tvtitle.setText("做一个低调的码农");  
  48.   
  49.         tvtitle.setOnClickListener(new View.OnClickListener() {  
  50.   
  51.             @Override  
  52.             public void onClick(View v) {  
  53.                 showWindow(v);  
  54.             }  
  55.         });  
  56.   
  57.     }  
  58.   
  59.     /** 
  60.      * 显示 
  61.      *  
  62.      * @param parent 
  63.      */  
  64.     private void showWindow(View parent) {  
  65.   
  66.         if (popupWindow == null) {  
  67.             LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  68.   
  69.             view = layoutInflater.inflate(R.layout.group_list, null);  
  70.   
  71.             lv_group = (ListView) view.findViewById(R.id.lvGroup);  
  72.             // 加载数据  
  73.             groups = new ArrayList<String>();  
  74.             groups.add("我的微博");  
  75.             groups.add("好友");  
  76.             groups.add("亲人");  
  77.             groups.add("陌生人");  
  78.   
  79.             GroupAdapter groupAdapter = new GroupAdapter(this, groups);  
  80.             lv_group.setAdapter(groupAdapter);  
  81.             // 创建一个PopuWidow对象  
  82.             popupWindow = new PopupWindow(view, 200250);  
  83.         }  
  84.   
  85.         // 使其聚集  
  86.         popupWindow.setFocusable(true);  
  87.         // 设置允许在外点击消失  
  88.         popupWindow.setOutsideTouchable(true);  
  89.   
  90.         // 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景  
  91.         popupWindow.setBackgroundDrawable(new BitmapDrawable());  
  92.         WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);  
  93.         // 显示的位置为:屏幕的宽度的一半-PopupWindow的高度的一半  
  94.         int xPos = windowManager.getDefaultDisplay().getWidth() / 2  
  95.                 - popupWindow.getWidth() / 2;  
  96.   
  97.         Log.i("coder""windowManager.getDefaultDisplay().getWidth()/2:"  
  98.                 + windowManager.getDefaultDisplay().getWidth() / 2);  
  99.         //  
  100.         Log.i("coder""popupWindow.getWidth()/2:" + popupWindow.getWidth() / 2);  
  101.   
  102.         Log.i("coder""xPos:" + xPos);  
  103.   
  104.         popupWindow.showAsDropDown(parent, xPos, 0);  
  105.   
  106.         lv_group.setOnItemClickListener(new OnItemClickListener() {  
  107.   
  108.             @Override  
  109.             public void onItemClick(AdapterView<?> adapterView, View view,  
  110.                     int position, long id) {  
  111.   
  112.                 Toast.makeText(PoupWindowDemoActivity.this,  
  113.                         "groups.get(position)" + groups.get(position), 1000)  
  114.                         .show();  
  115.   
  116.                 if (popupWindow != null) {  
  117.                     popupWindow.dismiss();  
  118.                 }  
  119.             }  
  120.         });  
  121.     }  
  122. }  

如需转载引用请注明出处:http://blog.csdn.net/jiahui524

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值