android如何在listview中没有选中复选框时隐藏linearlayout

时间:2013-01-22 22:55:44

标签: android android-layout

我有一个listview的布局,在它下面有水平布局,有两个按钮水平,我想当没有选中复选框时两个按钮隐藏,我尝试制作但我的代码使两个按钮隐藏和黑色而不是它们出现了,当检查一个checkbos时,两个按钮没有出现,为什么?

class RestaurantAdapter extends BaseAdapter {
    private ArrayList<Boolean> status = new ArrayList<Boolean>();
    private static LayoutInflater inflater = null;
    private ArrayList<HashMap<String, String>> data;
    Activity activity;
    LinearLayout layout;
public RestaurantAdapter(Activity activity,
        ArrayList<HashMap<String, String>> data, LinearLayout layout) {
    this.layout = layout;
    this.activity = activity;
    this.data = data;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    for (int i = 0; i < data.size(); i++) {
        status.add(false);
    }
}
public int getCount() {
    return data.size();
}
public Object getItem(int position) {
    return position;
}
public long getItemId(int position) {
    return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (vi == null)
        vi = inflater.inflate(R.layout.restaurant_multi_select_list_item,
                null);
    TextView name = (TextView) vi
            .findViewById(R.id.restaurant_multi_select_title);
    ImageView image = (ImageView) vi
            .findViewById(R.id.restaurant_multi_select_list_item_image);
    CheckBox cb = (CheckBox) vi
            .findViewById(R.id.restaurant_multi_select_checkBox);
    cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            if (isChecked) {
                status.set(position, true);
            } else {
                status.set(position, false);
            }
        }
    });
    cb.setChecked(status.get(position));
    boolean allFalse = true;
    int j = 0;
    while (j < status.size() && allFalse) {
        if (status.get(j))
            allFalse = false;
        j++;
    }
    if (allFalse)
        layout.setVisibility(LinearLayout.INVISIBLE);
    else
        layout.setVisibility(LinearLayout.VISIBLE);

    HashMap<String, String> restaurant = data.get(position);
    name.setText(restaurant.get("name"));

    image.setImageResource(Integer.parseInt(restaurant.get("image")));
    return vi;
}

} 检查allFalse变量

主要活动

layout = (LinearLayout) findViewById(R.id.llButtons);

        lvRestaurants = (ListView) findViewById(R.id.lvRestaurants);

        ArrayList<HashMap<String, String>> alist = new ArrayList<HashMap<String, String>>();
        for (int i = 0; i < restaurants.length; i++) {
            HashMap<String, String> hm = new HashMap<String, String>();
            hm.put("image", R.drawable.mcdonalds + "");
            hm.put("name", restaurants[i]);
            alist.add(hm);
        }
        RestaurantAdapter ra = new RestaurantAdapter(this, alist, layout);
        lvRestaurants.setAdapter(ra);

这是xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/lvRestaurants"
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="0dip" >
    </ListView>

    <LinearLayout
        android:id="@+id/llButtons"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

        android:orientation="horizontal" >

        <Button
            android:id="@+id/bDone"
            android:layout_width="153dp"
            android:layout_height="match_parent"
            android:text="Done" />

        <Button
            android:id="@+id/bCancel"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Cancel" />
    </LinearLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:3)

使用setVisibility(View.GONE)不仅可以隐藏按钮,还可以停止占用布局中的空间,如果这是你所说的“并且会出现黑色而不是”。

至于它们在检查时不会重新出现,您需要在status对象上添加一个监听器或调用ra.notifyDataSetChanged()重新绘制listview(这将导致onDraw( )逻辑被执行)。

编辑:最简单的方法是在适配器外声明你的onCheckedChangeListener并使用它。使用整数记录复选框的数量。

private int checkedBoxs = 0;
private final CheckBox.OnCheckedChangeListener listener = 
    new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                status.set(position, true);
                checkedBoxs++;
                if (layout.getVisibility() == View.GONE)
                    layout.setVisibility(View.VISIBLE);
            } else {
                status.set(position, false);
                checkedBoxs--;
                if (checkedBoxs == 0)
                    layout.setVisibility(View.GONE);
            }
        }
    };

然后在onDraw()中执行cb.setOnCheckedChangeListener(listener)。消除onDraw()

中的所有其他allFalse逻辑