Android弹出窗口PopupWindow

PopupWindow的应用场景还是很多的。

效果图


demo下载:http://download.csdn.net/download/shenyuanqing/9067873

部分源码

MainActivity

package com.example.shen.popupwindowtest.activity;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.example.shen.popupwindowtest.R;
import com.example.shen.popupwindowtest.adapter.ListViewAdapter;
import com.example.shen.popupwindowtest.util.ToastUtil;

import java.util.ArrayList;


public class MainActivity extends BaseActivity {
    private PopupWindow popupWindow=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView(){
        ImageView ivCategory=(ImageView) findViewById(R.id.iv_category);
        ivCategory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupWindow(v);
            }
        });
    }

    private void showPopupWindow(View view){
        View contentView= LayoutInflater.from(this).inflate(R.layout.layout_popup_window,null);
        if(popupWindow==null) {
            popupWindow = new PopupWindow(contentView, RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT, true);
            //设置动画效果
            popupWindow.setAnimationStyle(R.style.AnimationFade);
            popupWindow.setBackgroundDrawable(new BitmapDrawable());
        }
        ArrayList<String> alString=new ArrayList<>();
        for(int i=0;i<10;i++){
            alString.add("");
        }
        ListViewAdapter adapter=new ListViewAdapter(this,alString);
        ListView listView=(ListView) contentView.findViewById(R.id.listview);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ToastUtil.showToast(MainActivity.this,"item"+position);
            }
        });
        popupWindow.showAsDropDown(view);
    }
}
activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rl_main"
    android:fitsSystemWindows="true">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@color/green">
        <ImageView
            android:id="@+id/iv_category"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_category"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:padding="10dp"/>
    </RelativeLayout>
</RelativeLayout>
ListViewAdapter
package com.example.shen.popupwindowtest.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.shen.popupwindowtest.R;

import java.util.ArrayList;

/**
 * Created by shen on 2015/8/31.
 */
public class ListViewAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<String> list;
    private LayoutInflater inflater;
    public ListViewAdapter(Context context,ArrayList<String> list){
        this.context=context;
        this.list=list;
        inflater=LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null) {
            convertView = inflater.inflate(R.layout.item_listview, null);
            viewHolder=new ViewHolder();
            viewHolder.tvContent=(TextView) convertView.findViewById(R.id.tv_content);
            convertView.setTag(viewHolder);
        }else{
            viewHolder=(ViewHolder) convertView.getTag();
        }
        viewHolder.tvContent.setText("item"+position);
        return convertView;
    }

    static class ViewHolder{
        TextView tvContent;
    }
}

style:AnimationFade

    <style name="AnimationFade">
        <item name="android:windowEnterAnimation">@anim/righttoleft</item>
        <item name="android:windowExitAnimation">@anim/lefttoright</item>
    </style>
righttoleft.xml

<?xml version="1.0" encoding="UTF-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android" >  
    <translate
        android:fromXDelta="50%"
        android:toXDelta="0"
        android:duration="200" />
    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="0.1"
        android:duration="200"/>
</set>
lefttoright.xml

<?xml version="1.0" encoding="UTF-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android" >  
    <translate  
        android:fromXDelta="0"
        android:toXDelta="50%"
        android:duration="200"/>
    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:duration="200"/>
</set>










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值