ListView结合PopupWindow实现了左侧导航栏联动效果

ListView结合PopupWindow实现了左侧导航栏联动效果_第1张图片
activity_linkage_pop





    

    

    

layout_pop_window_city


import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
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 android.widget.TextView;

import com.thomas.android.base.R;
import com.thomas.tool.utility.DisplayUtil;

public class LinkagePopActivity extends AppCompatActivity {
    private String[] province = new String[] {"直辖市", "特别行政区","黑龙江"};
    private String[][] city = new String[][]{{"北京","上海","天津","重庆"},{"香港","澳门"},
            {"哈尔滨","齐齐哈尔","牡丹江","大庆","伊春","双鸭山","鹤岗","鸡西","佳木斯","七台河","黑河","绥化","大兴安岭"}};

    private ListView province_list;
    private PopupWindow cityWindow;
    private TextView tv_show_city;
    private ArrayAdapter cityAdapter;
    private int provinceIndex;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_linkage_pop);
        province_list = findViewById(R.id.province_list);
        tv_show_city = findViewById(R.id.tv_show_city);
        
        initView();
    }

    private void initView() {
        initCityView();
        initProvinceView();
    }

    private void initProvinceView() {
        province_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                provinceIndex = position;
                cityAdapter.clear();
                cityAdapter.addAll(city[position]);
                cityWindow.showAsDropDown(province_list,view.getMeasuredWidth(),-province_list.getMeasuredHeight());

            }
        });
        province_list.setAdapter(new ArrayAdapter<>(this,R.layout.support_simple_spinner_dropdown_item,province));
    }

    /**
     * pop window is city
     */
    private void initCityView() {
        ListView city_list = (ListView) View.inflate(this,R.layout.layout_pop_window_city,null);
        cityAdapter = new ArrayAdapter<>(this,R.layout.support_simple_spinner_dropdown_item);
        city_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                tv_show_city.setText(city[provinceIndex][position]);
                cityWindow.dismiss();
            }
        });
        city_list.setAdapter(cityAdapter);
        // set list to popWindow
        cityWindow = new PopupWindow(city_list, DisplayUtil.dpToPx(this,96), ViewGroup.LayoutParams.WRAP_CONTENT);
        ColorDrawable dw = new ColorDrawable(Color.TRANSPARENT);
        cityWindow.setBackgroundDrawable(dw);
        cityWindow.setOutsideTouchable(true);

    }


}

你可能感兴趣的:(ListView结合PopupWindow实现了左侧导航栏联动效果)