防携程筛选,listview和checkbox结合使用

 最近一直做一款旅游的APP,有一个刷选的功能,一看很简单,但是最后搞了很久,日了狗。先不说放图

这里写图片描述!!!!
其实呢原理很简单,就是两个listview 的合并还有listview 和CheckBox的混合使用!!!!!
首先呢原理很简单就是有一个PopupWindow 里面有两个listView,左边一个,右边一个(废话),
点击左边的listview。相应弹出相应右边的listview,然后右边的listview里面还有CheckBox,然后就是防止CheckBox的错乱,点击取消,确定,清除,相应的操作。
首先主布局:

<TextView
    android:id="@+id/textviewClick"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="点击我" />



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

    <ListView
        android:id="@+id/pop_listview_left"
        android:layout_width="0dp"
        android:layout_height="132dp"
        android:layout_weight="21"
        android:divider="#ccc"
        android:dividerHeight="1dp"
        android:scrollbars="none"
        android:scrollingCache="false" >
    </ListView>

    <ListView
        android:id="@+id/pop_listview_right"
        android:layout_width="0dp"
        android:layout_height="132dp"
        android:layout_weight="43"
        android:background="#ffffff"
        android:dividerHeight="0dp"
        android:listSelector="#fff"
        android:scrollbars="none"
        android:scrollingCache="false" />
</LinearLayout>

然后就是PopupWindow的布局,都很简单,哈哈哈!

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="44dp" >

    <TextView
        android:id="@+id/textview_call_off"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="取消"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/textview_eliminate"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="清除筛选"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/textview_confirm"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="确定"
        android:textSize="14sp" />
</LinearLayout>

然后就是两个listview 的两个Adapter:
第一个FirstClassAdapter很简单直接上代码:

public class FirstClassAdapter extends BaseAdapter {
private Context context;
private List list;

public FirstClassAdapter(Context context, List<FirstClassItem> list) {
    this.context = context;
    this.list = list;
}

@Override
public int getCount() {
    return list == null ? 0 : list.size();
}

@Override
public Object getItem(int i) {
    return null;
}

@Override
public long getItemId(int i) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = LayoutInflater.from(context).inflate(
                R.layout.left_listview_item, null);
        holder = new ViewHolder();

        holder.nameTV = (TextView) convertView
                .findViewById(R.id.left_item_name);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    if (position == selectedPosition) {
        convertView.setBackgroundResource(R.color.popup_right_bg);
    } else {
        convertView.setBackgroundResource(R.drawable.selector_left_normal);
    }

    holder.nameTV.setText(list.get(position).getName());

    return convertView;
}

private int selectedPosition = 0;

public void setSelectedPosition(int selectedPosition) {
    this.selectedPosition = selectedPosition;
}

public int getSelectedPosition() {
    return selectedPosition;
}

private class ViewHolder {
    TextView nameTV;
}

}

左边listview的布局:

<TextView
    android:id="@+id/left_item_name"
    android:layout_width="105dp"
    android:layout_height="44dp"
    android:gravity="center"
    android:textSize="14sp" />      

右边的SecondClassAdapter:
public class SecondClassAdapter extends BaseAdapter {
private Context context;
private List list;
private boolean isShow = false;
ArrayList dayscheckTrue = new ArrayList();
ArrayList dayscheckFalse = new ArrayList();
ArrayList timecheckTrue = new ArrayList();
ArrayList timecheckFalse = new ArrayList();
ArrayList pricecheckTrue = new ArrayList();
ArrayList pricecheckFalse = new ArrayList();

public SecondClassAdapter(Context context, List<SecondClassItem> list
        ) {
    this.context = context;
    this.list = list;


}

public boolean isShow() {
    return isShow;
}

public void setShow(boolean isShow) {
    this.isShow = isShow;
}

@Override
public int getCount() {
    return list == null ? 0 : list.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        holder = new ViewHolder();
        convertView = LayoutInflater.from(context).inflate(
                R.layout.right_listview_item, null);
        holder.nameTV = (TextView) convertView
                .findViewById(R.id.right_item_name);
        holder.cb = (CheckBox) convertView.findViewById(R.id.item_cb);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    final SecondClassItem classItem = list.get(position);

    holder.nameTV.setText(list.get(position).getName());

    holder.cb.setChecked(classItem.isChecked());
    return convertView;
}

private class ViewHolder {
    TextView nameTV;
    CheckBox cb;
} 

然后就是两个Bean类:
public class FirstClassItem {
private int id;
private String name;
private List secondList;

public FirstClassItem() {

}

public FirstClassItem(int id, String name, List<SecondClassItem> secondList) {
    this.id = id;
    this.name = name;
    this.secondList = secondList;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public List<SecondClassItem> getSecondList() {
    return secondList;
}

public void setSecondList(List<SecondClassItem> secondList) {
    this.secondList = secondList;
}

@Override
public String toString() {
    return "FirstClassItem{" + "id=" + id + ", name='" + name + '\''
            + ", secondList=" + secondList + '}';
}

第二个,因为第二个有CheckBox 所以我写一个 isChecked 字段,其实listview和checkbox 在一起有很多种解决方式,有兴趣可以看我另一篇博客,介绍一下我用过的方法。

public class SecondClassItem {
private int id;
private String name;
private boolean isChecked;
private int ids;

public int getIds() {
    return ids;
}

public void setIds(int ids) {
    this.ids = ids;
}

public SecondClassItem() {

}

public SecondClassItem(int id, String name, boolean isChecked, int ids) {
    super();
    this.id = id;
    this.name = name;
    this.isChecked = isChecked;
    this.ids = ids;
}

public boolean isChecked() {
    return isChecked;
}

public void setChecked(boolean isChecked) {
    this.isChecked = isChecked;
}

public boolean getChecked(boolean isChecked) {
    return isChecked;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Override
public String toString() {
    return "SecondClassItem{" + "id=" + id + ", name='" + name + '\'' + '}';
}

关键代码就是MainActivity中了,备注很详细,有用的朋友只需改变极少代码就可以使用很简单,上代码:
public class MainActivity extends Activity implements OnClickListener {

/** 左侧一级分类的数据 */
private List<FirstClassItem> firstList;
/** 右侧二级分类的数据 */
private List<SecondClassItem> secondList;
// 二级数据
List<SecondClassItem> list2;
// 保存的数据
private ArrayList<String> dayMap = new ArrayList<String>();
private ArrayList<String> timeMap = new ArrayList<String>();
private ArrayList<String> priceMap = new ArrayList<String>();
// 旅游的列表
private boolean isfrist = true;
private TextView textviewClick;
private PopupWindow popupWindow;
private ListView leftLV, rightLV;

private TextView textview_confirm;
private SecondClassAdapter secondAdapter;
private FirstClassAdapter firstAdapter;

private String val = "";
private String val2 = "";
private String val3 = "";

// 右边显示的数据
private List<String> days = new ArrayList<String>();
private List<String> date = new ArrayList<String>();
private List<String> prices = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();

}

private void initView() {
    textviewClick = (TextView) findViewById(R.id.textviewClick);

    textviewClick.setOnClickListener(this);

}

// 初始化数据,你只要改这里就可以
private void requestList() {
    firstList = new ArrayList<FirstClassItem>();

    days.add("2");
    days.add("3");
    days.add("4");
    days.add("5");
    days.add("6");

    date.add("2016-3-16");
    date.add("2016-3-17");
    date.add("2016-3-18");
    date.add("2016-3-19");

    prices.add("1元");
    prices.add("2元");
    prices.add("3元");
    prices.add("4元");
    // 1
    ArrayList<SecondClassItem> secondList1 = new ArrayList<SecondClassItem>();
    secondList1.add(new SecondClassItem(0, "不限", false, 0));
    for (int i = 0; i < days.size(); i++) {
        secondList1
                .add(new SecondClassItem(i, days.get(i) + "天", false, 0));
    }

    firstList.add(new FirstClassItem(1, "行程天数", secondList1));
    // 2
    ArrayList<SecondClassItem> secondList2 = new ArrayList<SecondClassItem>();
    secondList2.add(new SecondClassItem(0, "不限", false, 0));
    for (int i = 0; i < date.size(); i++) {
        secondList2.add(new SecondClassItem(i, date.get(i), false, 0));
    }
    firstList.add(new FirstClassItem(2, "出发时间", secondList2));
    // 3
    ArrayList<SecondClassItem> secondList3 = new ArrayList<SecondClassItem>();
    secondList3.add(new SecondClassItem(0, "不限", false, 0));

    for (int i = 0; i < prices.size(); i++) {
        secondList3.add(new SecondClassItem(i, prices.get(i), false, 0));
    }
    firstList.add(new FirstClassItem(3, "价格区间", secondList3));

}

private void initPopup() {
    requestList();
    View view = LayoutInflater.from(this).inflate(R.layout.popup_layout,
            null);
    popupWindow = new PopupWindow(view,
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    leftLV = (ListView) view.findViewById(R.id.pop_listview_left);
    rightLV = (ListView) view.findViewById(R.id.pop_listview_right);
    popupWindow.setBackgroundDrawable(new PaintDrawable());
    popupWindow.setFocusable(true);
    // 确定
    textview_confirm = (TextView) view.findViewById(R.id.textview_confirm);
    textview_confirm.setOnClickListener(this);
    // 初始进入都显示第一个
    popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {

            leftLV.setSelection(0);
            rightLV.setSelection(0);
        }
    });

    firstAdapter = new FirstClassAdapter(this, firstList);
    firstAdapter.notifyDataSetChanged();
    leftLV.setAdapter(firstAdapter);

    // 加载左侧第一行对应右侧二级分类
    secondList = new ArrayList<SecondClassItem>();
    secondList.addAll(firstList.get(0).getSecondList());
    secondAdapter = new SecondClassAdapter(this, secondList);
    rightLV.setAdapter(secondAdapter);
    secondAdapter.notifyDataSetChanged();

    // 左侧ListView点击事件
    leftLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // 二级数据
            list2 = firstList.get(position).getSecondList();

            FirstClassAdapter adapter = (FirstClassAdapter) (parent
                    .getAdapter());
            // 如果上次点击的就是这一个item,则不进行任何操作
            if (adapter.getSelectedPosition() == position) {

                return;

            }
            // 根据左侧一级分类选中情况,更新背景色
            // adapter.setSelectedPosition(position);
            // adapter.notifyDataSetChanged();

            // 显示右侧二级分类
            updateSecondListView(list2, secondAdapter);

        }
    });

    // 右侧ListView点击事件
    rightLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // 第一次进入问题右边加载问题
            if (list2 == null || isfrist == true) {

                isfrist = true;

                list2 = firstList.get(0).getSecondList();
                updateSecondListView(list2, secondAdapter);
                SecondClassItem classItem = list2.get(position);
                classItem.setChecked(!classItem.isChecked());
                secondAdapter.notifyDataSetChanged();

            } else {

                SecondClassItem classItem = list2.get(position);

                classItem.setChecked(!classItem.isChecked());
                secondAdapter.notifyDataSetChanged();
            }

            int firstPosition = firstAdapter.getSelectedPosition();
            int firstId = firstList.get(firstPosition).getId();
            int secondId = firstList.get(firstPosition).getSecondList()
                    .get(position).getId();
            String selectedName = firstList.get(firstPosition)
                    .getSecondList().get(position).getName();
            // 写一个方法处理结果
            handleResult(firstId, secondId, selectedName);

        }

    });

}

// 处理点击结果
private void handleResult(int firstId, int secondId, String selectedName) {

    // 相应的添加和删除
    if (firstId == 1) {
        selectedName = selectedName.substring(0, selectedName.length() - 1);
        if (dayMap.contains(selectedName)) {

            dayMap.remove(selectedName);
        } else {

            dayMap.add(selectedName);
        }
        System.out.println(dayMap.toString());
    }
    if (firstId == 2) {
        if (timeMap.contains(selectedName)) {
            timeMap.remove(selectedName);
        } else {
            timeMap.add(selectedName);
        }
        System.out.println(timeMap.toString());
    }
    if (firstId == 3) {

        if (priceMap.contains(selectedName)) {

            priceMap.remove(selectedName);
        } else {
            priceMap.add(selectedName);
        }
        System.out.println(priceMap.toString());

    }

}

// 刷新右侧ListView
private void updateSecondListView(List<SecondClassItem> list2,
        SecondClassAdapter secondAdapter) {
    System.out.println("第二个刷新");
    isfrist = false;
    secondList.clear();

    secondList.addAll(list2);
    secondAdapter.notifyDataSetChanged();
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.textviewClick:
        initPopup();
        boolean isShow = secondAdapter.isShow();// 从适配器里面查看当前页面状态
        secondAdapter.setShow(!isShow);// 点击以后改变CustomAdapter中的标记
        secondAdapter.notifyDataSetChanged();// 通知适配器数据更新了,改变界面
        if (popupWindow.isShowing()) {
            popupWindow.dismiss();
        } else {
            isfrist = true;
            popupWindow.showAsDropDown(findViewById(R.id.textviewClick));
            popupWindow.setAnimationStyle(-1);
        }
        break;
    case R.id.textview_confirm:
        // 点击确定时候处理得到结果
        // 清除数据
        val = "";
        val2 = "";
        val3 = "";
        for (int i = 0; i < dayMap.size(); i++) {
            String day = dayMap.get(i);
            if (day.equals("不")) {
                val = "";
                break;
            } else {
                val = val + "," + day;

            }
        }
        for (int i = 0; i < timeMap.size(); i++) {
            String day = timeMap.get(i);
            if (day.equals("不限")) {
                val2 = "";
                break;
            } else {
                val2 = val2 + "," + day;

            }
        }
        for (int i = 0; i < priceMap.size(); i++) {
            String day = priceMap.get(i);
            System.out.println("day" + day);
            if (day.equals("不限")) {
                val3 = "";
                break;
            } else {
                val3 = "" + val3 + "," + day;

            }
        }
        if (val.length() != 0) {
            val = val.substring(1, val.length());
        } else {
            val = "";
        }
        if (val2.length() != 0) {
            val2 = val2.substring(1, val2.length());
        } else {
            val2 = "";
        }
        if (val3.length() != 0) {
            val3 = val3.substring(1, val3.length());
        } else {
            val3 = "";
        }
        // 这里就是你点击后的逻辑
        Toast.makeText(MainActivity.this,
                "选择的数据天数" + val + "----日期" + val2 + "-----价格区间" + val3,
                Toast.LENGTH_LONG).show();

        popupWindow.dismiss();
        break;
    default:
        break;
    }

}

}

然后清除筛选和取消类似,简单吧。。。。。。。。。。。。。。
最后介绍一个群,每天都有干活。喜欢Lol和android的朋友可以加一下:Android晋级群 225274452
最后你们要的链接,一切就是俩字,免费。。。。。。
http://download.csdn.net/detail/u010786471/9462234

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页