关闭

PopupWindow仿淘宝排序选择,多个切换

标签: PopupWindo数据切换仿淘宝排序
142人阅读 评论(0) 收藏 举报
分类:

本人写这些主要是自己记不住所以把一些可能以后会用到的控件或者是功能写上来,写的不好大家不要见怪。
这里写图片描述
这里写图片描述
点击旅游类型或者是天数的时候会进行PopupWindow里面的数据切换,不会关闭PopupWindow,再次点击的时会关闭PopupWindow,点击外部区域或者是选择内容之后都会关闭,
下面是主要代码main里面的代码

public class MainActivity extends Activity implements OnClickListener,
        OnTraveLineClick {

    private LinearLayout ll_city;// 定位
    private LinearLayout ll_day;// 天数
    private LinearLayout ll_type;// 类型

    private TextView city_type;// 类型
    private TextView city_day;// 天数

    private View allView;

    private TravelLineAllPopupView popupWindow;

    private List<Integer> dayList;
    private List<String> typeList;

    /** 类型 */
    private String themeCode = "";
    /** 设置默认天数:全部 */
    private int travelDays = -1;

    /** 哪一个控件show出来的PopupWindow */
    private int isViewClick = -1;
    /** PopupWindow是否显示 1:显示,2:未显示 */
    private int isViewVisibility = 2;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_main);
        findViews();
        initViews();
        // 添加天数
        addDayList();
        // 添加主题类型
        addTypeList();

    }

    protected void findViews() {
        ll_city = (LinearLayout) findViewById(R.id.ll_city);
        ll_day = (LinearLayout) findViewById(R.id.ll_day);
        ll_type = (LinearLayout) findViewById(R.id.ll_type);
        city_day = (TextView) findViewById(R.id.city_day);
        city_type = (TextView) findViewById(R.id.city_type);
        allView = (View) findViewById(R.id.allView);

    }

    protected void initViews() {
        ll_city.setOnClickListener(this);
        ll_day.setOnClickListener(this);
        ll_type.setOnClickListener(this);

        popupWindow = new TravelLineAllPopupView(this);
        popupWindow.setTraveLineClick(this);

    }

    /**
     * 判断是否,是某一个控件点击之后,show出来的PopupWindow
     * 
     * @param v
     *            控件id
     * 
     * */
    public boolean isTrue(View v) {
        // 如果点击的是这个控件,并且PopupWindow是这个控件show出来的
        /** 如果有多个甚至超过2个用 else if() 最后需要else{return false} */
        if ((v.getId() == R.id.ll_day && isViewClick == 1)
                || (v.getId() == R.id.ll_type && isViewClick == 2)) {
            return true;
        } else {
            return false;
        }

    }

    @Override
    public void onClick(View v) {

        // 第一次进来isTrue返回false,isViewVisibility是2所以两个条件都不通过,需要show出PopupWindow;

        // 如果有多个控件点击判断建议写一个方法
        // 如果点击的控件不是这两个其中一个,并且件没有展示PopupWindow,并且PopupWindow是被点击的控件show的出来的
        if ((v.getId() == R.id.ll_day || v.getId() == R.id.ll_type)
                && isViewVisibility == 2 && isTrue(v)) {
            isViewVisibility = 1;
        } else if ((v.getId() != R.id.ll_day || v.getId() != R.id.ll_type)
                && isViewVisibility == 1) {
            isViewVisibility = 2;
        }

        switch (v.getId()) {
        case R.id.ll_day:
            // 选择天数
            // 第一次点击显示PopupWindow
            if (dayList != null) {
                popupWindow.setData(dayList, null, 1);
                // 如果PopupWindow不是null,并且已经显示,并且是这个控件show出来的PopupWindow
                if (null != popupWindow && isViewVisibility == 1 && isTrue(v)) {
                    popupWindow.dismiss();
                } else {
                    popupWindow.showAsDropDown(allView);
                }
            }
            // 如果上一次不是这个控件show出的PopupWindow
            if (isViewClick != 1) {
                isViewClick = 1;
            }
            break;
        case R.id.ll_type:
            // 选择类型

            if (typeList != null) {
                popupWindow.setData(null, typeList, 2);
                // 如果PopupWindow不是null,并且已经显示,并且是这个控件show出来的PopupWindow
                if (null != popupWindow && isViewVisibility == 1 && isTrue(v)) {
                    popupWindow.dismiss();
                } else {
                    popupWindow.showAsDropDown(allView);
                }
            }
            // 如果上一次不是这个控件show出的PopupWindow
            if (isViewClick != 2) {
                isViewClick = 2;
            }

            break;

        default:
            break;
        }

    }

    public void addDayList() {
        dayList = new ArrayList<Integer>();
        dayList.add(2);
        dayList.add(3);
        dayList.add(4);
        dayList.add(5);
        dayList.add(6);
        dayList.add(7);
        dayList.add(8);
        dayList.add(-1);
    }

    public void addTypeList() {
        typeList = new ArrayList<String>();
        typeList.add("亲子旅游");
        typeList.add("结婚旅游");
        typeList.add("独自旅游");
        typeList.add("西瓜旅游");
        typeList.add("乡村旅游");
        typeList.add("国外旅游");
    }

    @Override
    public void onTraveLineClick(int position, int type) {
        // 如果是选择天数show出来的PopupWindow
        if (type == 1) {
            // 如果选择的是全部
            if (dayList.get(position) == -1) {
                city_day.setText("全部");
            } else {
                city_day.setText(dayList.get(position) + "天");
            }
            // 获取天数,默认是全部
            travelDays = dayList.get(position);
        } else {
            themeCode = typeList.get(position);

            city_type.setText(typeList.get(position));
        }
        if (themeCode != null && !"".equals(themeCode)) {
            // 获取数据

        }
    }

}

这里是main的布局

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#FFFFFF"
        android:orientation="horizontal" >

        <LinearLayout
            android:id="@+id/ll_city"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_vertical|left"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/city_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="请选择"
                android:textColor="#222222"
                android:textSize="15sp" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:background="@drawable/open" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_day"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/city_day"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="全部"
                android:textColor="#222222"
                android:textSize="15sp" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:background="@drawable/open" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_type"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_vertical|right"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/city_type"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp"
                android:singleLine="true"
                android:text="类型"
                android:textColor="#222222"
                android:textSize="15sp" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:background="@drawable/open" />
        </LinearLayout>
    </LinearLayout>

    <View
        android:id="@+id/allView"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#f0f0f0" />

这里是PopupWindow代码

public class TravelLineAllPopupView extends PopupWindow {

    private OnTraveLineClick traveLineClick;
    private Activity context;

    private View mView;
    // ListView展示所需要的适配器
    private TravelLinePopupAdapter adapter;
    private ListView mListView;

    private int type;

    // 为适配器添加或者刷新数据
    public void setData(List<Integer> list, List<String> typeList, int type) {
        this.type = type;
        if (list != null) {
            adapter.setList(list, type);
            adapter.notifyDataSetChanged();
        } else if (typeList != null) {
            adapter.setTypeList(typeList, type);
            adapter.notifyDataSetChanged();
        } else {

        }
    }

    public TravelLineAllPopupView(final Activity context) {
        super(context);
        this.context = context;

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.travel_line_all_popup_view, null);

        mListView = (ListView) mView.findViewById(R.id.mListView);

        adapter = new TravelLinePopupAdapter(context);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // 接口回调
                traveLineClick.onTraveLineClick(position, type);
                // 关闭PopupWindow
                dismiss();
            }
        });

        // 设置PopupWindow的View
        this.setContentView(mView);
        // 设置PopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.MATCH_PARENT);
        // 设置PopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        // 如果PopupWindow区域外有多个控件点击需要设置成flase,区域外的点击才会响应
        // 设置PopupWindow弹出窗体可点击
        this.setFocusable(false);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        // this.setAnimationStyle(R.style.LineAnimation);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOutsideTouchable(true);
        this.setTouchable(true);

    }

    public void setTraveLineClick(OnTraveLineClick traveLineClick) {
        this.traveLineClick = traveLineClick;
    }

    public interface OnTraveLineClick {
        public void onTraveLineClick(int position, int type);
    }

}

这里是PopupWindow布局代码

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <com.example.popupwindowdemo.ListViewExt
                android:id="@+id/mListView"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:divider="@null"
                android:listSelector="@null"
                android:scrollbars="none" >
            </com.example.popupwindowdemo.ListViewExt>
        </LinearLayout>
    </ScrollView>

这里是ListView控件的代码

/**
 * listView内嵌套ScrollView显示不全
 * 
 * @author yunjiezhou
 * */
public class ListViewExt extends ListView {

    public ListViewExt(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public ListViewExt(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }



    public ListViewExt(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 根据模式计算每个child的高度和宽度
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

}

下面是adapter适配器的代码

public class TravelLinePopupAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater mInflater;
    // 将两个list类型换成自己的
    private List<Integer> list;
    private List<String> typeList;
    private int type;

    public TravelLinePopupAdapter(Context context) {
        this.context = context;

        mInflater = LayoutInflater.from(context);
    }

    public void setList(List<Integer> list, int type) {
        this.list = list;
        this.type = type;
    }

    public void setTypeList(List<String> list, int type) {
        this.typeList = list;
        this.type = type;
    }

    @Override
    public int getCount() {
        if (type == 1) {
            if (list != null) {
                return list.size();
            } else {
                return 0;
            }
        } else {
            if (typeList != null) {
                return typeList.size();
            } else {
                return 0;
            }

        }
    }

    @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 holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.travel_line_popup_item,
                    null);
            holder = new ViewHolder();
            holder.day = (TextView) convertView.findViewById(R.id.day);
            holder.type = (TextView) convertView.findViewById(R.id.type);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (type == 1) {
            holder.day.setVisibility(View.VISIBLE);
            holder.type.setVisibility(View.GONE);
            if (list.get(position) == -1) {
                holder.day.setText("全部");
            } else {
                holder.day.setText(list.get(position) + "天");
            }
        } else if (type == 2) {
            holder.day.setVisibility(View.GONE);
            holder.type.setVisibility(View.VISIBLE);
            holder.type.setText(typeList.get(position));
        }

        return convertView;
    }

    class ViewHolder {
        TextView day;
        TextView type;
    }

}

这里是适配器布局代码


    <TextView
        android:id="@+id/day"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#ffffff"
        android:gravity="center"
        android:textColor="#222222"
        android:textSize="15sp"
        android:visibility="gone" />

    <TextView
        android:id="@+id/type"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#ffffff"
        android:gravity="right|center_vertical"
        android:paddingRight="10dp"
        android:textColor="#222222"
        android:textSize="15sp"
        android:visibility="gone" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#f0f0f0" />

所有布局我全部用的是LinearLayout
这里是源码

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2968次
    • 积分:168
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:6篇
    • 译文:0篇
    • 评论:0条