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

所属分类: 软件编程 / Android 阅读数: 36
收藏 0 赞 0 分享

最近看到新浪微博顶部栏的微博分组效果很炫,从网上查了一些资料明白原来是用PopupWindow实现的,今天自己也写了一个例子实现了这种效果,希望对大家有帮助。

PopupWindow就是弹出窗口的意思,类似windows下面的开始按钮。PopupWindow可以实现浮层效果,而且可以自定义显示位置,出现和退出时的动画.

效果如下:

实现思路:

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

具体主要实现代码:
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:paddingRight="0.0sp" > 
 
  <TextView 
    android:id="@+id/groupAll" 
    style="@style/grouplist_item_textview" 
    android:layout_width="fill_parent" 
    android:layout_height="@dimen/group_item_height" 
    android:background="@drawable/grouplist_fixed_item_bg" 
    android:gravity="center" 
    android:text="全部" /> 
 
  <ImageView 
    android:id="@+id/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" /> 
 
  <ListView 
    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="@style/grouplist_item_textview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" /> 
 
</LinearLayout> 

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("做一个低调的码农"); 
 
    tvtitle.setOnClickListener(new View.OnClickListener() { 
 
      @Override 
      public void onClick(View v) { 
        showWindow(v); 
      } 
    }); 
 
  } 
 
  /** 
   * 显示 
   * 
   * @param parent 
   */ 
  private void showWindow(View parent) { 
 
    if (popupWindow == null) { 
      LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
 
      view = layoutInflater.inflate(R.layout.group_list, null); 
 
      lv_group = (ListView) view.findViewById(R.id.lvGroup); 
      // 加载数据 
      groups = new ArrayList<String>(); 
      groups.add("我的微博"); 
      groups.add("好友"); 
      groups.add("亲人"); 
      groups.add("陌生人"); 
 
      GroupAdapter groupAdapter = new GroupAdapter(this, groups); 
      lv_group.setAdapter(groupAdapter); 
      // 创建一个PopuWidow对象 
      popupWindow = new PopupWindow(view, 200, 250); 
    } 
 
    // 使其聚集 
    popupWindow.setFocusable(true); 
    // 设置允许在外点击消失 
    popupWindow.setOutsideTouchable(true); 
 
    // 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景 
    popupWindow.setBackgroundDrawable(new BitmapDrawable()); 
    WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); 
    // 显示的位置为:屏幕的宽度的一半-PopupWindow的高度的一半 
    int xPos = windowManager.getDefaultDisplay().getWidth() / 2 
        - popupWindow.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(PoupWindowDemoActivity.this, 
            "groups.get(position)" + groups.get(position), 1000) 
            .show(); 
 
        if (popupWindow != null) { 
          popupWindow.dismiss(); 
        } 
      } 
    }); 
  } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android设计登录界面、找回密码、注册功能

这篇文章主要为大家详细介绍了Android设计登录界面的方法,Android实现找回密码、注册功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android通过手势实现答题器翻页效果

这篇文章主要为大家详细介绍了Android通过手势实现答题器翻页效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android采用双缓冲技术实现画板

这篇文章主要为大家详细介绍了Android采用双缓冲技术实现画板的相关资料,思路清晰,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android开发之毛玻璃效果实例代码

这篇文章主要给大家分享android开发之毛玻璃效果的实例代码,非常具有参考借鉴价值,感兴趣的朋友一起学习吧
收藏 0 赞 0 分享

Android实现桌面悬浮窗、蒙板效果实例代码

这篇文章主要介绍了Android实现桌面悬浮窗、蒙板效果实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

深入解读Android的Volley库的功能结构

这篇文章主要介绍了Android的Volley开发框架的功能结构,Volley是Android开发中网络部分的一大利器,包含很多HTTP协议通信的相关操作,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发中使用Volley库发送HTTP请求的实例教程

这篇文章主要介绍了Android开发中使用Volley库发送HTTP请求的实例教程,包括创建Volley单例的基本知识与取消Request请求的技巧等,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿QQ聊天撒花特效 很真实

本文写的这个特效,是关于聊天的,你肯定遇到过,就是你跟人家聊天的时候,比如发送应(么么哒),然后屏幕上全部就是表情了,今天我们就是做这个,撒花的特效,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android的HTTP操作库Volley的基本使用教程

这篇文章主要介绍了Android的HTTP操作库Volley的基本使用教程,包括JSON请求与图片加载等用法的实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿水波纹流量球进度条控制器

这篇文章主要介绍了Android仿水波纹流量球进度条控制器的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多