popupwindow的二级Listview的联动菜单、ListView形式的菜单、GridView形式的菜单的Util

popupwindow的二级Listview的联动菜单、ListView形式的菜单、GridView形式的菜单的Util。
资源地址:http://download.csdn.net/detail/izheer/9642741
popupwindow的子布局无外乎就是那几个形式:listview、gridview、listview二级联动等。因此,我将它们都写成了util类,只需要将item的onClick事件单独引出来,使用者将数据源以及控件对象传入即可。具体效果见下:
popupwindow的二级Listview的联动菜单、ListView形式的菜单、GridView形式的菜单的Util_第1张图片popupwindow的二级Listview的联动菜单、ListView形式的菜单、GridView形式的菜单的Util_第2张图片
popupwindow的二级Listview的联动菜单、ListView形式的菜单、GridView形式的菜单的Util_第3张图片

二级联动Listview菜单
util类:

package izheer.com.utils;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import java.util.List;

import izheer.com.popupwindowutil.R;

/**
 * 二级菜单 popWindow
 * Created by Administrator on 2016/9/19.
 */
public abstract class DoubleListPopViewUtil {

    private Context context;
    private View view;//在哪个view的下面
    //数据源
    private List roots;//根目录的节点
    private List> sub_items;// 子目录节点
    private PopupWindow areaPopupWindow;
    private RootListViewAdapter rootAdapter;
    private SubListViewAdapter subAdapter;

    public abstract void onRootListviewOnClick(View v, int position);//根布局的item的点击事件
    public abstract void onSubListviewOnClick(View v, int position);//二级菜单的item的点击事件

    public DoubleListPopViewUtil(Context context, View view, List roots, List> sub_items) {
        this.context = context;
        this.view = view;
        this.roots = roots;
        this.sub_items = sub_items;
        init();
    }

    private  void init(){
        LayoutInflater inflater = LayoutInflater.from(context);
        final LinearLayout areaPopupLayout = (LinearLayout) inflater.inflate(R.layout.area_popupwindow_layout, null, false);
        final FrameLayout subAreaLayout = (FrameLayout) areaPopupLayout.findViewById(R.id.sub_popupwindow);
        FrameLayout rootAreaLayout = (FrameLayout) areaPopupLayout.findViewById(R.id.root_popupwindow);
        rootAreaLayout.setBackgroundResource(R.color.white);
        ListView rootAreaListView = (ListView) areaPopupLayout.findViewById(R.id.root_listview);
        ListView subAreaListView = (ListView) areaPopupLayout.findViewById(R.id.sub_listview);

        rootAdapter = new RootListViewAdapter(context);
        rootAdapter.setItems(roots);
        rootAreaListView.setAdapter(rootAdapter);
        subAdapter = new SubListViewAdapter(context, sub_items);
        subAreaListView.setAdapter(subAdapter);
        //弹出popupwindow时,二级菜单默认隐藏,当点击某项时,二级菜单再弹出
        subAreaLayout.setVisibility(View.INVISIBLE);
        areaPopupWindow = new PopupWindow(areaPopupLayout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        //使点击popupwindow以外的区域时popupwindow自动消失须放在showAsDropDown之前
        areaPopupWindow.setBackgroundDrawable(new BitmapDrawable());
 // 参数:2:左右的偏移量,正数向左偏移,负数向右偏移,参数3:上下方向的偏移量,正数向下偏移,负数向上偏移
        areaPopupWindow.showAsDropDown(view, -5, 5);
        areaPopupWindow.update();
        rootAreaListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                // TODO Auto-generated method stub
                //选中root某项时改变该ListView item的背景色
                rootAdapter.setSelectedPosition(position);
                rootAdapter.notifyDataSetInvalidated();
                subAdapter.setRoot_position(position);
                subAdapter.notifyDataSetChanged();
                //选中某个根节点时,使显示相应的子目录可见
                subAreaLayout.setVisibility(View.VISIBLE);
               onRootListviewOnClick(view,position);
            }
        });
        subAreaListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(
                    AdapterView parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
               onSubListviewOnClick(view,position);
            }
        });
    }
    public void notifyDataSetChangedRoots(List roots){
        this.roots = roots;
        rootAdapter.setItems(roots);
        rootAdapter.notifyDataSetChanged();
    }
    public void notifyDataSetChangedSubItem(List>  sub_items){
        this.sub_items = sub_items;
        subAdapter.setItems(sub_items);
        subAdapter.notifyDataSetChanged();
    }

    public void show(){
        areaPopupWindow.showAsDropDown(view);
    }
    public void dismiss(){
        if (areaPopupWindow != null && areaPopupWindow.isShowing()){
            areaPopupWindow.dismiss();
        }
    }
}

在Activity中使用:

/**
 * 地区选择的popupwindow
 */
    private void showAreaPopBtn() {
        areaPopupWindow = new DoubleListPopViewUtil(this, selectArea, rootList, subItemList) {
            @Override
            public void onRootListviewOnClick(View v, int position) {
                selectedPosition = position;
            }

            @Override
            public void onSubListviewOnClick(View v, int position) {
                selectArea.setText(subItemList.get(selectedPosition).get(position));
                areaPopupWindow.dismiss();
            }
        };
        areaPopupWindow.show();
    }

ListView形式菜单
Util类

package izheer.com.utils;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;

import izheer.com.popupwindowutil.R;


/**
 * 布局为Listview的popupwindow
 * Created by Administrator on 2016/9/4.
 */
public abstract class ListViewPopViewUtil {

    private Context context;
    private View view;//在哪个view的下面
    private ListView listView;
    private PopupWindow popupWindow;
    private String[] strs;//数据源

    public abstract void onListviewOnclick(View v, int position);

    public ListViewPopViewUtil(Context context, View view, String[] strs) {
        this.context = context;
        this.view = view;
        this.strs = strs;
        init();
    }

    private void init(){
        LayoutInflater inflater = LayoutInflater.from(context);
        View popView = inflater.inflate(R.layout.list_pop_layout,null);
        listView = (ListView) popView.findViewById(R.id.distance_listview);
        popupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        listView.setAdapter(new ArrayAdapter<>(context,R.layout.text_item_layout, android.R.id.text1,strs));

        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                onListviewOnclick(view,position);
            }
        });
    }
    public void show(){
        popupWindow.showAsDropDown(view);
    }
    public void dismiss(){
        if (popupWindow != null && popupWindow.isShowing()){
            popupWindow.dismiss();
        }
    }
}

在Activity中使用

    /**
     * 距离的popupwindow
     */
    private void showDistancePop() {
        distancePopview = new ListViewPopViewUtil(this, distance, distanceStrs){
            @Override
            public void onListviewOnclick(View v, int position) {
                distance.setText(distanceStrs[position]);
                distancePopview.dismiss();
            }
        };
        distancePopview.show();
    }

GridView形式菜单
Util类

package izheer.com.utils;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.PopupWindow;

import izheer.com.popupwindowutil.R;


/**
 * 布局为Gridview的popupwindow
 * Created by Administrator on 2016/9/4.
 */
public abstract class GridviewPopViewUtil {
    private Context context;
    private String[] strs;
    private View view;//在哪个view的下面
    private GridView gridview;
    private PopupWindow popupWindow;

    public abstract void onGridviewOnclick(View v,int position);

    public GridviewPopViewUtil(Context context, View view, String[] strs) {
        this.context = context;
        this.strs = strs;
        this.view = view;
        init();
    }

    private void init(){
        LayoutInflater inflater = LayoutInflater.from(context);
        View popView = inflater.inflate(R.layout.gridview_pop_layout,null);
        gridview = (GridView)popView.findViewById(R.id.gridview_popupview);

        popupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        gridview.setAdapter(new ArrayAdapter<>(context,R.layout.text_item_layout, android.R.id.text1,strs));

        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                onGridviewOnclick(view,position);
            }
        });
    }

    public void show(){
        popupWindow.showAsDropDown(view);
    }
    public void dismiss(){
        if (popupWindow != null && popupWindow.isShowing()){
            popupWindow.dismiss();
        }
    }
}

在Activity中使用

    /**
     * 排序选择popupwindow
     */
    private void showSortPop() {
        sortPopview = new GridviewPopViewUtil(this,sort, sortStrs) {
            @Override
            public void onGridviewOnclick(View v, int position) {
                sort.setText(sortStrs[position]);
                sortPopview.dismiss();
            }
        };
        sortPopview.show();
    }

本文的详细的代码见文中开始的链接。

你可能感兴趣的:(popupwindow的二级Listview的联动菜单、ListView形式的菜单、GridView形式的菜单的Util)