pupwindow列表

来源:互联网 发布:java object转date 编辑:程序博客网 时间:2024/06/04 10:32
整体思路
1.首先添加butterknife的依赖,取消掉ActionBar,使用toolbar代替2.完成整体的布局,初始化控件,设置点击事件3.初始化popwindow所要显示的数据4.初始化popwindow控件的设置5.popwindowlistview相关联6.三个popwindow所依附的linearlayout,根据点击事件,做对应逻辑处理(改变textview的颜色,显示效果)
main布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent" android:layout_height="match_parent">     <android.support.v7.widget.Toolbar         android:background="@color/colorAccent"         android:layout_width="match_parent"         android:layout_height="45dp">         <TextView             android:textSize="20sp"             android:text="周边"             android:layout_gravity="center_horizontal"             android:textColor="@color/white"             android:layout_width="wrap_content"             android:layout_height="wrap_content" />     </android.support.v7.widget.Toolbar>    <!--从这里开始,就是自定义多条件筛选菜单控件的核心所在-->    <View        android:layout_width="match_parent"        android:layout_height="1dp"        android:background="#E2E2E2"        />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="40dp"        android:background="@color/white"        android:orientation="horizontal"        >        <LinearLayout            android:id="@+id/supplier_list_product"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:gravity="center"            android:orientation="horizontal"            >            <TextView                android:id="@+id/supplier_list_product_tv"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="全部"                android:textSize="14dp"                />            <ImageView                android:layout_width="25dp"                android:layout_height="25dp"                android:src="@drawable/t1"/>        </LinearLayout>        <LinearLayout            android:id="@+id/supplier_list_sort"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:gravity="center"            android:orientation="horizontal"            >            <TextView                android:id="@+id/supplier_list_sort_tv"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="综合排序"                android:textSize="14dp"                />            <ImageView                android:layout_width="25dp"                android:layout_height="25dp"                android:src="@drawable/t1"/>        </LinearLayout>        <LinearLayout            android:id="@+id/supplier_list_activity"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:gravity="center"            android:orientation="horizontal"            >            <TextView                android:id="@+id/supplier_list_activity_tv"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="优惠活动"                android:textSize="14dp"                />            <ImageView                android:layout_width="25dp"                android:layout_height="25dp"                android:src="@drawable/t1"/>        </LinearLayout>    </LinearLayout>    <View        android:layout_width="match_parent"        android:layout_height="1dp"        android:background="#E2E2E2"        />    <ListView        android:id="@+id/supplier_list_lv"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_marginTop="10dp"        android:cacheColorHint="#00000000"        android:divider="#f0f0f0"        android:dividerHeight="10dp"        android:fadingEdge="none"        android:listSelector="#00000000"        android:scrollbarStyle="outsideOverlay"        android:scrollingCache="false"        >    </ListView></LinearLayout>
item_listview_popwindow布局
<LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="#ffffff"    android:orientation="vertical" >    <TextView        android:id="@+id/listview_popwind_tv"        android:layout_width="match_parent"        android:layout_height="45dp"        android:layout_marginLeft="15dp"        android:layout_marginRight="15dp"        android:gravity="center_vertical"        android:text="地点"        android:textColor="#5a5959"        android:textSize="18dp" />    <View        android:layout_width="match_parent"        android:layout_height="1dp"        android:layout_marginLeft="15dp"        android:layout_marginRight="15dp"        android:background="#E2E2E2" /></LinearLayout>
popwin_list布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:background="#5000"    android:layout_height="match_parent">    <ListView        android:id="@+id/popwin_supplier_list_lv"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:cacheColorHint="#00000000"        android:divider="#0000"        android:dividerHeight="0dp"        android:fadingEdge="none"        android:listSelector="#00000000"        android:scrollbarStyle="outsideOverlay"        android:scrollbars="none"        android:scrollingCache="false"        >    </ListView>    <LinearLayout        android:id="@+id/popwin_supplier_list_bottom"        android:layout_width="match_parent"        android:layout_weight="1"        android:layout_height="0dp"        android:orientation="vertical"        >    </LinearLayout></LinearLayout>
main代码
@Bind(R.id.supplier_list_product_tv)    TextView mProductTv;  // 可以修改名称    @Bind(R.id.supplier_list_product)    LinearLayout mProduct;    @Bind(R.id.supplier_list_sort_tv)    TextView mSortTv;     // 可以修改名称    @Bind(R.id.supplier_list_sort)    LinearLayout mSort;    @Bind(R.id.supplier_list_activity_tv)    TextView mActivityTv;   // 可以修改名称    @Bind(R.id.supplier_list_activity)    LinearLayout mActivity;    @Bind(R.id.supplier_list_lv)    ListView mSupplierListLv;    private ArrayList<Map<String, String>> menuData1;    private ArrayList<Map<String, String>> menuData2;    private ArrayList<Map<String, String>> menuData3;    private PopupWindow mPopMenu;    private ListView mpoplistview;    private SimpleAdapter mMenuAdapter1;    private SimpleAdapter mMenuAdapter2;    private SimpleAdapter mMenuAdapter3;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ButterKnife.bind(this);        //初始化popwindow所要显示的数据initData();        //初始化popwindow控件        initPopMenu();    }//初始化popwindow    private void initPopMenu() {        //把包裹listview布局的xml文件转换为view对象        View popview = LayoutInflater.from(this).inflate(R.layout.popwin_list, null);   //创建popwindow对象,参数1 popwindow要显示的布局  参数2 3  定义popwindow宽和高        mPopMenu = new PopupWindow(popview, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);    //设置popwindow外部可以点击        mPopMenu.setOutsideTouchable(true);        //设置popwindow里面的listview有焦点        mPopMenu.setFocusable(true);        //如果想让popwindow有动画效果,就必须有这行代码        mPopMenu.setBackgroundDrawable(new ColorDrawable());    //设置popwindow结束时的监听        mPopMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {            @Override            public void onDismiss() {                //设置TextView的颜色,把所有LinearLayout的文本颜色该为灰色                mProductTv.setTextColor(Color.parseColor("#5a5959"));                mSortTv.setTextColor(Color.parseColor("#5a5959"));                mActivityTv.setTextColor(Color.parseColor("#5a5959"));            }        });        //设置点击popwindow以外的地方,使popwindow消失        LinearLayout list_bottom = (LinearLayout) popview.findViewById(R.id.popwin_supplier_list_bottom);        list_bottom.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                //当点击到灰色区域时 popwindow消失                mPopMenu.dismiss();            }        });        //获取listview对象        mpoplistview = (ListView) popview.findViewById(R.id.popwin_supplier_list_lv);        //创建simpleadapter,一个listview安卓原生封装的适配器        mMenuAdapter1 = new SimpleAdapter(this, menuData1, R.layout.item_listview_popwindow, new String[]{"name"}, new int[]{R.id.listview_popwind_tv});        mMenuAdapter2 = new SimpleAdapter(this, menuData2, R.layout.item_listview_popwindow, new String[]{"name"}, new int[]{R.id.listview_popwind_tv});        mMenuAdapter3 = new SimpleAdapter(this, menuData3, R.layout.item_listview_popwindow, new String[]{"name"}, new int[]{R.id.listview_popwind_tv});//设置popwindow里的listview点击事件,当点击listview里的一个item,把这个item数据显示到最上方        mpoplistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {    @Override    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {//首先让popwindow消失        mPopMenu.dismiss();        switch (menuIndex){            case 0:                String currentProduct = menuData1.get(i).get("name");                mProductTv.setText(currentProduct);                break;            case 1:                String currentsort = menuData2.get(i).get("name");                mSortTv.setText(currentsort);                break;            case 2:                String currentActivity = menuData3.get(i).get("name");                mActivityTv.setText(currentActivity);                break;        }    }});    }//设置一个标记,方便对点击不同linearlayout做相应操作    private int menuIndex=0;    //初始化数据,popwindow所需,一共有三个,所以我要封装好三个数据,这里是假数据,真实数据从网上获取    private void initData() {//创建一个存放popwindow加载数据的大盒子,map集合(,)        menuData1 = new ArrayList<>();        //存放String的字符串数组        String[] menuStr1 = new String[]{"全部", "粮油", "衣服", "图书", "电子产品",                "酒水饮料", "水果"};        //创建一个小盒子,放编号和值        Map<String,String> map1;        for (int x=0;x<menuStr1.length;x++){            map1=new HashMap<String, String>();            map1.put("name",menuStr1[x]);            menuData1.add(map1);        }        //创建一个存放popwindow加载数据的大盒子,map集合(,)        menuData2 = new ArrayList<>();        //存放String的字符串数组        String[] menuStr2 = new String[]{"综合排序", "配送费最低"};        //创建一个小盒子,放编号和值        Map<String,String> map2;        for (int x=0;x<menuStr2.length;x++){            map2=new HashMap<String, String>();            map2.put("name",menuStr2[x]);            menuData2.add(map2);        }        //创建一个存放popwindow加载数据的大盒子,map集合(,)        menuData3 = new ArrayList<>();        //存放String的字符串数组        String[] menuStr3 = new String[]{"优惠活动", "特价活动", "免配送费",                "可在线支付"};        //创建一个小盒子,放编号和值        Map<String,String> map3;        for (int x=0;x<menuStr3.length;x++){            map3=new HashMap<String, String>();            map3.put("name",menuStr3[x]);            menuData3.add(map3);        }        //测试是否有问题        String name = menuData3.get(0).get("name");        System.out.println("name----"+name);    }    @OnClick({R.id.supplier_list_product, R.id.supplier_list_sort, R.id.supplier_list_activity})    public void onClick(View view) {        switch (view.getId()) {            //第一个popwindow所执行的点击后的逻辑            case R.id.supplier_list_product:                //设置其textview点击是红色                mProductTv.setTextColor(Color.RED);                //设置popwindow里的lisyview适配器                mpoplistview.setAdapter(mMenuAdapter1);                //popwindow显示出来  参数1.view对象  决定了popwindow在哪个控件下显示                //参数2 3  决定了popwindow的坐标  x y                mPopMenu.showAsDropDown(mProduct,0,2);                menuIndex=0;                break;            case R.id.supplier_list_sort:                mSortTv.setTextColor(Color.RED);                mpoplistview.setAdapter(mMenuAdapter2);                mPopMenu.showAsDropDown(mProduct,0,2);                menuIndex=1;                break;            case R.id.supplier_list_activity:                mActivityTv.setTextColor(Color.RED);                mpoplistview.setAdapter(mMenuAdapter3);                mPopMenu.showAsDropDown(mProduct,0,2);                menuIndex=2;                break;        }    }

原创粉丝点击