Android -- PopupWindow(其中嵌套ListView 可以被点击)

1. 效果图 

            

 

2.   实现代码

  MainActivity

package com.michael.popupwindowdemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends Activity {

    private ImageButton ibOperationMore;

    List> moreList;

    private PopupWindow pwMyPopWindow;

    private ListView lvPopupList;

    private int NUM_OF_VISIBLE_LIST_ROWS = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();
        initPopupWindow();

        ibOperationMore = (ImageButton) findViewById(R.id.ib_operate_more);
        ibOperationMore.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (pwMyPopWindow.isShowing()) {

                    pwMyPopWindow.dismiss();
                } else {

                    pwMyPopWindow.showAsDropDown(ibOperationMore);
                }

            }
        });

    }

    private void initPopupWindow() {
        // TODO Auto-generated method stub

        LayoutInflater inflater = (LayoutInflater) this
                .getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.task_detail_popupwindow, null);
        lvPopupList = (ListView) layout.findViewById(R.id.lv_popup_list);
        pwMyPopWindow = new PopupWindow(layout);
        pwMyPopWindow.setFocusable(true); // 设置opoupWindow 可以获取焦点
        lvPopupList.setAdapter(new SimpleAdapter(MainActivity.this, moreList,
                R.layout.list_item_popupwindow, new String[] { "share_key" },
                new int[] { R.id.tv_list_item }));
        lvPopupList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView parent, View view,
                    int position, long id) {

                Toast.makeText(MainActivity.this,
                        moreList.get(position).get("share_key"),
                        Toast.LENGTH_LONG).show();
            }
        });

        lvPopupList.measure(View.MeasureSpec.UNSPECIFIED,
                View.MeasureSpec.UNSPECIFIED);
        pwMyPopWindow.setWidth(lvPopupList.getMeasuredWidth());
        pwMyPopWindow.setHeight((lvPopupList.getMeasuredHeight() + 20)
                * NUM_OF_VISIBLE_LIST_ROWS);

        pwMyPopWindow.setBackgroundDrawable(this.getResources().getDrawable(
                R.drawable.bg_popupwindow));
        pwMyPopWindow.setOutsideTouchable(true);

    }

    private void initData() {
        // TODO Auto-generated method stub
        moreList = new ArrayList>();
        Map map;
        map = new HashMap();
        map.put("share_key", "111");
        moreList.add(map);
        map = new HashMap();
        map.put("share_key", "222");
        moreList.add(map);
        map = new HashMap();
        map.put("share_key", "333");
        moreList.add(map);
    }

}

 activity_main.xml


    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    tools:context=".MainActivity" >

    
    <LinearLayout
        android:id="@+id/ll_head_bar"
        style="@style/header_linear_layout"
        android:layout_alignParentTop="true" >

        <TextView
            style="@style/header_text_view"
            android:visibility="invisible" />

        <ImageButton
            android:id="@+id/ib_operate_more"
            style="@style/header_button_operate"
            android:layout_marginLeft="8dip"
            android:layout_marginRight="10dip"
            android:src="@drawable/ico_headbar_more" />
    

 task_detail_popupwindow.xml



    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/lv_popup_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:divider="@drawable/ico_list_item_line_popupwindow"
        android:focusableInTouchMode="true"
        android:listSelector="#00000000" />

  

list_item_popupwindow.xml



    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_list_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:minHeight="40dp"
        android:minWidth="120dp"
        android:textColor="@color/popupwindow_list_item_text_selector"
        android:textSize="20sp" />

btn_back_selector.xml




    
    
    
    

btn_operation_selector.xml




    
    
    
    

 

图片资源 

  

转载于:https://www.cnblogs.com/SM-t/p/4350095.html

你可能感兴趣的:(移动开发,java)