android 如何隐藏 linearlayout 号时 checkbox 签入listview

发布时间: 2017/3/13 22:25:08
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有 layoutlistview 和在它之下有是 linearlayout 与 horizantally 两个按钮,我想要当没有 checkbox 检查两个按钮隐藏,尽量做到但我的两个按钮隐藏的代码使和黑色而不是他们出现并当 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 变量

在主要 activity

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:

使用 setVisibility(View.GONE) 若要使按钮不仅被掩盖起来,但停止占领 spacelayout ,如果这就是你是什么意思"和黑色,而不是他们"。

至于他们不重新出现时他们检查,您需要添加一个listener上你 status 对象或调用 ra.notifyDataSetChanged()listview 重绘 (这将导致正在执行的 onDraw() 逻辑)。

编辑︰ 它可能是最容易为您声明您 onCheckedChangeListener 外的 adapter ,并使用它。使用一个整数来记录已检查框的数量。

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);
            }
        }
    };

然后执行 cb.setOnCheckedChangeListener(listener) 在你 onDraw()。消除所有其他 allFalse 逻辑,从你的 onDraw()

官方微信
官方QQ群
31647020